如何在Windows应用商店应用程序(c#)中将BitmapImage转换为byte []? [英] How to convert BitmapImage to byte[] in a Windows Store App (c#)?

查看:54
本文介绍了如何在Windows应用商店应用程序(c#)中将BitmapImage转换为byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该来这里:

  private   byte  [] ConvertToBytes(Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage)
{
???
}





如果有人有工作解决方案,请与我分享。

解决方案

HI Sogorka,



如果我理解你的问题是正确的,这里是将位图图像转换为字节数组的链接。

如下所示,它转换并保存在数据库中。但是你只需要转换部分。

如果你愿意,可以单独试试。



这里是链接 [ ^ ]



希望这对你有所帮助。



谢谢,

Rk


我找到了将结果作为byte []或BitmapImage获取的另一种方法如下:



  private   async 任务DownloadUrlAsync( string  url)
{
byte [] ByteResult = null ;
BitmapImage BitmapResult = new BitmapImage();
ImageSource ImageSourceResult = null ;
if (!string.IsNullOrEmpty(url))
{
HttpClient client = HttpClient();
ByteResult = await client.GetByteArrayAsync(url); // 结果为:byte []
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
await ras.WriteAsync(ByteResult.AsBuffer());
ras.Seek( 0 );
BitmapResult.SetSource(ras);
ImageSourceResult = BitmapResult; // 结果为:ImageSource或BitmapImage
}
}


What should come here:

private byte[] ConvertToBytes(Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage)
{
  ???
}



If someone has a working solution please share with me.

解决方案

HI Sogorka,

If I understand your question correct,here is the link to convert bitmap image to byte array.
Where as in the below link it converts and save it in database.But you need just conversion part.
Try that alone if you want.

And here is the link[^]

Hope this helps you a bit.

Thanks,
Rk


I found an alternate way to get the result as a byte[] or a BitmapImage as follows:

private async Task DownloadUrlAsync(string url)
{
  byte[] ByteResult = null;
  BitmapImage BitmapResult = new BitmapImage();
  ImageSource ImageSourceResult = null;
  if (!string.IsNullOrEmpty(url))
  {
    HttpClient client = new HttpClient();
    ByteResult = await client.GetByteArrayAsync(url);  //result as: byte[]
    InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
    await ras.WriteAsync(ByteResult.AsBuffer());
    ras.Seek(0);
    BitmapResult.SetSource(ras);
    ImageSourceResult = BitmapResult;  //result as: ImageSource or BitmapImage
  }
}


这篇关于如何在Windows应用商店应用程序(c#)中将BitmapImage转换为byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆