我想您会发现在您的代码中,流应该包含一个完整的图像文件,而不是一个原始数据块。下面是从一块数据中生成位图(虽然不是灰度图,但您可能会明白这一点):
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;
您已经在嵌套循环中完成了位(生成字节数组),但我将其保留在中,以便您可以看到在创建之前发生了什么