gpt4 book ai didi

c# - 如何保存 BitmapImage WinRT

转载 作者:行者123 更新时间:2023-11-30 13:58:45 26 4
gpt4 key购买 nike

我有 BitmapImage:

BitmapImage image = new BitmapImage();
image.SetSource(memStream);

我想将图像保存到磁盘以备将来查看。我找不到如何执行此操作的工作示例?第二个问题。我需要获取像素的颜色(例如:Color cl=image.GetColorPixel(X,Y)),我该怎么做?谢谢!

最佳答案

这是我前段时间在网上找到的一段代码。我不确定,但如果我没记错的话,它来自 sdk 示例或 winrt 博客

这一切都归结为 WritabelBitmap 图像(就像其他已经发布的一样),创建一个解码器并将其推送到流中。

  /// <summary>
///
/// </summary>
/// <param name="writeableBitmap"></param>
/// <param name="outputFile"></param>
/// <param name="encoderId"></param>
/// <returns></returns>
public static async Task SaveToFile(
this WriteableBitmap writeableBitmap,
IStorageFile outputFile,
Guid encoderId)
{
try
{
Stream stream = writeableBitmap.PixelBuffer.AsStream();
byte[] pixels = new byte[(uint)stream.Length];
await stream.ReadAsync(pixels, 0, pixels.Length);

using (var writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(encoderId, writeStream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)writeableBitmap.PixelWidth,
(uint)writeableBitmap.PixelHeight,
96,
96,
pixels);
await encoder.FlushAsync();

using (var outputStream = writeStream.GetOutputStreamAt(0))
{
await outputStream.FlushAsync();
}
}
}
catch (Exception ex)
{
string s = ex.ToString();
}
}

关于c# - 如何保存 BitmapImage WinRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15494965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com