// using System; // using System.Collections.Generic; // using System.Drawing; // using System.Drawing.Drawing2D; // using System.Drawing.Imaging; // using System.Linq; // // namespace DICOMCoverter // { // public enum ImageBitsPerPixel { Eight, Sixteen, TwentyFour }; // // internal class ImageControl // { // private List pix8; // private List pix16; // private List pix24; // private Bitmap bmp; // // // For Window Level // private int winMin; // // private int winMax; // private int winCentre; // private int winWidth; // // private byte[] lut8; // private byte[] lut16; // // //====================== // private List pixels8; // // private List pixels16; // private List pixels24; // 30 July 2010 // // /// // /// 每一个像素的取样数,一般来说,CT,MR,DR等灰度图像都是1,而彩超等彩**图像都是3,分别表示R, G, B三个颜色通道。 // /// // // int samplesPerPixel; // Updated 30 July 2010 // private double windowsCentre; // // private double windowsWidth; // private int maxPixelValue; // Updated July 2012 // private int minPixelValue; // //===================== // // public ImageControl() // { // pix8 = new List(); // pix16 = new List(); // pix24 = new List(); // winMin = 0; // winMax = 65535; // lut8 = new byte[256]; // lut16 = new byte[65536]; // //========================== // pixels8 = new List(); // pixels16 = new List(); // pixels24 = new List(); // maxPixelValue = 0; // minPixelValue = 65535; // //=========================== // } // // /// // /// 生成8位深度图 // /// // /// // public void CreateImage8depth(DicomDecoder dd) // { // if (dd.samplesPerPixel == 1 && dd.bitsAllocated == 8) // { // windowsCentre = dd.windowCentre; // windowsWidth = dd.windowWidth; // pixels8.Clear(); // pixels16.Clear(); // pixels24.Clear(); // dd.GetPixels8(ref pixels8); // // minPixelValue = pixels8.Min(); // maxPixelValue = pixels8.Max(); // // if (dd.signedImage) // { // windowsCentre -= char.MinValue; // } // // if (Math.Abs(windowsWidth) < 0.001) // { // windowsWidth = maxPixelValue - minPixelValue; // } // // if ((windowsCentre == 0) || // (minPixelValue > windowsCentre) || (maxPixelValue < windowsCentre)) // { // windowsCentre = (maxPixelValue + minPixelValue) / 2; // } // // winWidth = Convert.ToInt32(windowsWidth); // winCentre = Convert.ToInt32(windowsCentre); // // pix8 = pixels8; // if (bmp != null) // bmp.Dispose(); // ResetValues(); // ComputeLookUpTable8(); // bmp = new Bitmap(dd.width, dd.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // // CreateImage8(dd.width, dd.height); // } // // if (dd.samplesPerPixel == 3 && dd.bitsAllocated == 8) // { // windowsCentre = dd.windowCentre; // windowsWidth = dd.windowWidth; // winWidth = Convert.ToInt32(windowsWidth); // winCentre = Convert.ToInt32(windowsCentre); // pixels8.Clear(); // pixels16.Clear(); // pixels24.Clear(); // dd.GetPixels8(ref pixels8); // pix24 = pixels8; // // if (bmp != null) // bmp.Dispose(); // ResetValues(); // ComputeLookUpTable8(); // bmp = new Bitmap(dd.width, dd.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // CreateImage24(dd.width, dd.height); // } // } // // /// // /// 生成16位深度的图 // /// // /// // public void CreateImage16Depth(DicomDecoder dd) // { // windowsWidth = dd.windowWidth; // windowsCentre = dd.windowCentre; // pixels16.Clear(); // pixels8.Clear(); // pixels24.Clear(); // dd.GetPixels16(ref pixels16); // // minPixelValue = pixels16.Min(); // maxPixelValue = pixels16.Max(); // // if (dd.signedImage) // { // windowsCentre -= short.MinValue; // } // // if (Math.Abs(windowsWidth) < 0.001) // { // windowsWidth = maxPixelValue - minPixelValue; // } // // if ((windowsCentre == 0) || // (minPixelValue > windowsCentre) || (maxPixelValue < windowsCentre)) // { // windowsCentre = (maxPixelValue + minPixelValue) / 2; // } // // winWidth = Convert.ToInt32(windowsWidth); // winCentre = Convert.ToInt32(windowsCentre); // // pix16 = pixels16; // if (bmp != null) // bmp.Dispose(); // ResetValues(); // ComputeLookUpTable16(); // bmp = new Bitmap(dd.width, dd.height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); // CreateImage16(dd.width, dd.height); // } // // // Create a bitmap on the fly, using 8-bit grayscale pixel data // private void CreateImage8(int imgWidth, int imgHeight) // { // BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), // System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat); // // unsafe // { // int pixelSize = 3; // int i, j, j1; // byte b; // // for (i = 0; i < bmd.Height; ++i) // { // byte* row = (byte*)bmd.Scan0 + (i * bmd.Stride); // // for (j = 0; j < bmd.Width; ++j) // { // b = lut8[pix8[i * bmd.Width + j]]; // j1 = j * pixelSize; // row[j1] = b; // Red // row[j1 + 1] = b; // Green // row[j1 + 2] = b; // Blue // } // } // } // // bmp.UnlockBits(bmd); // // bmp = KiResizeImage(bmp, 400, 400); // SaveImage(bmp, DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.Millisecond); // } // // // Create a bitmap on the fly, using 24-bit RGB pixel data // private void CreateImage24(int imgWidth, int imgHeight) // { // { // int numBytes = imgWidth * imgHeight * 3; // int j; // int i, i1; // // BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, // bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); // // int width3 = bmd.Width * 3; // // unsafe // { // for (i = 0; i < bmd.Height; ++i) // { // byte* row = (byte*)bmd.Scan0 + (i * bmd.Stride); // i1 = i * bmd.Width * 3; // // for (j = 0; j < width3; j += 3) // { // // Windows uses little-endian, so the RGB data is // // actually stored as BGR // row[j + 2] = lut8[pix24[i1 + j]]; // Blue // row[j + 1] = lut8[pix24[i1 + j + 1]]; // Green // row[j] = lut8[pix24[i1 + j + 2]]; // Red // } // } // } // bmp.UnlockBits(bmd); // } // } // // // Create a bitmap on the fly, using 16-bit grayscale pixel data // private void CreateImage16(int imgWidth, int imgHeight) // { // BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, imgWidth, imgHeight), // System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed); // // unsafe // { // int pixelSize = 3; // int i, j, j1 = 0; // byte b; // // for (i = 0; i < bmd.Height; ++i) // { // //取到每行的总长度 // byte* row = (byte*)bmd.Scan0 + (i * bmd.Stride); // // //i1 = i * bmd.Width; // // for (j = 0; j < bmd.Width; ++j) // { // //取色值 // b = lut16[pix16[i * bmd.Width + j]];//i*width+j当前行的第j个像素点 // j1 = j * pixelSize; // row[j1] = b; // Red // row[j1 + 1] = b; // Green // row[j1 + 2] = b; // Blue // } // } // } // bmp.UnlockBits(bmd); // //指定压缩后的图片大小 // bmp = KiResizeImage(bmp, imgWidth, imgHeight); // SaveImage(bmp, DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.Millisecond); // //bmp.Save(DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.Millisecond + ".jpg"); // } // // /// // /// 压缩图片大小 // /// // /// // /// // /// // /// // /// // public Bitmap KiResizeImage(Bitmap bmp, int newW, int newH) // { // try // { // Bitmap b = new Bitmap(newW, newH); // Graphics g = Graphics.FromImage(b); // // 插值算法的质量 // g.InterpolationMode = InterpolationMode.Low; // g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); // g.Dispose(); // return b; // } // catch (Exception ex) // { // return null; // } // } // // // Method to save an image as PNG. The image is saved as per the current window/level values. // public void SaveImage(Bitmap bmp, String fileName) // { // if (bmp != null && !string.IsNullOrWhiteSpace(fileName)) // bmp.Save(fileName + ".jpg"); // } // // // We use the linear interpolation method here // // Nonlinear methods like sigmoid are also common, but we don't do them here. // private void ComputeLookUpTable8() // { // if (winMax == 0) // winMax = 255; // // int range = winMax - winMin; // if (range < 1) range = 1; // double factor = 255.0 / range; // // for (int i = 0; i < 256; ++i) // { // if (i <= winMin) // lut8[i] = 0; // else if (i >= winMax) // lut8[i] = 255; // else // { // lut8[i] = (byte)((i - winMin) * factor); // } // } // } // // // Linear interpolation here too // private void ComputeLookUpTable16() // { // int range = winMax - winMin; // if (range < 1) range = 1; // double factor = 255.0 / range; // int i; // // for (i = 0; i < 65536; ++i) // { // if (i <= winMin) // lut16[i] = 0; // else if (i >= winMax) // lut16[i] = 255; // else // { // lut16[i] = (byte)((i - winMin) * factor); // } // } // } // // // Restore original window/level values // public void ResetValues() // { // winMax = Convert.ToInt32(winCentre + 0.5 * winWidth); // winMin = winMax - winWidth; // } // } // }