简体   繁体   中英

Convert BitmapImage to byte[]

I have problem with converting BitmapImage to byte[]. I tried a lot of solutions and nothing works, every time i get different errors.

For example i found nice solutions but it also doesn't work. What's wrong with it?

I'm using Windows Phone 8.1.

public static byte[] ImageToBytes(BitmapImage img)
{
    using (MemoryStream ms = new MemoryStream())
    {
        WriteableBitmap btmMap = new WriteableBitmap(img);
        System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
        img = null;
        return ms.ToArray();
    }
}

this was taken from here: Convert Bitmap Image to byte array (Windows phone 8)

There is no argument given that corresponds to the required formal parameter 'pixelHeight' of 'WriteableBitmap.WriteableBitmap(int, int)'

The type or namespace name 'Extensions' does not exist in the namespace 'System.Windows.Media.Imaging' (are you missing an assembly reference?)

or if somebody has got another idea how to convert it, please post it. Thanks a lot for any help!

I also tried this:BitmapImage to byte[]

but there was problem with usings

'BitmapImage' is an ambiguous reference between 'System.Windows.Media.Imaging.BitmapImage' and 'Windows.UI.Xaml.Media.Imaging.BitmapImage'

so I used "BitmapEncoder" but it doesn't have method like Save and Frame.

I think that it can't be done on this platform. I change my project to Windows Phone Silverlight/8.0 and there is working everything.

 public static BitmapImage BytesToImage(byte[] bytes)
    {
        BitmapImage bitmapImage = new BitmapImage();
        try
        {
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                bitmapImage.SetSource(ms);
                return bitmapImage;
            }
        }
        finally { bitmapImage = null; }
    }

    public static byte[] ImageToBytes(BitmapImage img)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            WriteableBitmap btmMap = new WriteableBitmap(img);
            System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, img.PixelWidth, img.PixelHeight, 0, 100);
            img = null;
            return ms.ToArray();
        }
    }

Have you tried

      MemoryStream ms = new MemoryStream();
      WriteableBitmap wb = new WriteableBitmap(myImage);
      wb.SaveJpeg(ms, myImage.PixelWidth, myImage.PixelHeight, 0, 100);
      byte[] imageBytes = ms.ToArray();

You can also use extension method here

      public static class MyExtensions
      {
          public static Byte[] ByteFromImage(this System.Windows.Media.Imaging.BitmapImage imageSource)
          {
            Stream stream = imageSource.StreamSource;
            Byte[] imagebyte = null;
            if (stream != null && stream.Length > 0)
            {
               using (BinaryReader br = new BinaryReader(stream))
               {
                 imagebyte = br.ReadBytes((Int32)stream.Length);
               }
            }

              return imagebyte;
          }
       }

and then call

        System.Windows.Media.Imaging.BitmapImage myImage = new System.Windows.Media.Imaging.BitmapImage();
        byte[] imageBytes = myImage.ByteFromImage();

If you're going to do any transformation on the image, you'll want to use the appropriate image encoder, then do something like below. If you're working with a multi-frame image (eg TIF), you need to add the frames one at a time to the encoder or you'll only get the first frame of the image.

MemoryStream ms = null;
TiffBitmapEncoder enc = null

enc = new TiffBitmapEncoder();
enc.Compression = TiffCompressOption.Ccitt4;
enc.Frames.Add(BitmapFrame.Create(bmpImg));
using (ms = new MemoryStream())
{
  enc.Save(ms);
}
return ms.ToArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM