3

I am trying to convert bitmap Image to Byte array. I have select all the image by using MediaLibrary class and added it into a list of bitmap images. Here is my code

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (!store.DirectoryExists("ImagesZipFolder"))
            {
                store.CreateDirectory("ImagesZipFolder");
                for (int i = 0; i < imgname.Count(); i++)
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder\" + imgname[i], System.IO.FileMode.CreateNew, store))
                    {
                            byte[] bytes = null;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                WriteableBitmap wBitmap = new WriteableBitmap(ImgCollection[i]);
                                wBitmap.SaveJpeg(ms, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
                                stream.Seek(0, SeekOrigin.Begin);
                                bytes = ms.GetBuffer();
                                stream.Write(bytes, 0, bytes.Length);
                            }
                    //    byte[] bytes = Encoding.UTF8.GetBytes(imgname[i]);//new byte[ImgCollection[i].PixelWidth * ImgCollection[i].PixelHeight * 4];                           
                    //    stream.Write(bytes, 0, bytes.Length);
                    }
                }
            }
            else {
                directory = true;
            }
          }

Basically what I am trying to do is, selecting all images or photo from device and create a zip file of that images. I was successful in creating a zip file of images. When I extract that file there is some images, but the problem is when I double click on image, I can't see that image. I think the problem is in reading the bytes of image. I am not getting what's wrong? Is my code is correct ?

  • Can you try with `ms.ToArray()` instead of `ms.GetBuffer()`? – Kevin Gosse Jul 29 '13 at 13:34
  • @KooKiz Thanks for reply. I have tried that but it's not working –  Jul 29 '13 at 13:34
  • As a side note, you don't have to use a byte array. After the seek, directly call `ms.CopyTo(stream);`. It won't solve your issue, but it'll make your code easier to read and save a bit of RAM. And the seek should be call on ms, not on stream. – Kevin Gosse Jul 29 '13 at 13:50

2 Answers2

3

Perhaps you can try the below. I know this code maintains the image, so if you have no luck using this, you may have a different issue.

    // Convert the new image to a byte[]
    ImageConverter converter = new ImageConverter();
    byte[] newBA = (byte[])converter.ConvertTo(newImage, typeof(byte[]));

The ImageConverter is of the System.Drawing namespace.


Update:

http://msdn.microsoft.com/en-GB/library/system.windows.media.imagesourceconverter.convertto.aspx

You should be able to use this in place of the System.Drawing type I suggested.

stevepkr84
  • 1,637
  • 2
  • 13
  • 23
  • Thanks for reply. I think System.Drawing in not support in windows phone –  Jul 29 '13 at 13:35
  • Ah my apologies. Are the answers here of use? http://stackoverflow.com/questions/4732807/conversion-of-bitmapimage-to-byte-array?rq=1 – stevepkr84 Jul 29 '13 at 13:57
  • It gives me an error "ConvertTo not implemented in base TypeConverter." –  Jul 30 '13 at 05:10
  • I have added this code ImageSourceConverter converter = new ImageSourceConverter();byte[] bytes = (byte[])converter.ConvertTo(ImgCollection[i], typeof(byte[])); Where ImgCollection is the objecct of "List" –  Jul 30 '13 at 05:12
0

There is no need to save the WriteableBitmap to a MemoryStream and then copy it to an IsolatedStorageFileStream. Just save the bitmap directly to the IsolatedStorageFileStream.

using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(@"ImagesZipFolder\" + imgname[i], System.IO.FileMode.CreateNew, store))
{
    WriteableBitmap wBitmap = new WriteableBitmap(ImgCollection[i]);
    wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
}

This will allow you to save on memory as well. If you really want to save memory, you could reuse the WriteableBitmap.

Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41