none
reduce save image size to be around 250 - 500 kb.

    Question

  • hi,

    i have following code to save as image in silverlight

    private static byte[] GetBytesInternalPng(UIElement element)
        {
          WriteableBitmap w = new WriteableBitmap(element, new TranslateTransform());
          EditableImage imageData = new EditableImage(w.PixelWidth, w.PixelHeight);
    
          try
          {
            for (int y = 0; y < w.PixelHeight; ++y)
            {
              for (int x = 0; x < w.PixelWidth; ++x)
              {
                int pixel = w.Pixels[w.PixelWidth * y + x];
                imageData.SetPixel(x, y,
    
                                   (byte)((pixel >> 16) & 0xFF),
                                   (byte)((pixel >> 8) & 0xFF),
                                   (byte)(pixel & 0xFF), (byte)((pixel >> 24) & 0xFF)
                  );
              }
            }
          }
          catch (System.Security.SecurityException)
          {
            MessageBox.Show("Cannot print images from other domains");
            return null;
          }
    
          Stream pngStream = imageData.GetStream();
          StreamReader sr = new StreamReader(pngStream);
          byte[] binaryData = new Byte[pngStream.Length];
          pngStream.Read(binaryData, 0, (int)pngStream.Length);
    
          return binaryData;
        }

    it works well well but image is large at about 2.5 megabytes. but I would like to reduce the resulting image size to be around 250 - 500 kb.


     

    Wednesday, February 16, 2011 1:52 AM

Answers

  • Hi Sunil. Image formats like BMP or PNG work with lossless compression or without compression at all. This means that depending on the content of an image, the size can be pretty big. Other image formats like JPG use advanced compression algorithms that decrease quality (lossy) but often are able to achieve much smaller sizes (especially for photos and similar). Your only two chances are: reduce the resolution of the image (resize to smaller dimensions) to reduce the size, or switch to a different image format like the mentioned JPG, where you can lower the quality setting until you reach the desired size.


    Wednesday, February 16, 2011 5:10 AM
  • Thanks you MisterGoodCat

    i use JPG FJ.Core and it's works :)

    Wednesday, February 16, 2011 7:44 AM

All replies

  • Hi Sunil. Image formats like BMP or PNG work with lossless compression or without compression at all. This means that depending on the content of an image, the size can be pretty big. Other image formats like JPG use advanced compression algorithms that decrease quality (lossy) but often are able to achieve much smaller sizes (especially for photos and similar). Your only two chances are: reduce the resolution of the image (resize to smaller dimensions) to reduce the size, or switch to a different image format like the mentioned JPG, where you can lower the quality setting until you reach the desired size.


    Wednesday, February 16, 2011 5:10 AM
  • Thanks you MisterGoodCat

    i use JPG FJ.Core and it's works :)

    Wednesday, February 16, 2011 7:44 AM