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 ?

3
  • Can you try with ms.ToArray() instead of ms.GetBuffer()? Commented Jul 29, 2013 at 13:34
  • @KooKiz Thanks for reply. I have tried that but it's not working Commented Jul 29, 2013 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. Commented Jul 29, 2013 at 13:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply. I think System.Drawing in not support in windows phone
Ah my apologies. Are the answers here of use? stackoverflow.com/questions/4732807/…
It gives me an error "ConvertTo not implemented in base TypeConverter."
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<BitmapImage>"
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.