DicomImage

DicomImage class

This Class implements Digital Imaging and Communications in Medicine (DICOM) raster image format support and offers a comprehensive solution for processing DICOM images with precision and flexibility. You can seamlessly manipulate image pages, including operations to get, add, or remove pages, and control the default and active pages. With capabilities to work with alpha channels, embed XMP metadata, resize, rotate, crop, binarize, adjust, apply filters, and convert to other raster formats. This API empowers developers to handle DICOM images effectively while meeting diverse application requirements in medical imaging contexts.

public sealed class DicomImage : RasterCachedMultipageImage, IMultipageImageExt

Constructors

NameDescription
DicomImage(Stream)Create a new instance of the DicomImage class by utilizing a stream parameter in this constructor. Perfect for developers seeking a streamlined way to initialize DicomImage objects from existing data streams in their projects.
DicomImage(Stream, LoadOptions)Initiate a new instance of the DicomImage class smoothly by employing a stream and loadOptions parameters in this constructor. Ideal for developers eager to start working with DicomImage objects promptly and effectively in their projects.
DicomImage(DicomOptions, int, int)Initialize a fresh instance of the DicomImage class effortlessly with this constructor, utilizing dicomOptions parameters. Perfect for developers looking to dive into DicomImage objects swiftly and efficiently in their projects.

Properties

NameDescription
ActivePage { get; set; }Manage the active page of the image with this intuitive property. Ideal for developers seeking to dynamically switch between pages within multi-page images, ensuring efficient navigation and processing.
ActivePageIndex { get; }Retrieve the index of the active page effortlessly with this intuitive property. Ideal for developers seeking quick access to the current page index within multi-page images, ensuring efficient navigation and processing.
AutoAdjustPalette { get; set; }Gets or sets a value indicating whether automatic adjust palette.
override BackgroundColor { get; set; }Gets or sets a value for the background color.
override BitsPerPixel { get; }Gets the image bits per pixel count.
Bounds { get; }Gets the image bounds.
BufferSizeHint { get; set; }Gets or sets the buffer size hint which is defined max allowed size for all internal buffers.
Container { get; }Gets the Image container.
DataStreamContainer { get; }Gets the object’s data stream.
DicomPages { get; }Access the pages of the image with this intuitive property. Ideal for developers seeking to interact with individual pages within the image, ensuring seamless navigation and manipulation.
Disposed { get; }Gets a value indicating whether this instance is disposed.
override FileFormat { get; }Retrieve the file format value effortlessly with this intuitive property. Ideal for developers seeking quick access to the format of the image file, ensuring efficient handling and processing based on the file type.
FileInfo { get; }Retrieve valuable header information from the DICOM file effortlessly with this intuitive property. Ideal for developers seeking quick access to essential details encapsulated within the DICOM file, ensuring efficient data extraction and analysis.
override HasAlpha { get; }Retrieve whether the image has an alpha channel effortlessly with this intuitive property. Ideal for developers seeking to determine if the image contains transparency information, ensuring precise handling of alpha channel data in image processing tasks.
virtual HasBackgroundColor { get; set; }Gets or sets a value indicating whether image has background color.
override HasTransparentColor { get; }Gets a value indicating whether image has transparent color.
override Height { get; }Gets the image height.
virtual HorizontalResolution { get; set; }Gets or sets the horizontal resolution, in pixels per inch, of this RasterImage.
override ImageOpacity { get; }Gets opacity of this image.
InterruptMonitor { get; set; }Gets or sets the interrupt monitor.
override IsCached { get; }Gets a value indicating whether image data is cached currently.
IsRawDataAvailable { get; }Gets a value indicating whether raw data loading is available.
override PageCount { get; }Retrieve the total page count of the image with this intuitive property. Ideal for developers seeking quick access to the number of pages within an image, ensuring efficient navigation and management.
override PageExportingAction { get; set; }Manage the page exporting action with this intuitive property. Ideal for developers seeking to control the export behavior of image pages, ensuring seamless integration and customization in various image processing workflows. Please note that setting this method will automatically release page resources after it is executed. It will be executed just before each page is saved.
override Pages { get; }Access the pages of the image with this intuitive property. Ideal for developers seeking to interact with individual pages within the image, ensuring seamless navigation and manipulation.
Palette { get; set; }Gets or sets the color palette. The color palette is not used when pixels are represented directly.
virtual PremultiplyComponents { get; set; }Gets or sets a value indicating whether the image components must be premultiplied.
RawCustomColorConverter { get; set; }Gets or sets the custom color converter
virtual RawDataFormat { get; }Gets the raw data format.
RawDataSettings { get; }Gets the current raw data settings. Note when using these settings the data loads without conversion.
RawFallbackIndex { get; set; }Gets or sets the fallback index to use when palette index is out of bounds
RawIndexedColorConverter { get; set; }Gets or sets the indexed color converter
virtual RawLineSize { get; }Gets the raw line size in bytes.
Size { get; }Gets the image size.
virtual TransparentColor { get; set; }Gets the image transparent color.
virtual UpdateXmpData { get; set; }Gets or sets a value indicating whether to update the XMP metadata.
override UsePalette { get; }Gets a value indicating whether the image palette is used.
virtual UseRawData { get; set; }Gets or sets a value indicating whether to use raw data loading when the raw data loading is available.
virtual VerticalResolution { get; set; }Gets or sets the vertical resolution, in pixels per inch, of this RasterImage.
override Width { get; }Gets the image width.
override XmpData { get; set; }Access or modify the XMP metadata associated with the image using this intuitive property. Ideal for developers seeking to manage metadata information embedded within the image file, ensuring seamless integration and customization in various image processing workflows.

Methods

NameDescription
AddPage()Append a new page to the end of the image’s page list with this straightforward method. Ideal for developers seeking to dynamically expand multi-page images, ensuring seamless integration and organization of image content.
AddPage(RasterImage)Expand your image collection by adding a new page with this intuitive method. Ideal for developers seeking to dynamically append pages to multi-page images, ensuring seamless expansion and organization of image content.
override AdjustBrightness(int)Enhance image luminance with the adjustment of brightness, a parameterized method that allows developers to finely tune the luminosity of images. This user-friendly function empowers developers to seamlessly manipulate image brightness, offering flexibility and control over visual aesthetics.
override AdjustContrast(float)Enhance Image contrast with this user-friendly method, which adjusts the disparity between light and dark areas. Improve visual clarity and definition effortlessly, providing developers with intuitive control over image contrast for optimal rendering.
override AdjustGamma(float)Enhance image quality and adjust it with gamma correction, a powerful technique for fine-tuning visual appearance. Perfect for developers aiming to optimize image presentation, adjust color balance, and ensure consistent rendering across different devices and environments.
override AdjustGamma(float, float, float)Achieve precise color adjustments by applying gamma correction independently to the red, green, and blue components of an image. This method ensures accurate color balance and optimal visual output, catering to developers seeking granular control over image rendering and color accuracy.
override BinarizeBradley(double)Binarization of an image using Bradley’s adaptive thresholding algorithm using the integral image thresholding
override BinarizeBradley(double, int)Binarize images with Bradley’s adaptive thresholding algorithm, leveraging integral image thresholding for improved performance. Ideal for developers looking to automatically segment images based on local variations in brightness, ensuring accurate object detection and extraction in varying lighting conditions.
override BinarizeFixed(byte)Easily convert the image into a binary format using a predefined threshold with this straightforward method. Ideal for developers looking to simplify image processing tasks by segmenting the image into foreground and background components based on specified intensity levels.
override BinarizeOtsu()Apply Otsu thresholding to binarize the image, automatically determining the optimal threshold value based on the image’s histogram. Perfect for developers seeking a reliable method to segment images into foreground and background regions with minimal manual intervention.
Blend(Point, RasterImage, byte)Blends this image instance with the overlay image.
override Blend(Point, RasterImage, Rectangle, byte)Blends this image instance with the overlay image.
override CacheData()This method efficiently caches data, optimizing performance and ensuring swift access when needed. Ideal for developers seeking to enhance the speed and efficiency of their applications by intelligently managing data resources.
CanSave(ImageOptionsBase)Determines whether image can be saved to the specified file format represented by the passed save options.
override Crop(Rectangle)Crop the image to remove unwanted areas and focus on essential content with this simple method. Ideal for developers seeking to customize the visual composition of images, ensuring they convey the desired message effectively.
override Crop(int, int, int, int)Adjust the cropping area of the image by applying shifts with this versatile method. Perfect for developers who need precise control over the cropping process, ensuring that important details are retained while eliminating unnecessary elements.
Dispose()Disposes the current instance.
Dither(DitheringMethod, int)Performs dithering on the current image.
override Dither(DitheringMethod, int, IColorPalette)Enhance the current image by applying dithering effects with this straightforward method. Perfect for developers aiming to add texture and depth to images, improving their visual quality and overall appeal.
override Filter(Rectangle, FilterOptionsBase)Effortlessly enhance specific areas of your image by applying filters to designated rectangles. This method provides developers with precise control over image manipulation, allowing for targeted adjustments to achieve desired visual effects with ease.
GetArgb32Pixel(int, int)Gets an image 32-bit ARGB pixel.
GetDefaultArgb32Pixels(Rectangle)Gets the default 32-bit ARGB pixels array.
virtual GetDefaultOptions(object[])Gets the default options.
GetDefaultPixels(Rectangle, IPartialArgb32PixelLoader)Gets the default pixels array using partial pixel loader.
GetDefaultRawData(Rectangle, RawDataSettings)Gets the default raw data array.
GetDefaultRawData(Rectangle, IPartialRawDataLoader, RawDataSettings)Gets the default raw data array using partial pixel loader.
virtual GetModifyDate(bool)Gets the date and time the resource image was last modified.
virtual GetOriginalOptions()Gets the options based on the original file settings. This can be helpful to keep bit-depth and other parameters of the original image unchanged. For example, if we load a black-white PNG image with 1 bit per pixel and then save it using the Save method, the output PNG image with 8-bit per pixel will be produced. To avoid it and save PNG image with 1-bit per pixel, use this method to get corresponding saving options and pass them to the Save method as the second parameter.
GetPixel(int, int)Gets an image pixel.
GetSkewAngle()Gets the skew angle. This method is applicable to scanned text documents, to determine the skew angle when scanning.
override Grayscale()Easily transform images into their grayscale representation, simplifying visual analysis and processing tasks. Perfect for developers seeking to enhance image clarity, reduce complexity, and facilitate efficient grayscale-based algorithms for diverse applications.
InsertPage(int)Insert a new page into the image’s page list at a specified index with this intuitive method. Ideal for developers seeking precise control over the arrangement of pages in multi-page images, ensuring seamless organization and customization of image content.
LoadArgb32Pixels(Rectangle)Loads 32-bit ARGB pixels.
LoadArgb64Pixels(Rectangle)Loads 64-bit ARGB pixels.
LoadCmyk32Pixels(Rectangle)Loads pixels in CMYK format.
LoadPartialArgb32Pixels(Rectangle, IPartialArgb32PixelLoader)Loads 32-bit ARGB pixels partially by packs.
LoadPartialArgb64Pixels(Rectangle, IPartialArgb64PixelLoader)Loads 64-bit ARGB pixels partially by packs.
LoadPartialPixels(Rectangle, IPartialPixelLoader)Loads pixels partially by packs.
LoadPixels(Rectangle)Loads pixels.
LoadRawData(Rectangle, RawDataSettings, IPartialRawDataLoader)Loads raw data.
LoadRawData(Rectangle, Rectangle, RawDataSettings, IPartialRawDataLoader)Loads raw data.
NormalizeAngle()Normalizes the angle. This method is applicable to scanned text documents to get rid of the skewed scan. This method uses GetSkewAngle and Rotate methods.
override NormalizeAngle(bool, Color)Normalizes the angle. This method is applicable to scanned text documents to get rid of the skewed scan. This method uses GetSkewAngle and Rotate methods.
ReadArgb32ScanLine(int)Reads the whole scan line by the specified scan line index.
ReadScanLine(int)Reads the whole scan line by the specified scan line index.
RemovePage(int)Eliminate the page at the specified index from the page list with this convenient method. Ideal for developers seeking precise control over the management of multi-page images, ensuring seamless organization and customization of image content.
ReplaceColor(Color, byte, Color)Replaces one color to another with allowed difference and preserves original alpha value to save smooth edges.
override ReplaceColor(int, byte, int)Replaces one color to another with allowed difference and preserves original alpha value to save smooth edges.
ReplaceNonTransparentColors(Color)Replaces all non-transparent colors with new color and preserves original alpha value to save smooth edges. Note: if you use it on images without transparency, all colors will be replaced with a single one.
override ReplaceNonTransparentColors(int)Replaces all non-transparent colors with new color and preserves original alpha value to save smooth edges. Note: if you use it on images without transparency, all colors will be replaced with a single one.
Resize(int, int)Resizes the image. The default NearestNeighbourResample is used.
override Resize(int, int, ImageResizeSettings)Adjust the size of your image with this simple resizing method. Whether you need to shrink or enlarge your image, this function ensures that your resizing needs are met efficiently and accurately, making it perfect for developers seeking quick and easy image size adjustments.
override Resize(int, int, ResizeType)Adjust the size of the image with this straightforward method. Ideal for developers looking to dynamically resize images, ensuring they fit seamlessly into various contexts and layouts within their applications.
ResizeHeightProportionally(int)Resizes the height proportionally. The default NearestNeighbourResample is used.
virtual ResizeHeightProportionally(int, ImageResizeSettings)Resizes the height proportionally.
override ResizeHeightProportionally(int, ResizeType)Adjust the height of the image while maintaining its aspect ratio with this user-friendly method. Perfect for developers seeking to dynamically resize images while preserving their proportions, ensuring optimal display and usability in their applications.
ResizeProportional(int, int, ResizeType)Resize the image while maintaining its aspect ratio with this convenient method. Ideal for developers seeking to adjust the image dimensions proportionally, ensuring consistency and preserving the original content’s proportions. The proportional resize will resize each frame according to the ratio of newWidth/width and newHeight/height.
ResizeWidthProportionally(int)Resizes the width proportionally. The default NearestNeighbourResample is used.
virtual ResizeWidthProportionally(int, ImageResizeSettings)Resizes the width proportionally.
override ResizeWidthProportionally(int, ResizeType)Adjust the width of the image while maintaining its aspect ratio with this convenient method. Ideal for developers seeking to resize images proportionally, ensuring consistent and visually appealing results across different display environment.
virtual Rotate(float)Rotate image around the center.
override Rotate(float, bool, Color)Rotate the image around its center with this convenient method. Ideal for developers seeking to adjust image orientation dynamically, ensuring optimal presentation and alignment within their applications.
override RotateFlip(RotateFlipType)Easily manipulate the active frame by rotating, flipping, or performing both actions simultaneously with this straightforward method. Ideal for developers who need to dynamically adjust the orientation of specific frames within their image sequences, ensuring optimal presentation and alignment.
Save()Saves the image data to the underlying stream.
Save(Stream)Saves the object’s data to the specified stream.
override Save(string)Saves the image to the specified file location.
Save(Stream, ImageOptionsBase)Saves the image’s data to the specified stream in the specified file format according to save options.
virtual Save(string, bool)Saves the object’s data to the specified file location.
virtual Save(string, ImageOptionsBase)Saves the object’s data to the specified file location in the specified file format according to save options.
override Save(Stream, ImageOptionsBase, Rectangle)Easily save your image data to a specified stream in the desired file format using this convenient method. Whether you’re working with JPEG, PNG, or another format, this function ensures that your image data is saved efficiently and accurately, making it ideal for developers looking to streamline their file-saving processes.
virtual Save(string, ImageOptionsBase, Rectangle)Saves the object’s data to the specified file location in the specified file format according to save options.
SaveAll(string, ImageOptionsBase)Preserve the object’s data by saving it to the designated file (indexer + filename) location along with specified file format and options. Ideal for developers seeking to securely store data in various formats while maintaining flexibility and control over saving parameters.
SaveArgb32Pixels(Rectangle, int[])Saves the 32-bit ARGB pixels.
SaveCmyk32Pixels(Rectangle, int[])Saves the pixels.
SavePixels(Rectangle, Color[])Saves the pixels.
SaveRawData(byte[], int, Rectangle, RawDataSettings)Saves the raw data.
SetArgb32Pixel(int, int, int)Sets an image 32-bit ARGB pixel for the specified position.
override SetPalette(IColorPalette, bool)Sets the image palette.
SetPixel(int, int, Color)Sets an image pixel for the specified position.
override SetResolution(double, double)Adjust the resolution of this RasterImage with precision using this straightforward method. Ideal for developers looking to tailor image resolution to specific requirements, ensuring optimal display quality and file size management.
virtual ToBitmap()Converts raster image to the bitmap. This method is not supported in versions from .Net7.0 and higher
WriteArgb32ScanLine(int, int[])Writes the whole scan line to the specified scan line index.
WriteScanLine(int, Color[])Writes the whole scan line to the specified scan line index.

Examples

Change Color Type in DICOM compression.

[C#]

using (var inputImage = Image.Load("original.jpg"))
{
    var options = new DicomOptions { ColorType = ColorType.Grayscale8Bit };

    inputImage.Save("original_8Bit.dcm", options);
}

Use RLE compression in DICOM image.

[C#]

using (var inputImage = Image.Load("original.jpg"))
{
    var options = new DicomOptions
    {
        ColorType = ColorType.Rgb24Bit,
        Compression = new Compression { Type = CompressionType.Rle }
    };

    inputImage.Save("original_RLE.dcm", options);
}

Use JPEG 2000 compression in DICOM image.

[C#]

using (var inputImage = Image.Load("original.jpg"))
{
    var options = new DicomOptions
    {
        ColorType = ColorType.Rgb24Bit,
        Compression = new Compression
        {
            Type = CompressionType.Jpeg2000,
            Jpeg2000 = new Jpeg2000Options
            {
                Codec = Jpeg2000Codec.Jp2,
                Irreversible = false
            }
        }
    };

    inputImage.Save("original_JPEG2000.dcm", options);
}

Use JPEG compression in DICOM image.

[C#]

using (var inputImage = Image.Load("original.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("original_JPEG.dcm", options);
}

This example shows how to load a DICOM image from a file stream.

[C#]

string dir = "c:\\temp\\";

// Load a DICOM image from a file stream.
using (System.IO.Stream stream = System.IO.File.OpenRead(dir + "sample.dicom"))
{
    using (Aspose.Imaging.FileFormats.Dicom.DicomImage dicomImage = new Aspose.Imaging.FileFormats.Dicom.DicomImage(stream))
    {
        // Save each page as an individual PNG image.                    
        foreach (Aspose.Imaging.FileFormats.Dicom.DicomPage dicomPage in dicomImage.DicomPages)
        {
            // Generate a file name based on the page index.
            string fileName = string.Format("sample.{0}.png", dicomPage.Index);

            // A DICOM page is a raster image, so all allowed operations with a raster image are applicable to a DICOM page.
            dicomPage.Save(dir + fileName, new Aspose.Imaging.ImageOptions.PngOptions());
        }
    }
}

Create a multi-page Dicom image.

[C#]

using (DicomImage image = (DicomImage)Image.Create(
        new DicomOptions() { Source = new StreamSource(new MemoryStream()) },
        100,
        100))
{
    // Draw something using vector graphics
    Graphics graphics = new Graphics(image);
    graphics.FillRectangle(new SolidBrush(Color.BlueViolet), image.Bounds);
    graphics.FillRectangle(new SolidBrush(Color.Aqua), 10, 20, 50, 20);
    graphics.FillEllipse(new SolidBrush(Color.Orange), 30, 50, 70, 30);

    // Save the pixels of the drawn image. They are now on the first page of the Dicom image.
    int[] pixels = image.LoadArgb32Pixels(image.Bounds);

    // Add a few pages after, making them darker
    for (int i = 1; i < 5; i++)
    {
        DicomPage page = image.AddPage();
        page.SaveArgb32Pixels(page.Bounds, pixels);
        page.AdjustBrightness(i * 30);
    }

    // Add a few pages in front of the main page, making them brighter
    for (int i = 1; i < 5; i++)
    {
        DicomPage page = image.InsertPage(0);
        page.SaveArgb32Pixels(page.Bounds, pixels);
        page.AdjustBrightness(-i * 30);
    }

    // Save the created multi-page image to the output file
    image.Save("MultiPage.dcm");
}

See Also