WPF 中 WriteableBitmap 的怪异

问题描述 投票:0回答:1

我正在 WPF/.Net 8 中创建一个gray8 可写位图。

当我将其设置为图像源时,结果是一个全黑图像 - 大小正确。

如果我保存可写位图(使用 PngEncoder),图像将按我的预期保存。如果我将 png 图像从磁盘重新加载到图像控件,那么它会显示得很好。

但是,如果我重新创建图像并重新保存并重新加载它,则不会显示该图像 - 保留原始图像。

我主要关心的是知道我需要做什么才能使可写位图不显示为黑色,因为我不需要保存等等。

位图的目的是显示一系列条形,其暗度对应于数组中的值 - 一种密度直方图

public BitmapSource CreateLineWeightImage()
{
    if (LineWeights == null) throw new InvalidOperationException("Must GetLineWeights first");

    WriteableBitmap lineWeights = new WriteableBitmap(LineWeightImageWidth, OriginalImage.PixelHeight, 96, 96, PixelFormats.Gray8, null);
    lineWeights.Lock();
    unsafe
    {
        for (int y = 0; y < lineWeights.PixelHeight; ++y)
        {
            for (int x = 0; x < LineWeightImageWidth; ++x)
            {
                byte* PixelPtr = (byte*)lineWeights.BackBuffer + y * lineWeights.BackBufferStride + x;

                *((byte*)PixelPtr) = (byte)LineWeights[y];
            }
        }
    }

    lineWeights.Unlock();
    LineWeightsImage = lineWeights;
    return lineWeights;
}

谢谢

伊恩

c# wpf writeablebitmap
1个回答
0
投票

你忘了打电话

AddDirtyRect
:

lineWeights.AddDirtyRect(new Int32Rect(
    0, 0, lineWeights.PixelWidth, lineWeights.PixelHeight));

lineWeights.Unlock();
© www.soinside.com 2019 - 2024. All rights reserved.