Thursday, April 17, 2014

Create Thumbnail in C# windows application

//This function creates the Thumbnail image and returns the
      //image created in Byte() format
      public static byte[] createThumnail(Stream ImageStream, Double tWidth, Double tHeight)
      {
          System.Drawing.Image g = System.Drawing.Image.FromStream(ImageStream);
          Size thumbSize = new Size();
          thumbSize = NewThumbSize(g.Width, g.Height, tWidth, tHeight);
          Bitmap imgOutput = new Bitmap(g, thumbSize.Width, thumbSize.Height);
          MemoryStream imgStream = new MemoryStream();
          System.Drawing.Imaging.ImageFormat thisFormat = g.RawFormat;
          imgOutput.Save(imgStream, thisFormat);
          int imgBinLength = (int)imgStream.Length;
          byte[] imgBin = new byte[imgBinLength];
          imgStream.Position = 0;
          int n = imgStream.Read(imgBin, 0, imgBinLength);
          g.Dispose();
          imgOutput.Dispose();
          return imgBin;
      }
      public static Size NewThumbSize(Double currentwidth, Double currentheight, Double newWidth, Double newHeight)
      {
          //Calculate the Size of the New image
          Double tempMultiplier;
          if (currentheight > currentwidth)        // Then it's portrait
          {
              tempMultiplier = newHeight / currentheight;
          }
          else
          {
              tempMultiplier = newWidth / currentwidth;
          }

          Size NewSize = new Size(Convert.ToInt16(currentwidth * tempMultiplier), Convert.ToInt16(currentheight * tempMultiplier));
          return NewSize;
      }


      public static Image byteArrayToImage(byte[] byteArrayIn)
      {
          MemoryStream ms = new MemoryStream(byteArrayIn);
          Image returnImage = Image.FromStream(ms);
          return returnImage;
      }

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...