1

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.

Community
  • 1
  • 1
Icet
  • 678
  • 2
  • 13
  • 31
  • http://stackoverflow.com/questions/6597676/bitmapimage-to-byte – Nathan Cooper Sep 08 '15 at 09:07
  • I tried to use this solutions but with JpegBitmapEncoder there is a lot of errors between using System.Windows.Media.Imaging; and using Windows.UI.Xaml.Media.Imaging; then I use BitmapEncoder but it doesn't have method like .Save – Icet Sep 08 '15 at 09:13

4 Answers4

1

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();
        }
    }
Icet
  • 678
  • 2
  • 13
  • 31
0

see the below link it might help

https://social.msdn.microsoft.com/Forums/en-US/713c0ed1-d979-43ef-8857-bbe0b35576a9/windows-8-how-to-convert-bitmapimage-into-byte?forum=winappswithcsharp

Developer Nation
  • 374
  • 3
  • 4
  • 20
  • Please don't post link-only answers, which are subject to bit-rot and provide very little information in and of themselves. – Jon Skeet Sep 08 '15 at 09:06
  • Yes I found it also but I'm still getting "There is no argument given that corresponds to the required formal parameter 'pixelHeight' of 'WriteableBitmap.WriteableBitmap(int, int)'" when in code is WriteableBitmap btmMap = new WriteableBitmap(img); – Icet Sep 08 '15 at 09:09
0

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();
Rohit
  • 10,056
  • 7
  • 50
  • 82
  • It gives me the same error "There is no argument given that corresponds to the required formal parameter 'pixelHeight' of 'WriteableBitmap.WriteableBitmap(int, int)' – Icet Sep 08 '15 at 09:07
  • After your edit: I tried it and I'm getting this error on this line: "Stream stream = imageSource.StreamSource;" 'BitmapImage' does not contain a definition for 'StreamSource' and no extension method 'StreamSource' accepting a first argument of type 'BitmapImage' could be found (are you missing a using directive or an assembly reference?) – Icet Sep 08 '15 at 09:22
  • what is the namespace for `BitmapImage` ? – Rohit Sep 08 '15 at 09:28
  • It's using Windows.UI.Xaml.Media.Imaging; – Icet Sep 08 '15 at 09:28
  • Do you have any idea? – Icet Sep 08 '15 at 10:48
  • Thanks but what referencee I need to add? PresentationCore.dll ? It's not going to work with Windows Phone. – Icet Sep 08 '15 at 12:09
0

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();
B H
  • 1,730
  • 18
  • 24