C# metro应用程序中像素字节数组的位图(win 8)

C# metro应用程序中像素字节数组的位图(win 8),c#,.net,microsoft-metro,dicom,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 bmpDat

我想从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;
但现在windows 8中不存在BitmapData类。请为同样的问题建议任何替代方法

谢谢,
Pankaj

尝试使用WritableBitmap类

类似的方法应该会奏效:

WriteableBitmap newBmp = new WriteableBitmap(width, height);
Stream stream = newBmp.PixelBuffer.AsStream();

stream.Seek(0, 0);
stream.Write(bytes, 0, bytes.Length);
newBmp.Invalidate();
您可能必须使用以下名称空间才能工作:

using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;

如果我没有完全弄错的话,上面的部分代码取自库中的
ImageHelper.GetBitmap
方法

我自己曾尝试将此代码移植到Metro(或微软最终将称之为什么),下面是Metro模拟的
ImageHelper.GetBitmap
在我的端口中的样子:

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;
}
公共静态位图源GetBitmap(浮点[]像素,图像属性)
{
var bmp=new WriteableBitmap(properties.Rows,properties.Columns);
使用(var stream=bmp.PixelBuffer.AsStream())
{
var bytes=新字节[4*pixels.Length];
对于(int i=0;i
(实际上,我在自己的代码中使用了
stream.WriteAsync
,要求将该方法声明为
async
。但是,这与上述问题本身无关。)

这个答案类似于迪奥戈的,但如果你还没有“在那里”,希望这个答案能引导你更直接地达到你的目标

更新

我现在已经在Github上将我邪恶的Dicom端口发布到Metro。您可以找到存储库,这里还显示了一个非常简单的Metro应用程序,其中我使用了这个邪恶的Dicom Metro端口:


谢谢!!但是这个我已经试过了。它不起作用。关于细节,我想补充一点,我从dicom图像的解析中得到了这个像素阵列。它是否引发了异常,或者图像被扭曲了?如果是第一次,发生了什么样的异常?还有,你用的是哪一个巨像空间?嗨,迪奥戈,实际上在这之后,我试图在图像控制中显示这个可写位图,但它什么也不显示。我还试图保存这张图片,并为此编写了下面的代码。使用此代码,我看到一个扭曲的图像保存在文件夹中。它在图像控件中没有显示任何内容,但当我使用以下代码保存文件时,它会保存扭曲的图像irandomaccesstream writeStream=wait sampleFile.OpenAsync(FileAccessMode.ReadWrite);BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId,writeStream);编码器.SetPixelData(BitmapPixelFormat.Rgba8,BitmapAlphaMode.Premultiplied,(uint)writeableBitmap.PixelWidth,(uint)writeableBitmap.PixelHeight,96,96,StreamBuffer);是颜色失真,还是图像大小失真?在这两种情况下,请记住bytearray都使用bgra颜色空间。如果使用rgba进行处理,只需更改顺序即可。如果图像是灰度的,我不确定WriteableBitmap类是如何工作的,但也许你必须复制两次灰度值,并为Alpha通道使用一个固定的值。嗨,Anders,我尝试过使用lib,它工作得非常好。你能告诉我如何从这个库中获取DCM文件的标签吗?邪恶DICOM的原始作者Rex Cardan目前正在对库进行全面的检修。您可以按照“正在进行的工作”进行操作。由于这次大修,“旧”网站即将退役,但你仍然可以找到一些关于旧图书馆的入门信息(这也适用于我的Metro改编)。顺便说一句,@Pankaj,如果我的回答回答回答了你的问题,请不要忘了。我的公共静态浮点[]GetPixels功能正在崩溃如果我尝试一个接一个地打开两个dcm图像,imagehelper类的(PixelData数据,ImageProperties属性)。行if(data.Format==FrameDataFormat.NATIVE)崩溃说数据是空的。有时它不会崩溃,但很少像这样发生。@ PANKAJ请在CuroS/邪恶DICOM上发布这些问题。并且通过点击答案左上方的V按钮来考虑我对原始问题的答案。