You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinUI应用中从BitmapImage获取指定像素颜色的方法

在WinUI中从BitmapImage提取指定坐标的像素颜色

BitmapImage是为UI显示优化的对象,无法直接访问像素数据。要获取指定坐标的像素颜色,需要先将其转换为可操作的位图格式(如SoftwareBitmap或WriteableBitmap),再读取像素缓冲区。

方法1:使用SoftwareBitmap解码读取

SoftwareBitmap是Windows Runtime中处理像素数据的核心类,适合直接操作原始像素:

using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
using Windows.UI;

public async Task<Color> GetPixelColor(BitmapImage bitmap, int x, int y)
{
    // 校验坐标有效性
    if (x < 0 || x >= bitmap.PixelWidth || y < 0 || y >= bitmap.PixelHeight)
        throw new ArgumentOutOfRangeException(nameof(x), "指定坐标超出图像像素范围");

    // 打开BitmapImage的源数据流
    using IRandomAccessStream stream = await bitmap.OpenReadAsync();
    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

    // 解码为BGRA8格式的SoftwareBitmap(WinUI默认像素格式)
    SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(
        BitmapPixelFormat.Bgra8, 
        BitmapAlphaMode.Premultiplied);

    // 将像素数据复制到字节数组
    byte[] pixelBuffer = new byte[softwareBitmap.PixelWidth * softwareBitmap.PixelHeight * 4];
    softwareBitmap.CopyToBuffer(pixelBuffer.AsBuffer());

    // 计算目标像素的数组索引(每个像素占4字节:B、G、R、A)
    int pixelIndex = (y * softwareBitmap.PixelWidth + x) * 4;
    byte blue = pixelBuffer[pixelIndex];
    byte green = pixelBuffer[pixelIndex + 1];
    byte red = pixelBuffer[pixelIndex + 2];
    byte alpha = pixelBuffer[pixelIndex + 3];

    return Color.FromArgb(alpha, red, green, blue);
}

方法2:使用WriteableBitmap转换读取

如果需要后续对图像进行修改,WriteableBitmap会更方便,它支持直接编辑像素:

using Windows.Storage.Streams;
using Windows.UI;
using Windows.UI.Xaml.Media.Imaging;

public async Task<Color> GetPixelColorViaWriteableBitmap(BitmapImage bitmap, int x, int y)
{
    if (x < 0 || x >= bitmap.PixelWidth || y < 0 || y >= bitmap.PixelHeight)
        throw new ArgumentOutOfRangeException(nameof(x), "指定坐标超出图像像素范围");

    // 创建与BitmapImage尺寸一致的WriteableBitmap
    WriteableBitmap writeableBitmap = new WriteableBitmap(
        (int)bitmap.PixelWidth, 
        (int)bitmap.PixelHeight);

    // 从BitmapImage加载数据到WriteableBitmap
    using IRandomAccessStream stream = await bitmap.OpenReadAsync();
    await writeableBitmap.SetSourceAsync(stream);

    // 读取像素缓冲区
    byte[] pixelBuffer = new byte[writeableBitmap.PixelBuffer.Length];
    using Stream pixelStream = writeableBitmap.PixelBuffer.AsStream();
    await pixelStream.ReadAsync(pixelBuffer, 0, pixelBuffer.Length);

    // 解析目标像素颜色
    int pixelIndex = (y * writeableBitmap.PixelWidth + x) * 4;
    return Color.FromArgb(
        pixelBuffer[pixelIndex + 3],
        pixelBuffer[pixelIndex + 2],
        pixelBuffer[pixelIndex + 1],
        pixelBuffer[pixelIndex]);
}

关键注意事项

  • 确保图像加载完成:调用上述方法前,需等待BitmapImage.ImageOpened事件触发,避免因图像未加载导致的流读取失败。
  • 像素格式转换:示例中使用BGRA8格式,这是WinUI的默认格式,注意颜色通道顺序是蓝→绿→红→Alpha,转换为Color对象时需调整通道顺序。
  • 性能优化:如果需要多次读取像素,建议缓存SoftwareBitmap或WriteableBitmap,避免重复解码。

内容的提问来源于stack exchange,提问作者keal24

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 05:35:40