我想在 metro 应用程序中从像素字节数组创建位图。下面的函数用于相同的早期:
//Here create the Bitmap to the know height, width and format
Bitmap bmp = new Bitmap( 352, 288, PixelFormat.Format24bppRgb);
//Create a BitmapData and Lock all pixels to be written
BitmapData bmpData = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.WriteOnly, bmp.PixelFormat);
//Copy the data from the byte array into BitmapData.Scan0
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
//Unlock the pixels
bmp.UnlockBits(bmpData);
//Return the bitmap
return bmp;
但 BitmapData 类现在不存在于 Windows 8 中。请提出任何替代方法。
谢谢, 潘卡吉
最佳答案
如果我没有完全弄错的话,上面的部分代码摘自 Evil DICOM图书馆,来自 ImageHelper.GetBitmap
方法。
我自己尝试将这段代码移植到Metro(或者微软最终会调用它的任何名称),这就是ImageHelper.GetBitmap
的Metro 类似物。看起来像在我的港口:
public static BitmapSource GetBitmap(float[] pixels, ImageProperties properties)
{
var bmp = new WriteableBitmap(properties.Rows, properties.Columns);
using (var stream = bmp.PixelBuffer.AsStream())
{
var bytes = new byte[4 * pixels.Length];
for (int i = 0; i < pixels.Length; i++)
{
var greyness = properties.WindowAndLevel.GetValue(pixels[i]);
bytes[4 * i] = greyness;
bytes[4 * i + 1] = greyness;
bytes[4 * i + 2] = greyness;
bytes[4 * i + 3] = 0xff;
}
stream.Write(bytes, 0, bytes.Length);
}
return bmp;
}
(实际上,我在自己的代码中使用了 stream.WriteAsync
,要求将方法声明为 async
。但是,这与上述问题无关。)
这个答案与 Diogo 的相似,但如果您还没有“达到目标”,希望这个答案可以引导您更直接地实现目标。
更新
我现在已经将我的 Evil Dicom 端口发布到 Github 上的 Metro。您可以找到存储库 here ,这里也是一个非常简单的 Metro 应用程序的显示,我在其中使用了这个 Evil Dicom Metro 端口:
关于c# - Metro 应用程序中像素字节数组的位图(win 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685776/