字节[]为灰度的BitmapImage [英] byte[] to gray scale BitmapImage
问题描述
我开始了一个128×128阵列双打,并把它转换成与每个双比例值字节的一维数组。
I start out with a 128 x 128 array of doubles and turn that into a 1D array of bytes with proportional values for each double.
然后我把这个数组字节并把它变成一个内存流(数据流
以下),并尝试并将它放入一个的BitmapImage
,如下所示:
I then take this array of bytes and turn it into a memory stream (dataStream
below) and try and put that into a BitmapImage
like so:
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
这不会产生预期的图像(我只是得到一个白色方块)。
This doesn't produce the expected image (I just get a white square).
我应该如何去这件事?
推荐答案
我觉得你会发现,在你的代码,流应该包含一个完整的图像文件,数据不是一个原始块。下面是从数据块制作的位图(它不是灰度图,但你可能会得到的想法):
I think you'll find that in your code, the stream should contain a complete image file, not a raw block of data. Here's making a Bitmap from a block of data (it's not greyscale, but you might get the idea):
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;
您已经做了位在嵌套循环(使字节数组),但我离开它所以你可以看到之前创建
You've already done the bit in the nested loops (making the byte array), but I left it in so you can see what comes before the Create
这篇关于字节[]为灰度的BitmapImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!