4

I am using this code

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

from Convert image to byte array on Windows Phone 7 No System.Drawing Dll any other way? but WriteableBitmap does not contain SaveJpeg method in Windows phone 8.1 environment.

Community
  • 1
  • 1
ababil
  • 73
  • 3
  • 12
  • There must be something that causing you to think that it doesn't work. – mbm May 22 '14 at 07:53
  • Are you targeting Silverlight or Runtime - depending on this there are different methods to save bitmap. – Romasz May 22 '14 at 08:11

3 Answers3

3

On Windows Phone 8.1 Silverlight applications your code works fine, I assume that myImage is a BitmapImage. The only thing just must do is to wait for the BitmapImage to load completly:

using System.IO;
using System.Windows.Media.Imaging;
...
myImage = new BitmapImage(new Uri("https://i.stack.imgur.com/830Ke.jpg?s=128&g=1", UriKind.Absolute));
myImage.CreateOptions = BitmapCreateOptions.None;
myImage.ImageOpened += myImage_ImageOpened;
...
void myImage_ImageOpened(object sender, RoutedEventArgs e)
{
    MemoryStream ms = new MemoryStream();
    WriteableBitmap wb = new WriteableBitmap(myImage);
    wb.SaveJpeg(ms, myImage.PixelWidth, myImage.PixelHeight, 0, 100);
    byte[] imageBytes = ms.ToArray();
}

I just tested that code and it works perfectly on WP8.1.

But as you commented on another post you can't reference Microsoft.Phone, you are probably doing a Windows Phone 8.1 Store App, in which case you can use the following code:

using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage.Streams;
using System.Runtime.InteropServices.WindowsRuntime;
...
BitmapImage bitmapImage = new BitmapImage(new Uri("https://i.stack.imgur.com/830Ke.jpg?s=128&g=1"));
RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource);
var streamWithContent = await rasr.OpenReadAsync();
byte[] buffer = new byte[streamWithContent.Size];
await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None);
Johan Falk
  • 4,341
  • 2
  • 30
  • 42
2

I suspect that you are targeting Windows Runtime Apps as you haven't found the namespace suggested in this answer (which will work for WP8.1 Silverlight).

In Windows Runtime apps you can use Encoder - the sample code can look like this:

// lets assume that you have your image in WriteableBitmap yourWb
using (MemoryStream ms = new MemoryStream())
{
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms.AsRandomAccessStream());
    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
        BitmapAlphaMode.Ignore,
        imageWidth,
        imageHeight,
        horizontalDPI,
        verticalDPI,
        yourWb.PixelBuffer.ToArray());

    await encoder.FlushAsync();
}
Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • thanks @Romasz to your reply. I resolved my problem another way. – ababil May 22 '14 at 09:53
  • @ababil If I may ask - how? Maybe you can add yourself an answer? – Romasz May 22 '14 at 09:55
  • Sure @Romasz. My colplete code: XAML: – ababil May 22 '14 at 10:22
  • BitmapImage bitmapImage = new BitmapImage(new Uri("https://lh6.googleusercontent.com/ImYzXmXzbrWHt5wsrRkN_53OWH9nP6y_jqvH3nbXIQ=s207-p-no?s=128&g=1")); RandomAccessStreamReference rasr = RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource); var streamWithContent = await rasr.OpenReadAsync(); byte[] buffer = new byte[streamWithContent.Size]; await streamWithContent.ReadAsync(buffer.AsBuffer(), (uint)streamWithContent.Size, InputStreamOptions.None); – ababil May 22 '14 at 11:12
  • @Romasz There is no explanation for how to set image data to writeable bitmap. Youre creating writeable bitmap with image width and height. And there is no data in 'yourWb.pixelbuffer'. – MohanRajNK Dec 19 '14 at 14:34
  • @MohanRajNK Yeah, you are right, but this was just an example to show the right direction. If you are looking how to get pixelbuffer, you may take a look [at this answer](http://stackoverflow.com/a/25995145/2681948). – Romasz Dec 19 '14 at 15:15
0

SaveJpeg is an extension method (http://msdn.microsoft.com/en-US/library/windowsphone/develop/system.windows.media.imaging.extensions.savejpeg(v=vs.105).aspx). Have you referenced Microsoft.Phone in your project references and added using System.Windows.Media.Imaging; to your .cs file?

tster
  • 17,883
  • 5
  • 53
  • 72
  • thanks for quick reply @tster. I have not found Microsoft.Phone reference as you mentioned.Do you suggest me how to achieve this another way. – ababil May 22 '14 at 08:23
  • @ababil, Try creating a "Windows Phone" project in Visual Studio. That will come with Microsoft.Phone referenced. – tster May 22 '14 at 08:29
  • Microsoft.Phone namespace no longer exists in Windows Phone 8.1 platform. – NadaNK May 28 '15 at 06:23