gpt4 book ai didi

c# - byte[]转灰度BitmapImage

转载 作者:太空宇宙 更新时间:2023-11-03 14:28:06 25 4
gpt4 key购买 nike

我从一个 128 x 128 的 double 组开始,然后将其转换为一维字节数组,每个 double 值都具有比例值。

然后我获取这个字节数组并将其转换为内存流(下面的 dataStream)并尝试将其放入 BitmapImage 中,如下所示:

imgScan.Width = 128;
imgScan.Height = 128;
BitmapImage bi = new BitmapImage();
bi.SourceRect = new Int32Rect(0, 0, width, height);
bi.StreamSource = dataStream;
imgScan.Source = bi;

这里 imgScan 是一个 System.Windows.Controls.Image

这不会产生预期的图像(我只得到一个白色方 block )。

我应该怎么做?

最佳答案

我想您会发现在您的代码中,流应该包含完整的图像文件,而不是原始数据 block 。这是从数据 block 制作位图(它不是灰度图,但您可能会明白):

const int bytesPerPixel = 4;
int stride = bytesPerPixel * pixelsPerLine;
UInt32[] pixelBytes = new uint[lineCount * pixelsPerLine];

for (int y = 0; y < lineCount; y++)
{
int destinationLineStart = y * pixelsPerLine;
int sourceLineStart = y * pixelsPerLine;
for (int x = 0; x < pixelsPerLine; x++)
{
pixelBytes[x] = _rgbPixels[x].Pbgr32;
}
}
var bmp = BitmapSource.Create(pixelsPerLine, lineCount, 96, 96, PixelFormats.Pbgra32, null, pixelBytes, stride);
bmp.Freeze();
return bmp;

您已经完成了嵌套循环中的操作(生成字节数组),但我将其留在原处以便您可以看到在创建之前发生了什么

关于c# - byte[]转灰度BitmapImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3353860/

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