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.

2
  • There must be something that causing you to think that it doesn't work. Commented May 22, 2014 at 7:53
  • Are you targeting Silverlight or Runtime - depending on this there are different methods to save bitmap. Commented May 22, 2014 at 8:11

3 Answers 3

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.sstatic.net/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.sstatic.net/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);
Sign up to request clarification or add additional context in comments.

Comments

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();
}

6 Comments

thanks @Romasz to your reply. I resolved my problem another way.
@ababil If I may ask - how? Maybe you can add yourself an answer?
Sure @Romasz. My colplete code: XAML: <Button Grid.Row="1" x:Name="btnSubmit" Content="Submit" Click="btnSubmit_Click"/> <Image Grid.Row="2" x:Name="imagePreivew" />
BitmapImage bitmapImage = new BitmapImage(new Uri("lh6.googleusercontent.com/…)); 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);
@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'.
|
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?

3 Comments

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, Try creating a "Windows Phone" project in Visual Studio. That will come with Microsoft.Phone referenced.
Microsoft.Phone namespace no longer exists in Windows Phone 8.1 platform.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.