Best way to create Thumbnails of images

ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

Hi,

I need the function in my app, that the user can pick an image from the image gallery and a Thumbnail of this Image should the be stored in a Zip-File.
I have no problem accesing a stream of the just picked image through the MediaPlugin and I can write into a ZIP-File via a Stream.

What's the easiest way to get a Thumbnail image from the selected image?

All the best

Thomas

Tagged:

Best Answers

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭
    Accepted Answer

    @ThomasBurkhart said:
    One question. Instead of using compiler switches, would it also be possible to create a partial class with one part of the class in the platform specific projects?

    It would probably be better to use Dependency Injection for this. I've explained the basics in a blog post here.

Answers

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Has nobody an idea?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    Thanks a lot, that looks excatly like the function I was looking for!!!

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    One question. Instead of using compiler switches, would it also be possible to create a partial class with one part of the class in the platform specific projects?

  • GeraldVersluisGeraldVersluis NLUniversity ✭✭✭✭
    Accepted Answer

    @ThomasBurkhart said:
    One question. Instead of using compiler switches, would it also be possible to create a partial class with one part of the class in the platform specific projects?

    It would probably be better to use Dependency Injection for this. I've explained the basics in a blog post here.

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    I'm just trying to add this class to my project, but the compiler complains that he doesn't know the type Bitmap.

    Which usings have I to use to get these Android specifics?

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    @DanielL
    The class above scales the image but be keeping its aspect ratio which is fine. The problem ist you want to have the tumbnails of e.g. ListView to be all the same size, so it would be needed to crop the image after scaling if necessary.
    Any idea how I could change the above class to achive this?

  • MCvelMCvel USMember ✭✭

    Hi mates,

    I think I'm having now the same questions you had @ThomasBurkhart
    to keep the same aspect ratio for all thumbnails, or to handle that in the image gallery with a fixed size,
    also if one of you have knows what would be the best, to store the thumbnail in a separated folder or in the image itself using Exif ?

    thank you!

  • AlejandroSeguraAlejandroSegura USMember ✭✭

    @ThomasBurkhart said:
    I'm just trying to add this class to my project, but the compiler complains that he doesn't know the type Bitmap.

    Which usings have I to use to get these Android specifics?

    In case anybody else needs this I believe all you have to do is inside the Android method give it the namespace.

    So instead of:
    Bitmap originalImage = BitmapFactory.DecodeFile(imageData);

    You do:
    Android.Graphics.Bitmap originalImage = Android.Graphics.BitmapFactory.DecodeFile(imageData);

  • SaurabhRathiSaurabhRathi USMember ✭✭

    @DanielL, Not able to access "PictureDecoder" in winphone 8.1.
    Can you please provide the Resize the image for winphone 8.1, I think your code is just compatible with winphone 8.

  • SaurabhRathiSaurabhRathi USMember ✭✭

    @DanielL, Not able to access "PictureDecoder" in winphone 8.1.
    Can you please provide the Resize the image for winphone 8.1, I think your code is just compatible with winphone 8.

  • @DanielL said:
    Maybe that would help:

      public static class PhotoResizer
      {
          public static byte[] ResizeImage (byte[] imageData, float width, float height, int quality)
          {
              #if __IOS__
              return ResizeImageIOS ( imageData, width, height, quality);
              #endif
              #if __ANDROID__
              return ResizeImageAndroid ( imageData, width, height, quality );
              #endif 
              #if WINDOWS_PHONE
              return ResizeImageWinPhone ( imageData, width, height, quality );
              #endif
          }
    
    
          #if __IOS__
          public static byte[] ResizeImageIOS (byte[] imageData, float width, float height, int quality)
          {
              UIImage originalImage = ImageFromByteArray (imageData);
    
    
              float oldWidth = (float)originalImage.Size.Width;
              float oldHeight = (float)originalImage.Size.Height;
              float scaleFactor = 0f;
    
              if (oldWidth > oldHeight)
              {
                  scaleFactor = width / oldWidth;
              }
              else
              {
                  scaleFactor = height / oldHeight;
              }
    
              float newHeight = oldHeight * scaleFactor;
              float newWidth = oldWidth * scaleFactor;
    
              //create a 24bit RGB image
              using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero,
                  (int)newWidth, (int)newHeight, 8,
                  (int)(4 * newWidth), CGColorSpace.CreateDeviceRGB (),
                  CGImageAlphaInfo.PremultipliedFirst)) {
    
                  RectangleF imageRect = new RectangleF (0, 0, newWidth, newHeight);
    
                  // draw the image
                  context.DrawImage (imageRect, originalImage.CGImage);
    
                  UIKit.UIImage resizedImage = UIKit.UIImage.FromImage (context.ToImage ());
    
                  // save the image as a jpeg
                  return resizedImage.AsJPEG((float)quality).ToArray();
              }
          }
    
          public static UIKit.UIImage ImageFromByteArray(byte[] data)
          {
              if (data == null) {
                  return null;
              }
    
              UIKit.UIImage image;
              try {
                  image = new UIKit.UIImage(Foundation.NSData.FromArray(data));
              } catch (Exception e) {
                  Console.WriteLine ("Image load failed: " + e.Message);
                  return null;
              }
              return image;
          }
          #endif
    
          #if __ANDROID__
    
          public static byte[] ResizeImageAndroid (byte[] imageData, float width, float height, int quality)
          {
          // Load the bitmap
          Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
    
          float oldWidth = (float)originalImage.Width;
          float oldHeight = (float)originalImage.Height;
          float scaleFactor = 0f;
    
          if (oldWidth > oldHeight)
          {
              scaleFactor = width / oldWidth;
          }
          else
          {
              scaleFactor = height / oldHeight;
          }
    
          float newHeight = oldHeight * scaleFactor;
          float newWidth = oldWidth * scaleFactor;
          
          Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, (int)newHeight, false);
    
          using (MemoryStream ms = new MemoryStream())
          {
          resizedImage.Compress (Bitmap.CompressFormat.Jpeg, quality, ms);
          return ms.ToArray ();
          }
          }
    
          #endif
    
          #if WINDOWS_PHONE
    
          public static byte[] ResizeImageWinPhone (byte[] imageData, float width, float height)
          {
          byte[] resizedData;
    
          using (MemoryStream streamIn = new MemoryStream (imageData))
          {
          WriteableBitmap bitmap = PictureDecoder.DecodeJpeg (streamIn, (int)width, (int)height);
    
          using (MemoryStream streamOut = new MemoryStream ())
          {
          bitmap.SaveJpeg(streamOut, (int)width, (int)height, 0, 100);
          resizedData = streamOut.ToArray();
          }
          }
          return resizedData;
          }
    
          #endif
      }
    

    thanks.
    please help for write code in uwp(win10) no winphone .Also write this code for pcl.

    very thanks my friend

  • @DanielL said:
    Maybe that would help:

      public static class PhotoResizer
      {
          public static byte[] ResizeImage (byte[] imageData, float width, float height, int quality)
          {
              #if __IOS__
              return ResizeImageIOS ( imageData, width, height, quality);
              #endif
              #if __ANDROID__
              return ResizeImageAndroid ( imageData, width, height, quality );
              #endif 
              #if WINDOWS_PHONE
              return ResizeImageWinPhone ( imageData, width, height, quality );
              #endif
          }
    
    
          #if __IOS__
          public static byte[] ResizeImageIOS (byte[] imageData, float width, float height, int quality)
          {
              UIImage originalImage = ImageFromByteArray (imageData);
    
    
              float oldWidth = (float)originalImage.Size.Width;
              float oldHeight = (float)originalImage.Size.Height;
              float scaleFactor = 0f;
    
              if (oldWidth > oldHeight)
              {
                  scaleFactor = width / oldWidth;
              }
              else
              {
                  scaleFactor = height / oldHeight;
              }
    
              float newHeight = oldHeight * scaleFactor;
              float newWidth = oldWidth * scaleFactor;
    
              //create a 24bit RGB image
              using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero,
                  (int)newWidth, (int)newHeight, 8,
                  (int)(4 * newWidth), CGColorSpace.CreateDeviceRGB (),
                  CGImageAlphaInfo.PremultipliedFirst)) {
    
                  RectangleF imageRect = new RectangleF (0, 0, newWidth, newHeight);
    
                  // draw the image
                  context.DrawImage (imageRect, originalImage.CGImage);
    
                  UIKit.UIImage resizedImage = UIKit.UIImage.FromImage (context.ToImage ());
    
                  // save the image as a jpeg
                  return resizedImage.AsJPEG((float)quality).ToArray();
              }
          }
    
          public static UIKit.UIImage ImageFromByteArray(byte[] data)
          {
              if (data == null) {
                  return null;
              }
    
              UIKit.UIImage image;
              try {
                  image = new UIKit.UIImage(Foundation.NSData.FromArray(data));
              } catch (Exception e) {
                  Console.WriteLine ("Image load failed: " + e.Message);
                  return null;
              }
              return image;
          }
          #endif
    
          #if __ANDROID__
    
          public static byte[] ResizeImageAndroid (byte[] imageData, float width, float height, int quality)
          {
          // Load the bitmap
          Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
    
          float oldWidth = (float)originalImage.Width;
          float oldHeight = (float)originalImage.Height;
          float scaleFactor = 0f;
    
          if (oldWidth > oldHeight)
          {
              scaleFactor = width / oldWidth;
          }
          else
          {
              scaleFactor = height / oldHeight;
          }
    
          float newHeight = oldHeight * scaleFactor;
          float newWidth = oldWidth * scaleFactor;
          
          Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, (int)newHeight, false);
    
          using (MemoryStream ms = new MemoryStream())
          {
          resizedImage.Compress (Bitmap.CompressFormat.Jpeg, quality, ms);
          return ms.ToArray ();
          }
          }
    
          #endif
    
          #if WINDOWS_PHONE
    
          public static byte[] ResizeImageWinPhone (byte[] imageData, float width, float height)
          {
          byte[] resizedData;
    
          using (MemoryStream streamIn = new MemoryStream (imageData))
          {
          WriteableBitmap bitmap = PictureDecoder.DecodeJpeg (streamIn, (int)width, (int)height);
    
          using (MemoryStream streamOut = new MemoryStream ())
          {
          bitmap.SaveJpeg(streamOut, (int)width, (int)height, 0, 100);
          resizedData = streamOut.ToArray();
          }
          }
          return resizedData;
          }
    
          #endif
      }
    

    very good.

    please help me for write this code for uwp(win10) no winphone and help for write this code in pcl

  • @DanielL said:
    Maybe that would help:

      public static class PhotoResizer
      {
          public static byte[] ResizeImage (byte[] imageData, float width, float height, int quality)
          {
              #if __IOS__
              return ResizeImageIOS ( imageData, width, height, quality);
              #endif
              #if __ANDROID__
              return ResizeImageAndroid ( imageData, width, height, quality );
              #endif 
              #if WINDOWS_PHONE
              return ResizeImageWinPhone ( imageData, width, height, quality );
              #endif
          }
    
    
          #if __IOS__
          public static byte[] ResizeImageIOS (byte[] imageData, float width, float height, int quality)
          {
              UIImage originalImage = ImageFromByteArray (imageData);
    
    
              float oldWidth = (float)originalImage.Size.Width;
              float oldHeight = (float)originalImage.Size.Height;
              float scaleFactor = 0f;
    
              if (oldWidth > oldHeight)
              {
                  scaleFactor = width / oldWidth;
              }
              else
              {
                  scaleFactor = height / oldHeight;
              }
    
              float newHeight = oldHeight * scaleFactor;
              float newWidth = oldWidth * scaleFactor;
    
              //create a 24bit RGB image
              using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero,
                  (int)newWidth, (int)newHeight, 8,
                  (int)(4 * newWidth), CGColorSpace.CreateDeviceRGB (),
                  CGImageAlphaInfo.PremultipliedFirst)) {
    
                  RectangleF imageRect = new RectangleF (0, 0, newWidth, newHeight);
    
                  // draw the image
                  context.DrawImage (imageRect, originalImage.CGImage);
    
                  UIKit.UIImage resizedImage = UIKit.UIImage.FromImage (context.ToImage ());
    
                  // save the image as a jpeg
                  return resizedImage.AsJPEG((float)quality).ToArray();
              }
          }
    
          public static UIKit.UIImage ImageFromByteArray(byte[] data)
          {
              if (data == null) {
                  return null;
              }
    
              UIKit.UIImage image;
              try {
                  image = new UIKit.UIImage(Foundation.NSData.FromArray(data));
              } catch (Exception e) {
                  Console.WriteLine ("Image load failed: " + e.Message);
                  return null;
              }
              return image;
          }
          #endif
    
          #if __ANDROID__
    
          public static byte[] ResizeImageAndroid (byte[] imageData, float width, float height, int quality)
          {
          // Load the bitmap
          Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
    
          float oldWidth = (float)originalImage.Width;
          float oldHeight = (float)originalImage.Height;
          float scaleFactor = 0f;
    
          if (oldWidth > oldHeight)
          {
              scaleFactor = width / oldWidth;
          }
          else
          {
              scaleFactor = height / oldHeight;
          }
    
          float newHeight = oldHeight * scaleFactor;
          float newWidth = oldWidth * scaleFactor;
          
          Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, (int)newHeight, false);
    
          using (MemoryStream ms = new MemoryStream())
          {
          resizedImage.Compress (Bitmap.CompressFormat.Jpeg, quality, ms);
          return ms.ToArray ();
          }
          }
    
          #endif
    
          #if WINDOWS_PHONE
    
          public static byte[] ResizeImageWinPhone (byte[] imageData, float width, float height)
          {
          byte[] resizedData;
    
          using (MemoryStream streamIn = new MemoryStream (imageData))
          {
          WriteableBitmap bitmap = PictureDecoder.DecodeJpeg (streamIn, (int)width, (int)height);
    
          using (MemoryStream streamOut = new MemoryStream ())
          {
          bitmap.SaveJpeg(streamOut, (int)width, (int)height, 0, 100);
          resizedData = streamOut.ToArray();
          }
          }
          return resizedData;
          }
    
          #endif
      }
    

    very good.

    please help me for write this code for uwp(win10) no winphone and help for write this code in pcl

Sign In or Register to comment.