Ejemplo n.º 1
0
        public int SaveImage(string Path, string path)
        {
            DirectoryInfo d = new DirectoryInfo(Path);

            FileInfo[] files = d.GetFiles();
            int        i     = 1;



            foreach (FileInfo f in files)
            {
                string PathSave = HttpContext.Current.Server.MapPath("~/DicomImage/" + path);

                if (!Directory.Exists(PathSave))
                {
                    Directory.CreateDirectory(PathSave);
                }


                Class.File file = new Class.File();
                file.NameFile = f.Name;
                file.TypeFile = f.Extension;

                DicomDecoder dd = new DicomDecoder();
                dd.DicomFileName = f.FullName;
                double winCentre       = dd.windowCentre;
                double winWidth        = dd.windowWidth;
                int    samplesPerPixel = dd.samplesPerPixel;
                bool   signedImage     = dd.signedImage;
                int    minPixelValue   = 1;
                int    maxPixelValue   = 0;
                PathSave += i + ".png";


                TypeOfDicomFile typeOfDicomFile = dd.typeofDicomFile;

                int imageWidth  = dd.width;
                int imageHeight = dd.height;



                bmp = new Bitmap(imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

                dd.GetPixels8(ref pixels8);
                dd.GetPixels16(ref pixels16);
                dd.GetPixels24(ref pixels24);

                if (pixels8 != null)
                {
                    minPixelValue = pixels8.Min();
                    maxPixelValue = pixels8.Max();
                    // Bug fix dated 24 Aug 2013 - for proper window/level of signed images
                    // Thanks to Matias Montroull from Argentina for pointing this out.
                    if (dd.signedImage)
                    {
                        winCentre -= char.MinValue;
                    }

                    if (Math.Abs(winWidth) < 0.001)
                    {
                        winWidth = maxPixelValue - minPixelValue;
                    }

                    if ((winCentre == 0) ||
                        (minPixelValue > winCentre) || (maxPixelValue < winCentre))
                    {
                        winCentre = (maxPixelValue + minPixelValue) / 2;
                    }
                    ResetValues(winWidth, winCentre);
                    pix8 = pixels8;
                    ComputeLookUpTable8();

                    CreateImage8(imageWidth, imageHeight);
                }
                else if (pixels16 != null)
                {
                    minPixelValue = pixels16.Min();
                    maxPixelValue = pixels16.Max();

                    // Bug fix dated 24 Aug 2013 - for proper window/level of signed images
                    // Thanks to Matias Montroull from Argentina for pointing this out.
                    if (dd.signedImage)
                    {
                        winCentre -= short.MinValue;
                    }

                    if (Math.Abs(winWidth) < 0.001)
                    {
                        winWidth = maxPixelValue - minPixelValue;
                    }

                    if ((winCentre == 0) ||
                        (minPixelValue > winCentre) || (maxPixelValue < winCentre))
                    {
                        winCentre = (maxPixelValue + minPixelValue) / 2;
                    }

                    ResetValues(winWidth, winCentre);
                    pix16 = pixels16;
                    ComputeLookUpTable16();

                    CreateImage16(imageWidth, imageHeight);
                }
                else if (pixels24 != null)
                {
                    minPixelValue = pixels8.Min();
                    maxPixelValue = pixels8.Max();
                    // Bug fix dated 24 Aug 2013 - for proper window/level of signed images
                    // Thanks to Matias Montroull from Argentina for pointing this out.
                    if (dd.signedImage)
                    {
                        winCentre -= char.MinValue;
                    }

                    if (Math.Abs(winWidth) < 0.001)
                    {
                        winWidth = maxPixelValue - minPixelValue;
                    }

                    if ((winCentre == 0) ||
                        (minPixelValue > winCentre) || (maxPixelValue < winCentre))
                    {
                        winCentre = (maxPixelValue + minPixelValue) / 2;
                    }

                    ResetValues(winWidth, winCentre);
                    pix24 = pixels24;
                    ComputeLookUpTable8();

                    CreateImage24(imageWidth, imageHeight);
                }

                i++;
                if (bmp != null)
                {
                    bmp.Save(PathSave, ImageFormat.Png);
                }
            }

            return(i);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 更换显示图片
        /// </summary>
        /// <param name="filefullnamimage"></param>
        /// <returns></returns>
        public Bitmap fB_ChangeImage(string filefullnamimage)
        {
            dd.DicomFileName = filefullnamimage;

            TypeOfDicomFile typeOfDicomFile = dd.typeofDicomFile;

            if (typeOfDicomFile == TypeOfDicomFile.Dicom3File ||
                typeOfDicomFile == TypeOfDicomFile.DicomOldTypeFile)
            {
                imageWidth  = dd.width;
                imageHeight = dd.height;
                bitDepth    = dd.bitsAllocated;

                if (winCD == DBLNULL)
                {
                    winCD = dd.windowCentre;
                }
                if (winWD == DBLNULL)
                {
                    winWD = dd.windowWidth;
                }



                //if (winCentreD == DBLNULL) winCentreD = dd.windowCentre;
                //if (winWidthD == DBLNULL) winWidthD = dd.windowWidth;

                winCentreD = winCD;
                winWidthD  = winWD;


                samplesPerPixel = dd.samplesPerPixel;
                signedImage     = dd.signedImage;

                this.NewImage = true;

                if (samplesPerPixel == 1 && bitDepth == 8)
                {
                    pixels8.Clear();
                    pixels16.Clear();
                    pixels24.Clear();
                    dd.GetPixels8(ref pixels8);

                    minPixelValue = pixels8.Min();
                    maxPixelValue = pixels8.Max();

                    // Bug fix dated 24 Aug 2013 - for proper window/level of signed images
                    // Thanks to Matias Montroull from Argentina for pointing this out.
                    if (dd.signedImage)
                    {
                        winCentreD -= char.MinValue;
                    }

                    if (Math.Abs(winWidthD) < 0.001)
                    {
                        winWidthD = maxPixelValue - minPixelValue;
                    }

                    if ((winCentreD == 0) ||
                        (minPixelValue > winCentreD) || (maxPixelValue < winCentreD))
                    {
                        winCentreD = (maxPixelValue + minPixelValue) / 2;
                    }

                    SetParameters(ref pixels8, imageWidth, imageHeight,
                                  winWidthD, winCentreD, samplesPerPixel, true);
                }

                if (samplesPerPixel == 1 && bitDepth == 16)
                {
                    pixels16.Clear();
                    pixels8.Clear();
                    pixels24.Clear();
                    dd.GetPixels16(ref pixels16);

                    minPixelValue = pixels16.Min();
                    maxPixelValue = pixels16.Max();

                    // Bug fix dated 24 Aug 2013 - for proper window/level of signed images
                    // Thanks to Matias Montroull from Argentina for pointing this out.
                    if (dd.signedImage)
                    {
                        winCentreD -= short.MinValue;
                    }

                    if (Math.Abs(winWidthD) < 0.001)
                    {
                        winWidthD = maxPixelValue - minPixelValue;
                    }

                    if ((winCentreD == 0) ||
                        (minPixelValue > winCentreD) || (maxPixelValue < winCentreD))
                    {
                        winCentreD = (maxPixelValue + minPixelValue) / 2;
                    }

                    Signed16Image = dd.signedImage;

                    SetParameters(ref pixels16, imageWidth, imageHeight,
                                  winWidthD, winCentreD, true);
                }

                if (samplesPerPixel == 3 && bitDepth == 8)
                {
                    // This is an RGB colour image
                    pixels8.Clear();
                    pixels16.Clear();
                    pixels24.Clear();
                    dd.GetPixels24(ref pixels24);

                    SetParameters(ref pixels24, imageWidth, imageHeight,
                                  winWidthD, winCentreD, samplesPerPixel, true);
                }
            }
            else
            {
                if (typeOfDicomFile == TypeOfDicomFile.DicomUnknownTransferSyntax)
                {
                    //MessageBox.Show("Sorry, I can't read a DICOM file with this Transfer Syntax.",
                    //    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    //MessageBox.Show("Sorry, I can't open this file. " +
                    //    "This file does not appear to contain a DICOM image.",
                    //    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                //Text = "DICOM Image Viewer: ";
                // Show a plain grayscale image instead
                try
                {
                    pixels8.Clear();
                    pixels16.Clear();
                    pixels24.Clear();
                    samplesPerPixel = 1;

                    imageWidth  = this.Width - 25;  // 25 is a magic number
                    imageHeight = this.Height - 25; // Same magic number
                    int iNoPix = imageWidth * imageHeight;

                    for (int i = 0; i < iNoPix; ++i)
                    {
                        pixels8.Add(240);// 240 is the grayvalue corresponding to the Control colour
                    }
                    winWidthD  = 256;
                    winCentreD = 127;
                    this.SetParameters(ref pixels8, imageWidth, imageHeight,
                                       winWidthD, winCentreD, samplesPerPixel, true);
                }
                catch (Exception)
                { }

                //this.Invalidate();
            }

            //fv_BitMapCopy(ref btmtmp, bmp); //复制图片
            //fv_ImageWriter(ref btmtmp,ddlist.Count-1); //添加文字
            fv_ImageWriter(ref bmp);

            ComputeScrollBarParameters();//显示位置
            return(bmp);
        }
Ejemplo n.º 3
0
        private void ReadAndDisplayDicomFile(string fileName, string fileNameOnly)
        {
            dd.DicomFileName = fileName;

            TypeOfDicomFile typeOfDicomFile = dd.typeofDicomFile;

            if (typeOfDicomFile == TypeOfDicomFile.Dicom3File ||
                typeOfDicomFile == TypeOfDicomFile.DicomOldTypeFile)
            {
                imageWidth      = dd.width;
                imageHeight     = dd.height;
                bitDepth        = dd.bitsAllocated;
                winCentre       = dd.windowCentre;
                winWidth        = dd.windowWidth;
                samplesPerPixel = dd.samplesPerPixel;
                signedImage     = dd.signedImage;

                label1.Visible    = true;
                label2.Visible    = true;
                label3.Visible    = true;
                label4.Visible    = true;
                bnSave.Enabled    = true;
                bnTags.Enabled    = true;
                bnResetWL.Enabled = true;
                label2.Text       = imageWidth.ToString() + " X " + imageHeight.ToString();
                if (samplesPerPixel == 1)
                {
                    label4.Text = bitDepth.ToString() + " bit";
                }
                else
                {
                    label4.Text = bitDepth.ToString() + " bit, " + samplesPerPixel +
                                  " samples per pixel";
                }

                imagePanelControl.NewImage = true;
                Text = "DICOM Image Viewer: " + fileNameOnly;

                if (samplesPerPixel == 1 && bitDepth == 8)
                {
                    pixels8.Clear();
                    pixels16.Clear();
                    pixels24.Clear();
                    dd.GetPixels8(ref pixels8);

                    // This is primarily for debugging purposes,
                    //  to view the pixel values as ascii data.
                    //if (true)
                    //{
                    //    System.IO.StreamWriter file = new System.IO.StreamWriter(
                    //               "C:\\imageSigned.txt");

                    //    for (int ik = 0; ik < pixels8.Count; ++ik)
                    //        file.Write(pixels8[ik] + "  ");

                    //    file.Close();
                    //}

                    minPixelValue = pixels8.Min();
                    maxPixelValue = pixels8.Max();

                    // Bug fix dated 24 Aug 2013 - for proper window/level of signed images
                    // Thanks to Matias Montroull from Argentina for pointing this out.
                    if (dd.signedImage)
                    {
                        winCentre -= char.MinValue;
                    }

                    if (Math.Abs(winWidth) < 0.001)
                    {
                        winWidth = maxPixelValue - minPixelValue;
                    }

                    if ((winCentre == 0) ||
                        (minPixelValue > winCentre) || (maxPixelValue < winCentre))
                    {
                        winCentre = (maxPixelValue + minPixelValue) / 2;
                    }

                    imagePanelControl.SetParameters(ref pixels8, imageWidth, imageHeight,
                                                    winWidth, winCentre, samplesPerPixel, true, this);
                }

                if (samplesPerPixel == 1 && bitDepth == 16)
                {
                    pixels16.Clear();
                    pixels8.Clear();
                    pixels24.Clear();
                    dd.GetPixels16(ref pixels16);

                    // This is primarily for debugging purposes,
                    //  to view the pixel values as ascii data.
                    //if (true)
                    //{
                    //    System.IO.StreamWriter file = new System.IO.StreamWriter(
                    //               "C:\\imageSigned.txt");

                    //    for (int ik = 0; ik < pixels16.Count; ++ik)
                    //        file.Write(pixels16[ik] + "  ");

                    //    file.Close();
                    //}

                    minPixelValue = pixels16.Min();
                    maxPixelValue = pixels16.Max();

                    // Bug fix dated 24 Aug 2013 - for proper window/level of signed images
                    // Thanks to Matias Montroull from Argentina for pointing this out.
                    if (dd.signedImage)
                    {
                        winCentre -= short.MinValue;
                    }

                    if (Math.Abs(winWidth) < 0.001)
                    {
                        winWidth = maxPixelValue - minPixelValue;
                    }

                    if ((winCentre == 0) ||
                        (minPixelValue > winCentre) || (maxPixelValue < winCentre))
                    {
                        winCentre = (maxPixelValue + minPixelValue) / 2;
                    }

                    imagePanelControl.Signed16Image = dd.signedImage;

                    imagePanelControl.SetParameters(ref pixels16, imageWidth, imageHeight,
                                                    winWidth, winCentre, true, this);
                }

                if (samplesPerPixel == 3 && bitDepth == 8)
                {
                    // This is an RGB colour image
                    pixels8.Clear();
                    pixels16.Clear();
                    pixels24.Clear();
                    dd.GetPixels24(ref pixels24);

                    // This code segment is primarily for debugging purposes,
                    //    to view the pixel values as ascii data.
                    //if (true)
                    //{
                    //    System.IO.StreamWriter file = new System.IO.StreamWriter(
                    //                      "C:\\image24.txt");

                    //    for (int ik = 0; ik < pixels24.Count; ++ik)
                    //        file.Write(pixels24[ik] + "  ");

                    //    file.Close();
                    //}

                    imagePanelControl.SetParameters(ref pixels24, imageWidth, imageHeight,
                                                    winWidth, winCentre, samplesPerPixel, true, this);
                }
            }
            else
            {
                if (typeOfDicomFile == TypeOfDicomFile.DicomUnknownTransferSyntax)
                {
                    MessageBox.Show("Sorry, I can't read a DICOM file with this Transfer Syntax.",
                                    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("Sorry, I can't open this file. " +
                                    "This file does not appear to contain a DICOM image.",
                                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                Text = "DICOM Image Viewer: ";
                // Show a plain grayscale image instead
                pixels8.Clear();
                pixels16.Clear();
                pixels24.Clear();
                samplesPerPixel = 1;

                imageWidth  = imagePanelControl.Width - 25;  // 25 is a magic number
                imageHeight = imagePanelControl.Height - 25; // Same magic number
                int iNoPix = imageWidth * imageHeight;

                for (int i = 0; i < iNoPix; ++i)
                {
                    pixels8.Add(240);// 240 is the grayvalue corresponding to the Control colour
                }
                winWidth  = 256;
                winCentre = 127;
                imagePanelControl.SetParameters(ref pixels8, imageWidth, imageHeight,
                                                winWidth, winCentre, samplesPerPixel, true, this);
                imagePanelControl.Invalidate();
                label1.Visible    = false;
                label2.Visible    = false;
                label3.Visible    = false;
                label4.Visible    = false;
                bnSave.Enabled    = false;
                bnTags.Enabled    = false;
                bnResetWL.Enabled = false;
            }
        }