Search code examples
.netwindows-phone-8arraysimage-conversion

How to change BitmapImage to ConvertToByte in Windows Phone 8


 BitmapImage bm = new BitmapImage(new Uri("/Assets/ToolKit.Content/RedIcon/arab-woman.jpg", UriKind.RelativeOrAbsolute));

 using (MemoryStream ms = new MemoryStream())
 {
      WriteableBitmap btmMap = new WriteableBitmap(bm);
      System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, bm.PixelWidth, bm.PixelHeight, 0, 100);
      bm = null;
      byte[] a = ms.ToArray();
 }

I am using this code but it showing error I cannot convert to WriteableBitmap.


Solution

  • You have an error because you didnt really download the image. If you open your BitmapImage in your debugger, I am pretty sure you can see that there is nothing here. You can force the download by using an Image. I dont know if it is a great solution, but it is a working solution.

    Image image = new Image {Source = new BitmapImage(new Uri("/Assets/ToolKit.Content/RedIcon/arab-woman.jpg", UriKind.Relative))};
    using (MemoryStream ms = new MemoryStream())
    {
        WriteableBitmap btmMap = new WriteableBitmap(image, null);
        btmMap.SaveJpeg(ms, btmMap.PixelWidth, btmMap.PixelHeight, 0, 100);
        byte[] a = ms.ToArray();
    }