我从 128 x 128 双精度数组开始,然后将其转换为每个双精度值具有比例值的一维字节数组。
然后我获取这个字节数组并将其转换为内存流(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;
Here imgScan
is a System.Windows.Controls.Image
这不会产生预期的图像(我只得到一个白色方块)。
我该怎么办?
我想您会发现在您的代码中,流应该包含完整的图像文件,而不是原始数据块。这是从数据块创建位图(它不是灰度,但您可能会明白):
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;
您已经完成了嵌套循环中的部分(创建字节数组),但我将其保留在其中,以便您可以看到 Create 之前发生的情况
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)