简体   繁体   English

C#如何在图片框中显示图像

[英]C# how to show image in picturebox

I'm trying to display dicom image using openDicom.net. 我正在尝试使用openDicom.net显示dicom图像。 What should i correct here? 我在这里应该纠正什么?

openDicom.Image.PixelData obraz = new openDicom.Image.PixelData(file.DataSet);
// System.Drawing.Bitmap obrazek = (Bitmap)Bitmap.FromFile(element);
pictureBox1.Image = obraz;
pictureBox1.Show();

PixelData is not an image. PixelData不是图像。 PixelData is raw image information. PixelData是原始图像信息。 In my experience, most DICOM files will be using jpeg2000 images. 以我的经验,大多数DICOM文件将使用jpeg2000图像。 In order to convert them to something usable by a PictureBox, you'll need to convert it to an Image. 为了将它们转换为PictureBox可用的内容,您需要将其转换为Image。 For raw monochrome types, you can make it into a System.Drawing.Bitmap using the following conversion: 对于原始单色类型,可以使用以下转换将其转换为System.Drawing.Bitmap:

openDicom.Image.PixelData obraz = new openDicom.Image.PixelData(file.DataSet);

Bitmap img = new System.Drawing.Bitmap(obraz.Columns, obraz.Rows, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

int resampleval = (int)Math.Pow(2, (obraz.BitsAllocated - obraz.BitsStored));
int pxCount = 0;
int temp = 0;

try
{
    unsafe
    {
        BitmapData bd = img.LockBits(new Rectangle(0, 0, obraz.Columns, obraz.Rows), ImageLockMode.WriteOnly, img.PixelFormat);

        for (int r = 0; r < bd.Height; r++)
        {
            byte* row = (byte*)bd.Scan0 + (r * bd.Stride);

            for (int c = 0; c < bd.Width; c++)
            {
                temp = PixelData16[pxCount] / resampleval;

                while (temp > 255)
                    temp = temp / resampleval;

                row[(c * 3)] = (byte)temp;
                row[(c * 3) + 1] = (byte)temp;
                row[(c * 3) + 2] = (byte)temp;

                pxCount++;
            }
        }

        img.UnlockBits(bd);
    }
}
catch
{
    img = new Bitmap(10, 10);
}

pictureBox1.Image = img;
pictureBox1.Show();

For other image types, you'll need to do a similar conversion with the appropriate values. 对于其他图像类型,您需要使用适当的值进行类似的转换。 This conversion is strictly for monochrome types, and only after they have been converted from jpeg2000 to jpeg. 此转换仅适用于单色类型,并且仅在将它们从jpeg2000转换为jpeg之后。 Performing this operation on a jpeg2000 image will give you exactly half of the image filled with static and the other half completely empty. 在jpeg2000图像上执行此操作将使您完全将一半的图像填充为静态图像,而另一半则完全空白。

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

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