簡體   English   中英

在圖片框中渲染16位圖像

[英]Render 16 bits image in picturebox

我有一個數組,其中包含從Dicom圖像中提取的PixelData。

這是代碼:

        byte[] bytes = img.PixelData.GetFrame(0).Data; // img is the Dicom Image
        int count = bytes.Length / 2;
        ushort[] words = new ushort[count];
        for (int i = 0, p = 0; i < count; i++, p += 2)
        {
            words[i] = BitConverter.ToUInt16(bytes, p);
        }
        pixels16 = words.ToList(); //pixels16 contains now the PixelData for the Grayscale image

現在,這是我的問題,如何將其渲染到Picturebox中?

我的代碼將位圖從Format16bppGrayScale轉換為Format8bppIndexed格式。 PictureBox可以輕松顯示此格式。 (如果需要,可以使用其他調色板)。

public Bitmap Gray16To8bppIndexed(Bitmap BmpIn)
{
    if (BmpIn.PixelFormat != PixelFormat.Format16bppGrayScale)
        throw new BadImageFormatException();

    byte[] ImageData = new byte[BmpIn.Width * BmpIn.Height * 2];
    Rectangle Re = new Rectangle(0, 0, BmpIn.Width, BmpIn.Height);

    BitmapData BmpData = BmpIn.LockBits(Re, ImageLockMode.ReadOnly, BmpIn.PixelFormat);
    Marshal.Copy(BmpData.Scan0, ImageData, 0, ImageData.Length);
    BmpIn.UnlockBits(BmpData);

    byte[] ImageData2 = new byte[BmpIn.Width * BmpIn.Height];
    for (long i = 0; i < ImageData2.LongLength; i++)
        ImageData2[i] = ImageData[i * 2 + 1];
    ImageData = null;

    Bitmap BmpOut = new Bitmap(BmpIn.Width, BmpIn.Height, PixelFormat.Format8bppIndexed);
    BmpData = BmpOut.LockBits(Re, ImageLockMode.WriteOnly, BmpOut.PixelFormat);
    Marshal.Copy(ImageData2, 0, BmpData.Scan0, ImageData2.Length);
    BmpOut.UnlockBits(BmpData);
    ImageData2 = null;
    BmpData = null;

    ColorPalette GrayPalette = BmpOut.Palette;
    Color[] GrayColors = GrayPalette.Entries;
    for (int i = 0; i < GrayColors.Length; i++)
        GrayColors[GrayColors.Length - 1 - i] = Color.FromArgb(i, i, i);
    BmpOut.Palette = GrayPalette;

    return BmpOut;
}

好吧,我不知道具體細節,因為這取決於您真正想怎么做(如果性能很重要,則需要創建自己的Bitmap子類,否則,Bitmap.SetPixel可以正常工作)。

但實際上,您需要將這些像素推入位圖,然后將圖片框的圖像設置為該位圖,例如:

Bitmap bitmap = new Bitmap(width, height);

for(int y = 0;y < height;y++)
   for(int x = 0;x < width;x++)
       bitmap.SetPixel(x,y, Color.fromRGB(/* unpack your R,G,B channel of your pixel here */);

pictureBox.Image = bitmap;

您可以利用AForge .NET Framework,它是用於圖像處理的出色.NET庫。 內置的.NET Picturebox無法使用System.Drawing.Imaging.PixelFormat.Format16bppGrayScale來自然顯示圖像,但是AForge庫具有其自己的Picturebox控件,請檢查一下。 需要一個.NET映像。

您可以使用NuGet輕松將AForge包括到您的項目中:

Install-Package AForge.Controls
Install-Package AForge.Imaging

要不就

Install-Package AForge

下面的示例代碼:

//SOME BYTES
//Load here the DICOM image
int width=640, height=480;
int numberOfPixels = width*height;
byte[] source = new byte[2*numberOfPixels];

//With AFORGE
var image = AForge.Imaging.UnmanagedImage.Create(width, height, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
IntPtr ptrToImage = image.ImageData;
//Copies the bytes from source to the image
//System.Runtime.InteropServices
Marshal.Copy(source, 0, ptrToImage,numberOfPixels);

//WITH .NET
System.Drawing.Bitmap bitmapImage = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
var imageData = bitmapImage.LockBits(new System.Drawing.Rectangle(0, 0, width, height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
Marshal.Copy(source, 0, imageData.Scan0, numberOfPixels);
bitmapImage.UnlockBits(imageData);   

從朋友那里得到了這個主意。 inputImage.ImageSource屬性是具有灰度像素值的2D數組。

Bitmap grayscaleImage = new Bitmap(inputImage.ImageSource);
            for (int x = 0; x < grayscaleImage.Width; x++)
            {
                for (int y = 0; y < grayscaleImage.Height; y++)
                {
                    byte[,] tempMatrix = inputImage.ImageGrayscale;
                    byte temp = tempMatrix[x, y];
                    Color tempColor = Color.FromArgb(255, temp, temp, temp);
                    grayscaleImage.SetPixel(x, y, tempColor);
                }
            }
            picboxDisplay.Image = grayscaleImage;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM