c# - Metro 应用程序中像素字节数组的位图(win 8)

标签 c# .net microsoft-metro dicom

我想在 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 端口:

Metro DICOM Viewer

关于c# - Metro 应用程序中像素字节数组的位图(win 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685776/

相关文章:

c# - 是否可以使用面向方面的方法通过 Autofac 和 DynamicProxy 登录 Azure 函数

c# - ASP.NET MVC2 生成的 Javascript 包含逗号,该逗号会破坏 Internet Explorer 中的脚本、内部代码

c# - 比较两个连续的行 - 分组依据

c# - 如何将大型多维数组分段写入 HDF5 文件?

c# - 后台下载器找不到文件异常

javascript - WinRT Metro 风格的 HTML5/JavaScript 应用程序是如何打包和保护的

c# - 您所见过的最好的WPF开源项目

c# - ushort 数组到 Image 对象

.net - ListBox 、ScrollViewer 和 CanContentScroll

c# - Windows 商店框架导航 : How to get the source page?