public static unsafe void RotateFlip(ref ImageAttributes source, RotateFlipType rft) { Bitmap bitmap = OtherImageFormats.GetBitmap(source, null, null, false); bitmap.RotateFlip(rft); DataSet dicom = OtherImageFormats.GetImage(bitmap); source.height = bitmap.Height; source.width = source.stride = bitmap.Width; Array.Copy((Array)dicom[t.PixelData].Value, source.buffer, source.buffer.Length); }
private void GetPicture() { PictureBox.Location = new Point(0, 0); PictureBox.Size = ClientRectangle.Size; ImageAttributes cropped = attributes; // OtherImageFormats.Crop(attributes); // if the application is minimized, PictureBox.Size has zero dimensions Size size = PictureBox.Size; if (size.Width == 0 || size.Height == 0) { // when the window is resized, we will render another image for display PictureBox.Image = null; return; } size = OtherImageFormats.Constrain(new Size(cropped.width, cropped.height), size); ImageAttributes resampled = (cropped.bitsperpixel == 8) ? cropped : Utilities.Resample(cropped, (double)attributes.width / (double)size.Width); ushort[] planes = null; if (overlays && overlay != null) { ImageAttributes source = new ImageAttributes(); source.type = TypeCode.UInt16; source.buffer = overlay; source.width = attributes.width; source.height = attributes.height; source.stride = attributes.width; // attributes.spacing = ; source.bitsperpixel = 12; source.monochrome1 = true; ImageAttributes temp = Utilities.Resample(source, (double)attributes.width / (double)size.Width); planes = (ushort[])temp.buffer; } Bitmap bitmap = OtherImageFormats.GetBitmap(resampled, planes, voilut, invert); Bitmap final = (bitmap.Size != size) ? new Bitmap(bitmap, size) : bitmap; SurroundMask(final); PictureBox.Image = final; }
public void SaveAs(DataSet dicom, string filename, bool invert, int rotation, bool flip) { try { if (rotation != 0 || flip) { string text = String.Format("Rotate{0}Flip{1}", rotation, (flip) ? "X" : "None"); RotateFlipType rft = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), text); RotateFlip(dicom, rft); } ImageAttributes attributes = Utilities.GetAttributes(dicom.Elements); ushort[] voilut = GetVOILUT(dicom.Elements); ushort[] overlays = ExtractOverlays(dicom.Elements); FileInfo info = new FileInfo(filename); ImageFormat format = ImageFormat.Bmp; switch (info.Extension.ToLower()) { case ".dcm": dicom.TransferSyntaxUID = Syntax.ExplicitVrLittleEndian; dicom.Part10Header = true; dicom.Write(filename); return; case ".raw": filename = String.Format("{0}.{1}.{2}.raw", info.FullName.Replace(info.Extension, ""), attributes.width, attributes.height); if (File.Exists(filename)) { DialogResult result = MessageBox.Show("File Exists.\nDo you want to overwrite?", "Dicom Viewer", MessageBoxButtons.YesNo); if (result == DialogResult.No) { return; } } using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write)) { EndianBinaryWriter writer = new EndianBinaryWriter(stream, Endian.Little); writer.WriteWords((short[])dicom[t.PixelData].Value); } return; case ".bmp": format = ImageFormat.Bmp; break; case ".jpg": format = ImageFormat.Jpeg; break; case ".tif": case ".tiff": format = ImageFormat.Tiff; break; case ".gif": format = ImageFormat.Gif; break; case ".png": format = ImageFormat.Png; break; case ".emf": format = ImageFormat.Emf; break; case ".exif": format = ImageFormat.Exif; break; case ".wmf": format = ImageFormat.Wmf; break; default: throw new ArgumentException("Unsupported extension"); } Bitmap bitmap = OtherImageFormats.GetBitmap(attributes, overlays, voilut, invert); bitmap.Save(filename, format); } catch (Exception ex) { MessageBox.Show(Logging.Log(ex)); } }