我正在使用mDCM和C#來查看dicom標籤,但我試圖將像素數據轉換爲位圖並最終轉換爲JPG文件。我已閱讀了mDCM Google Group上有關該主題的所有帖子,並且所有代碼示例都不起作用或缺少重要的代碼行。我正在使用的圖像是一個16位monochrome1(這是提到的格式,但它實際上是16位灰度)。我嘗試使用LockBits,SetPixel和不安全的代碼來將像素數據轉換爲位圖,但所有嘗試均失敗。有沒有人有任何可以使這項工作的代碼。使用mDCM將JPG導出爲Dicom文件
下面是使用SetPixel我的最新嘗試:
DcmElement pixelData = this.currentFileFormat.Dataset.GetElement(new DcmTag(DcmConstTags.PixelData));
ushort[] pixels = this.currentPixelData.GetFrameDataU16(0);
this.currentImage = new Bitmap(this.currentPixelData.ImageWidth, this.currentPixelData.ImageHeight, PixelFormat.Format16bppRgb555);
int resample = (int)Math.Pow(2, (this.currentPixelData.BitsStored - 8));
int pxCounter = 0;
int min = ushort.MaxValue;
int max = 0;
for (int c = 0; c < this.currentPixelData.ImageHeight; c++)
{
for (int r = 0; r < this.currentPixelData.ImageWidth; r++)
{
ushort pxColor = pixels[pxCounter];
int temp = 255 - (pxColor/resample);
if (temp < min) min = temp;
if (temp > max) max = temp;
this.currentImage.SetPixel(r, c, Color.FromArgb(temp, temp, temp));
pxCounter++;
}
}
更新: 我已經得到了使用LockBits和Marshal.Copy接近,但圖像出來,在顏色的灰度,而不是彩虹。所以我猜我需要一種方法來灰度數據轉換爲RBG格式:
byte[] pixels = this.currentPixelData.GetFrameDataU8(frameIndex);
this.currentImage = new Bitmap(this.currentPixelData.ImageWidth, this.currentPixelData.ImageHeight, PixelFormat.Format16bppRgb555);
BitmapData bitmapData = this.currentImage.LockBits(new Rectangle(0, 0, this.currentImage.Width, this.currentImage.Height), ImageLockMode.ReadWrite, this.currentImage.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(pixels, 0, bitmapData.Scan0, this.currentImage.Width * this.currentImage.Height * 2);
this.currentImage.UnlockBits(bitmapData);
在此先感謝
附:在任何人建議嘗試像ClearCanvas之類的其他東西之前,知道mDCM是唯一符合我需求的庫,而ClearCanvas對於我需要做的事情太過臃腫。
添加了關於灰度的部分。對購買LeadTools不感興趣。需要代碼。 – 2010-03-29 20:31:13
我已經能夠進一步使用LockBits和Marshal.Copy使用下面的代碼,但它出現在顏色彩虹而不是灰度: byte [] pixels = currentPixelData.GetFrameDataU8(frameIndex); currentImage =新的位圖(寬度,高度,PixelFormat.Format16bppRgb555); BitmapData bitmapData = currentImage.LockBits(new Rectangle(0,0,width,height),ImageLockMode.ReadWrite,currentImage.PixelFormat); System.Runtime。InteropServices.Marshal.Copy(pixels,0,bitmapData.Scan0,width * height * 2); currentImage.UnlockBits(bitmapData); – 2010-03-29 20:41:14