Question

I can convert bitmap to DICOM using the following:

  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:

    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. I guess I am using for statement wrong. I would appreciate your suggestions.

Was it helpful?

Solution

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.

If DicomImage has a public constructor (it is not clear from your question if it does), either add the line that Adil suggests:

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

before the Import line in the second loop.

Alternatively, use the LINQ method Enumerable.Repeat to create the array:

MySession.Current.dicomArray = Enumerable.Repeat(new DicomImage(), NFiles).ToArray();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top