Manipulating DICOM Formats
Adjusting Brightness, Contrast and Gamma
Color adjustments in digital images is one of the core features that most of the imaging libraries provide. Color adjustments can be categorized in the following.
- Brightness refers to the lightness or darkness of color. Increasing the brightness of an image lights out all colors whereas decreasing the brightness darkens all colors.
- Contrast refers to making the objects or details within an image more obvious. Increasing the contrast of an image increases the difference between light and dark areas so that the light areas becomes lighter and dark areas becomes darker. Decreasing the contrast will make lighter and darker areas stay approximately the same but the overall image becomes more homogeneous.
- Gamma optimizes the contrast and brightness of the indirect lighting that is illuminating an object in the image.
Adjusting Brightness
Aspose.Imaging for .NET API provide AdjustBrightness method for the DicomImage class that can be used to adjust the Brightness by passing an integer value as parameter.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Adjust the brightness and Create an instance of BmpOptions for the resultant image and Save the resultant image | |
image.AdjustBrightness(50); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Adjusting Contrast
The AdjustContrast method exposed by the DicomImage class can be used to adjust the Contrast of an image by passing a float value as parameter.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Adjust contrast and Create an instance of BmpOptions for the resultant image and Save the resultant image | |
image.AdjustContrast(50); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Adjusting Gamma
The AdjustGamma method exposed by the DicomImage class can be used to adjust the Gamma of an image by passing a float value as parameter.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Adjust gamma and Create an instance of BmpOptions for the resultant image and Save the resultant image | |
image.AdjustGamma(50); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Applying Filters
This article demonstrates the usage of Aspose.Imaging for .NET to apply filter on a DICOM image. Aspose.Imaging APIs have exposed efficient & easy to use methods to achieve this goal. The following code example demonstrates how to apply filter on DICOM image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Apply filter and Create an instance of BmpOptions for the resultant image and Save the resultant image | |
image.Filter(image.Bounds, new MedianFilterOptions(8)); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Applying Binarization
In order to understand the concept of Binarization, it is important to define a Binary Image; that is a digital image that can have only two possible values for each pixel. Normally, the two colors used for a binary image are black and white though any two colors can be used. Binarization is the process of converting an image to bi-level meaning that each pixel is stored as a single bit (0 or 1) where 0 denotes the absence of color and 1 means presence of color. Aspose.Imaging for .NET API supports three different methods of binarization for DICOM images.
Using Fixed Threshold
Following code snippet demonstrates how Fixed Threshold Binarization can be applied to a DICOM image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an image in an instance of Image | |
using (Image image = Image.Load(dataDir + "template.dcm")) | |
{ | |
// Cast the image to RasterCachedImage and Check if image is cached | |
RasterCachedImage rasterCachedImage = (RasterCachedImage)image; | |
if (!rasterCachedImage.IsCached) | |
{ | |
// Cache image if not already cached | |
rasterCachedImage.CacheData(); | |
} | |
// Binarize image with predefined fixed threshold and Save the resultant image | |
rasterCachedImage.BinarizeFixed(100); | |
rasterCachedImage.Save(dataDir + "result.jpg"); | |
} | |
File.Delete(dataDir + "result.jpg"); |
Using Otsu Threshold
Following code snippet demonstrates how Otsu Threshold Binarization can be applied to a DICOM image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Binarize image with Otsu Thresholding and Save the resultant image. | |
image.BinarizeOtsu(); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Using Bradley’s Adaptive Threshold
Following code snippet demonstrates how Bradley’s Adaptive Threshold Binarization can be applied to a DICOM image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Binarize image with Otsu Thresholding and Save the resultant image. | |
image.BinarizeOtsu(); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Using Grayscaling
Gray-scaling is the process of converting a continuous-tone image to an image with discontinues gray shades.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Grayscale dicom image and Save the resultant image. | |
image.Grayscale(); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Cropping Image
Image cropping usually refers to the removal of the outer parts of an image to help improve the framing. Cropping may also be used to take out some portion of an image to increase the focus on a particular area. Aspose.Imaging for .NET API now supports DICOM image cropping.
Cropping by Shifts
The DicomImage class provides the Crop method that accepts 4 integer values. Based on these four values, the Crop method take out that part of the image while discarding the rest of the image. The code snippet below demonstrates how to crop a DICOM image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Crop dicom image and Save the resultant image. | |
image.Crop(1, 1, 1, 1); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Dithering For DICOM Image
Dithering is a technique of creating the illusion of new colors and shades by varying the pattern of dots that actually create an image. It is the most common means of reducing the color range of images down to the 256 (or fewer) colors. Aspose.Imaging provides the dithering support for DicomImage class by introducing Dither method that accepts two parameters. First is of type DitheringMethod to be applied with two possible options FloydSteinbergDithering and ThresholdDithering. The second parameter to Dither method is the BitCount in integer. BitCount defines the sampling size for the dithering result. Default value is 1 that represents black and white, whereas allowed values are 1, 4, 8 generating palettes with 2, 4 and 256 colors respectively.
Resizing DICOM Image
This article demonstrates the usage of Aspose.Imaging for .NET to perform Resize operation on a DICOM image. Aspose.Imaging APIs have exposed efficient & easy to use methods to achieve this goal. Aspose.Imaging for .NET has exposed the Resize method of the DicomImage class that can be used to re-size existing images on the fly. The steps to perform Resize are as simple as below:
- Load in image using the constructor of the DicomImage class.
- Call the DicomImage.Resize method while specifying new Height & Width.
- Save the results.
Simple Resizing
The following code example demonstrates how to Resize an image.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Resize dicom image and Save the resultant image. | |
image.Resize(200, 200); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Other Resizing Options
Aspose.Imaging API has exposed ResizeHeightProportionally and ResizeWidthProportionally methods of the DicomImage class that can be used to re-size the DICOM images. These methods take in an integer as first argument and ResizeType enumeration to achieve desired results. Below provided code snippet demonstrates the usage of these methods.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Resize dicom image and Save the resultant image. | |
image.ResizeHeightProportionally(100, ResizeType.AdaptiveResample); | |
image.ResizeWidthProportionally(150, ResizeType.AdaptiveResample); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Rotate and Flip DICOM Image
Aspose.Imaging for .NET has provided Rotate and RotateFlip methods of DicomImage class to rotate/flip a DICOM image. The DicomImage.Rotate method can be used to rotate the image. The Rotate method take an integer parameter as degrees to rotate. DicomImage.RotateFlip method accepts a parameter of RotateFlipType that specifies the type of rotation and flip to apply to the image. The steps to perform Rotate & Flip are as simple as below:
- Load in image using the DicomImage class constructor.
- Call the DicomImage.RotateFlip or DicomImage.Rotate method while specifying the appropriate parameter.
- Save the results.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
// Rotate dicom image and Save the resultant image. | |
image.Rotate(10); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (var fileStream = new FileStream(dataDir + "template.dcm", FileMode.Open, FileAccess.Read)) | |
{ | |
using (DicomImage image = new DicomImage(fileStream)) | |
{ | |
image.RotateFlip(RotateFlipType.Rotate180FlipNone); | |
image.Save(dataDir + "result.bmp", new BmpOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.bmp"); |
Export to DICOM
Aspose.Imaging supports export from various raster file formats including multi-paged to DICOM. Below there are some examples related to this.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
//DICOM file format | |
//Digital Imaging and Communications in Medicine is the standard for the communication and management of medical imaging information. DICOM is most commonly used for storing and transmitting //medical images in medical devices such as scanners, servers, printers and picture archiving and communication systems (PACS). DICOM is used worldwide to store, exchange, and transmit //medical images. | |
//Various programs for Windows, macOS, and Linux can view DICOM files. DICOM uses the .DCM extension. These images can also be viewed online through certain web browsers. It is only //compatible using Chrome, Opera, Firefox, and Internet Explorer with the Google Chrome Frame extension installed. | |
//Why to use DICOM? | |
//DICOM provides a well-tested and widely accepted foundation for Medical Image Management. The advantages of using DICOM: | |
//Makes medical imaging information interoperable. | |
//Integrates image-acquisition devices, PACS, workstations, VNAs and printers from different manufacturers. | |
//Is actively developed and maintained to meet the evolving technologies and needs of medical imaging. | |
//Is free to download and use. | |
//Convert JPEG to DICOM | |
//The next code sample converts JPEG image to DICOM file format: | |
using (var image = Image.Load(dataDir + "template.jpg")) | |
{ | |
image.Save(dataDir + "result.dcm", new DicomOptions()); | |
} | |
File.Delete(dataDir + "result.dcm"); | |
//Convert multipage images to DICOM | |
//DICOM format supports multipage images. You can convert GIF or TIFF images to DICOM in the same way as JPEG images: | |
using (var image = Image.Load(dataDir + "template.gif")) | |
{ | |
image.Save(dataDir + "result.dcm", new DicomOptions()); | |
} | |
File.Delete(dataDir + "result.dcm"); | |
//Export all DICOM pages to JPEG | |
//In case if you need to extract all the pages from DICOM file you can use the next code. It creates separate JPEG file for each DICOM page: | |
using (var image = (DicomImage)Image.Load(dataDir + "template.dcm")) | |
{ | |
for (var index = 0; index < image.Pages.Length; index++) | |
{ | |
var page = image.Pages[index]; | |
page.Save(dataDir + $"Page {index}.jpeg", new JpegOptions()); | |
File.Delete(dataDir + $"Page {index}.jpeg"); | |
} | |
} |
Support of Jpeg, Jpeg2000 and RLE compression methods in Dicom
Dicom format can be compressed using Jpeg, Jpeg2000 and RLE compressions.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
//### DICOM compression settings | |
//The property ***DicomOptions.Compression*** allows you to specify compression settings. | |
//For instance, ***CompressionType*** enumeration allows you to select compression algorithm: | |
//*None *, *Jpeg *, *Jpeg2000 * or * Rle *.The * None * option corresponds to uncompressed DICOM image. | |
//The following code shows how to use DICOM compression settings: | |
using (var inputImage = Image.Load(dataDir + "template.jpg")) | |
{ | |
var options = new DicomOptions | |
{ | |
ColorType = ColorType.Rgb24Bit, | |
Compression = new Compression { Type = CompressionType.None } | |
}; | |
inputImage.Save(dataDir + "result.dcm", options); | |
} | |
File.Delete(dataDir + "result.dcm"); | |
//### Using JPEG compression in DICOM image | |
//To use JPEG compression algorithm you should specify | |
//*CompressionType.Jpeg* enumeration value in ***Compression.Type*** property: | |
using (var inputImage = Image.Load(dataDir + "template.jpg")) | |
{ | |
var options = new DicomOptions | |
{ | |
ColorType = ColorType.Rgb24Bit, | |
Compression = new Compression { Type = CompressionType.Jpeg } | |
}; | |
inputImage.Save(dataDir + "result.dcm", options); | |
} | |
File.Delete(dataDir + "result.dcm"); | |
//You can tune JPEG compression algorithm using ***Compression.Jpeg * **property.For instance, | |
//you can specify the *CompressionType*, *SampleRoundingMode* and *Quality*: | |
using (var inputImage = Image.Load(dataDir + "template.jpg")) | |
{ | |
var options = new DicomOptions | |
{ | |
ColorType = ColorType.Rgb24Bit, | |
Compression = new Compression | |
{ | |
Type = CompressionType.Jpeg, | |
Jpeg = new JpegOptions | |
{ | |
CompressionType = JpegCompressionMode.Baseline, | |
SampleRoundingMode = SampleRoundingMode.Truncate, | |
Quality = 50 | |
} | |
} | |
}; | |
inputImage.Save(dataDir + "result.dcm", options); | |
} | |
File.Delete(dataDir + "result.dcm"); | |
//### Using JPEG 2000 compression in DICOM image | |
//To use JPEG 2000 compression you need to use *CompressionType.Jpeg2000* enumeration value and | |
//***Jpeg2000Options*** class for algorithm settings. The following code demonstrates how to specify | |
//JPEG 2000 * Codec * and * Irreversible * properties: | |
using (var inputImage = Image.Load(dataDir + "template.jpg")) | |
{ | |
var options = new DicomOptions | |
{ | |
ColorType = ColorType.Rgb24Bit, | |
Compression = new Compression | |
{ | |
Type = CompressionType.Jpeg2000, | |
Jpeg2000 = new Jpeg2000Options | |
{ | |
Codec = Jpeg2000Codec.Jp2, | |
Irreversible = false | |
} | |
} | |
}; | |
inputImage.Save(dataDir + "result.dcm", options); | |
} | |
File.Delete(dataDir + "result.dcm"); | |
//### Using RLE compression in DICOM image | |
//For this compression type you need to use *CompressionType.Rle* enumeration value.The RLE compression | |
//algorithm doesn't have additional settings. The following code shows how you can use RLE compression | |
//algorithm in DICOM image: | |
using (var inputImage = Image.Load(dataDir + "template.jpg")) | |
{ | |
var options = new DicomOptions | |
{ | |
ColorType = ColorType.Rgb24Bit, | |
Compression = new Compression { Type = CompressionType.Rle } | |
}; | |
inputImage.Save(dataDir + "result.dcm", options); | |
} | |
File.Delete(dataDir + "result.dcm"); | |
//### How to change Color Type in DICOM compression | |
//The property ***DicomOptions.ColorType*** allows you to change color type in DICOM compression. | |
//There are several supported color types: *Grayscale8Bit *, *Grayscale16Bit * and * Rgb24Bit *.Use the following code in order to change the color type: | |
using (var inputImage = Image.Load(dataDir + "template.jpg")) | |
{ | |
var options = new DicomOptions { ColorType = ColorType.Grayscale8Bit }; | |
inputImage.Save(dataDir + "result.dcm", options); | |
} | |
File.Delete(dataDir + "result.dcm"); |
Memory strategy optimization
The memory optimization strategy is now supported for Dicom images.
Here are some examples of its use:
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.FileFormats.Dicom; | |
using Aspose.Imaging.FileFormats.Eps; | |
using Aspose.Imaging.FileFormats.Eps.Consts; | |
using Aspose.Imaging.FileFormats.Jpeg; | |
using Aspose.Imaging.FileFormats.Jpeg2000; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Psd; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.ImageFilters.FilterOptions; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Example 1. Setting a memory limit of 50 megabytes for operations on the created Dicom image | |
var imageOptions = new DicomOptions(); | |
imageOptions.Source = new FileCreateSource(dataDir + "created.dcm", false); | |
imageOptions.BufferSizeHint = 50; | |
using (Image image = Image.Create(imageOptions, 1000, 1000)) | |
{ | |
// Do something with the created image | |
image.Save(); | |
} | |
File.Delete(dataDir + "created.dcm"); | |
// Example 2. Setting a memory limit of 20 megabytes for operations on the loaded Dicom image | |
var loadOptions = new LoadOptions(); | |
loadOptions.BufferSizeHint = 20; | |
using (Image image = Image.Load(dataDir + "template.dcm", loadOptions)) | |
{ | |
// Do something with the loaded image | |
} | |
// Example 3. Settings a memory limit of 30 mebagytes for export-to-dicom operation | |
loadOptions = new LoadOptions(); | |
loadOptions.BufferSizeHint = 30; | |
using (Image image = Image.Load(dataDir + "template.png", loadOptions)) | |
{ | |
image.Save(dataDir + "result.dcm", new DicomOptions()); | |
} | |
File.Delete(dataDir + "result.dcm"); |
Add xmp tags to dicom image
Using Aspose.Imaging you can easily add xmp tags to dicom image.
Here is example of usage: