简体   繁体   English

C#将BMP列表转换为DICOM文件数组

[英]c# converting list of BMP to array of DICOM files

I can convert bitmap to DICOM using the following: 我可以使用以下方法将位图转换为DICOM:

  Bitmap bmp = new Bitmap(System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/FileName)));
                Color c = bmp.GetPixel(0, 0);
                bmp.MakeTransparent(c);
                im.Import(bmp);

It works great. 效果很好。

Now I am trying to convert a list of bitmap images to a list of DICOM using: 现在,我尝试使用以下方法将位图图像列表转换为DICOM列表:

    MySession.Current.dicomArray = new DicomImage[NFiles];
      MySession.Current.bmpArray = new Bitmap[NFiles];
.....
 for (int i = 0; i < NFiles; ++i)
                                {
                                    MySession.Current.bmpArray[i] =
                                        new Bitmap(System.Drawing.Image.FromFile(
                                            System.Web.HttpContext.Current.Server.MapPath(
                                                "~/" + ImagePath + files[i])));
                                }
    ......
          for (int i = 0; i < NFiles; ++i)
         {
           MySession.Current.dicomArray[i].Import(MySession.Current.bmpArray[i]);
        }

I get the following error: 我收到以下错误:

Object reference not set to an instance of an object.

I can see all files in the bmpArray. 我可以看到bmpArray中的所有文件。 I guess I am using for statement wrong. 我想我的陈述是错误的。 I would appreciate your suggestions. 非常感谢您的建议。

As pointed out by @AdilMammadov in the comments above, the problem is due to the fact that you have not yet defined the individual members of the dicomArray array, so when you are calling the Import method in the second loop, you are invoking it on a null object. 正如@AdilMammadov在上面的注释中指出的那样,问题是由于您尚未定义dicomArray数组的各个成员,因此,当您在第二个循环中调用Import方法时,您将对其进行调用null对象。

If DicomImage has a public constructor (it is not clear from your question if it does), either add the line that Adil suggests: 如果DicomImage具有公共构造函数(不清楚是否存在),请添加Adil建议的行:

MySession.Current.dicomArray[i] = new DicomImage();

before the Import line in the second loop. 在第二个循环中的“ Import行之前。

Alternatively, use the LINQ method Enumerable.Repeat to create the array: 或者,使用LINQ方法Enumerable.Repeat创建数组:

MySession.Current.dicomArray = Enumerable.Repeat(new DicomImage(), NFiles).ToArray();

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

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