简体   繁体   English

如何使用c#将图像转换为Windows Phone 8.1中的字节数组

[英]How to convert an image to byte array in windows phone 8.1 using c#

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? Windows Phone 7上的转换图像到字节数组没有System.Drawing Dll任何其他方式? but WriteableBitmap does not contain SaveJpeg method in Windows phone 8.1 environment. 但是,在Windows Phone 8.1环境中,WriteableBitmap不包含SaveJpeg方法。

On Windows Phone 8.1 Silverlight applications your code works fine, I assume that myImage is a BitmapImage . Windows Phone 8.1 Silverlight应用程序上,您的代码工作正常,我假设myImage是一个BitmapImage The only thing just must do is to wait for the BitmapImage to load completly: 唯一必须做的就是等待BitmapImage加载完全:

using System.IO;
using System.Windows.Media.Imaging;
...
myImage = new BitmapImage(new Uri("http://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. 我刚测试了那段代码,它在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: 但是当您评论另一篇文章时,您无法引用Microsoft.Phone ,您可能正在使用Windows Phone 8.1 Store应用程序,在这种情况下,您可以使用以下代码:

using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage.Streams;
using System.Runtime.InteropServices.WindowsRuntime;
...
BitmapImage bitmapImage = new BitmapImage(new Uri("http://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);

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) . 我怀疑您是针对Windows运行时应用程序,因为您没有找到此答案中建议的命名空间(这将适用于WP8.1 Silverlight)

In Windows Runtime apps you can use Encoder - the sample code can look like this: 在Windows运行时应用程序中,您可以使用Encoder - 示例代码如下所示:

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

SaveJpeg is an extension method ( http://msdn.microsoft.com/en-US/library/windowsphone/develop/system.windows.media.imaging.extensions.savejpeg(v=vs.105).aspx ). SaveJpeg是一种扩展方法( 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; 您是否在项目引用中引用了Microsoft.Phoneusing System.Windows.Media.Imaging;添加using System.Windows.Media.Imaging; to your .cs file? 你的.cs文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows Phone 8.1中将字节数组转换为图像的同步方法 - Synchronous way to convert byte array to image in Windows Phone 8.1 在Windows Phone 8.1中将IBuffer转换为字节数组,怎么样? - Convert an IBuffer to a byte array in Windows Phone 8.1, how? 如何将 C# 中的 Image 对象制作为 Windows Phone 8.1 - How to make an Image object in C# to Windows Phone 8.1 在Windows Phone 8.1中加载,显示,转换来自字节数组(数据库)的图像 - Load, show, convert image from byte array (database) in Windows Phone 8.1 如何在Windows Phone 8中将像素字节数组转换为位图图像 - How to convert pixel byte array to bitmap image in windows phone 8 C# - 在Windows 7 Phone中将byte []转换为String - C# - Convert byte[] to String in Windows 7 Phone 如何将字节数组转换为mp4文件文件MediaElment Windows Phone 8.1 - how to convert byte array to mp4 file file MediaElment windows phone 8.1 如何使用解决方案中的图像制作字节数组。 (Windows Phone 8.1运行时) - How to make a byte array with an image from solution. (Windows Phone 8.1 runtime) 如何在Windows Phone 8.1上从数据库获取和显示图像作为字节数组 - How to get and display image as byte array from database on Windows Phone 8.1 通过fileopenpicker获取所选图像的路径Windows Phone 8.1 C# - get path of selected image by fileopenpicker windows phone 8.1 C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM