【问题标题】:How to convert imagesource to byte[] or stream in uwp?如何将图像源转换为字节 [] 或 uwp 中的流?
【发布时间】:2018-11-09 09:23:15
【问题描述】:

我有一个ImageSource imageSource,想转成字节数组或者流,怎么办?我试过了

WriteableBitmap bitmap = imageSource as WriteableBitmap;
var stream = bitmap.PixelBuffer.AsStream();

但是位图是空的...

【问题讨论】:

标签: uwp


【解决方案1】:

你应该确定 imageSource 是 WriteableBitmap 因为它可以是 BitmapImage 或 WriteableBitmap ……

如果imageSource是WriteableBitmap,你可以写你的代码。

但是如果 imageSource 是 BitmapImage 你应该使用WriteableBitmapEx

首先是使用nuget下载WriteableBitmapEx。

然后你可以把它改成WriteableBitmap。

 WriteableBitmap image = await BitmapFactory.New(1, 1).FromContent((BitmapImage).UriSource);

然后您可以将 WriteableBitmap 转换为流。

如果你的 imageSource 是 RenderTargetBitmap 你可以使用这个代码。

private async Task<string> ToBase64(RenderTargetBitmap bitmap)
{
    var bytes = (await bitmap.GetPixelsAsync()).ToArray();
    return await ToBase64(bytes, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight);
}

见(中文):https://lindexi.gitee.io/post/win10-uwp-%E8%AF%BB%E5%8F%96%E4%BF%9D%E5%AD%98WriteableBitmap-BitmapImage.html

【讨论】:

  • 对于WriteableBitmap,您可以从这里下载/安装:nuget.org/packages/WriteableBitmapEx
  • 反正链接坏了,你的代码不够清晰。例如,.UriSource 在哪个对象上调用?
  • @YoavFeuerstein Thx 我修复了它。
猜你喜欢
  • 2015-10-17
  • 1970-01-01
  • 2016-05-08
  • 2016-09-27
  • 1970-01-01
  • 2018-08-21
  • 2019-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多