简体   繁体   English

Metro应用程序中像素字节进位的位图(Win 8)

[英]bitmap from byte arry of pixels in metro app(win 8)

I want to create a bitmap from a byte array of pixel in metro Apps. 我想从Metro Apps中的像素字节数组创建位图。 Earlier below function was used for the same: 以下较早的功能用于相同的功能:

//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;

but BitmapData class is not present in windows 8 now. 但是Windows 8中现在不存在BitmapData类。 Please suggest any alternate way for the same. 请提出其他替代方法。

thanks, Pankaj 谢谢,Pankaj

If I am not entirely mistaken, part of the code above is taken from the Evil DICOM library, from the ImageHelper.GetBitmap method. 如果我没有完全弄错,则上面的部分代码来自Evil DICOM库的ImageHelper.GetBitmap方法。

I have myself made an attempt to port this code to Metro (or whatever Microsoft is going to call it eventually), and here is what the Metro analogue of ImageHelper.GetBitmap looks like in my port: 我本人试图将该代码移植到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;
}

(Actually, I am using stream.WriteAsync in my own code, requiring the method to be declared as async . However, this is irrelevant for the above question as such.) (实际上,我在自己的代码中使用stream.WriteAsync ,要求将方法声明为async 。但是,这与上述问题无关。)

This answer is similar to Diogo's, but if you are not already "there" hopefully this answer can lead you more directly to your goal. 这个答案类似于Diogo的答案,但是如果您还没有“存在”的希望,那么这个答案可以使您更直接地实现自己的目标。

UPDATE UPDATE

I have now published my Evil Dicom port to Metro on Github. 我现在已经在Github上将我的Evil Dicom端口发布到Metro了。 You can find the repository here , and here is also a display from a very simple Metro application where I have used this Evil Dicom Metro port: 您可以在此处找到存储库,这也是一个非常简单的Metro应用程序的显示,我在其中使用了Evil Dicom Metro端口:

Metro DICOM查看器

Try using the WritableBitmap class. 尝试使用WritableBitmap类。

Something like that should work: 这样的事情应该起作用:

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

stream.Seek(0, 0);
stream.Write(bytes, 0, bytes.Length);
newBmp.Invalidate();

You'll probably have to use the following namespaces for that to work: 您可能必须使用以下名称空间才能起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM