Class BmpImage

Class BmpImage

Namespace: Aspose.Imaging.FileFormats.Bmp
Assembly: Aspose.Imaging.dll (25.7.0)

You can effortlessly handle Bitmap (BMP) and Device Independent Bitmap(DIB) files, facilitating efficient manipulation and processing of rasterimages. Performing various operations on images, this API streamlines theworkflow, offering developers a reliable toolkit for working with BMP andDIB formats in their software applications.

[JsonObject(MemberSerialization.OptIn)]
   public sealed class BmpImage : RasterCachedImage, IDisposable, IObjectWithBounds, IRasterImageArgb32PixelLoader, IRasterImageRawDataLoader, IHasXmpData, IHasMetadata
   {
   }

Inheritance

object DisposableObject DataStreamSupporter Image RasterImage RasterCachedImage BmpImage

Implements

IDisposable , IObjectWithBounds , IRasterImageArgb32PixelLoader , IRasterImageRawDataLoader , IHasXmpData , IHasMetadata

Inherited Members

RasterCachedImage.CacheData() , RasterCachedImage.Blend(Point, RasterImage, Rectangle, byte) , RasterCachedImage.Resize(int, int, ResizeType) , RasterCachedImage.Resize(int, int, ImageResizeSettings) , RasterCachedImage.RotateFlip(RotateFlipType) , RasterCachedImage.Rotate(float, bool, Color) , RasterCachedImage.Crop(Rectangle) , RasterCachedImage.Dither(DitheringMethod, int, IColorPalette) , RasterCachedImage.Grayscale() , RasterCachedImage.NormalizeHistogram() , RasterCachedImage.BinarizeFixed(byte) , RasterCachedImage.BinarizeOtsu() , RasterCachedImage.BinarizeBradley(double, int) , RasterCachedImage.BinarizeBradley(double) , RasterCachedImage.AdjustBrightness(int) , RasterCachedImage.AdjustContrast(float) , RasterCachedImage.AdjustGamma(float, float, float) , RasterCachedImage.AdjustGamma(float) , RasterCachedImage.IsCached , RasterImage.RemoveMetadata() , RasterImage.Save(Stream, ImageOptionsBase, Rectangle) , RasterImage.GetModifyDate(bool) , RasterImage.Dither(DitheringMethod, int) , RasterImage.Dither(DitheringMethod, int, IColorPalette) , RasterImage.GetDefaultPixels(Rectangle, IPartialArgb32PixelLoader) , RasterImage.GetDefaultRawData(Rectangle, IPartialRawDataLoader, RawDataSettings) , RasterImage.GetDefaultArgb32Pixels(Rectangle) , RasterImage.GetDefaultRawData(Rectangle, RawDataSettings) , RasterImage.GetArgb32Pixel(int, int) , RasterImage.GetPixel(int, int) , RasterImage.SetArgb32Pixel(int, int, int) , RasterImage.SetPixel(int, int, Color) , RasterImage.ReadScanLine(int) , RasterImage.ReadArgb32ScanLine(int) , RasterImage.WriteScanLine(int, Color[]) , RasterImage.WriteArgb32ScanLine(int, int[]) , RasterImage.LoadPartialArgb32Pixels(Rectangle, IPartialArgb32PixelLoader) , RasterImage.LoadPartialPixels(Rectangle, IPartialPixelLoader) , RasterImage.LoadArgb32Pixels(Rectangle) , RasterImage.LoadArgb64Pixels(Rectangle) , RasterImage.LoadPartialArgb64Pixels(Rectangle, IPartialArgb64PixelLoader) , RasterImage.LoadPixels(Rectangle) , RasterImage.LoadCmykPixels(Rectangle) , RasterImage.LoadCmyk32Pixels(Rectangle) , RasterImage.LoadRawData(Rectangle, RawDataSettings, IPartialRawDataLoader) , RasterImage.LoadRawData(Rectangle, Rectangle, RawDataSettings, IPartialRawDataLoader) , RasterImage.SaveRawData(byte[], int, Rectangle, RawDataSettings) , RasterImage.SaveArgb32Pixels(Rectangle, int[]) , RasterImage.SavePixels(Rectangle, Color[]) , RasterImage.SaveCmykPixels(Rectangle, CmykColor[]) , RasterImage.SaveCmyk32Pixels(Rectangle, int[]) , RasterImage.SetResolution(double, double) , RasterImage.SetPalette(IColorPalette, bool) , RasterImage.Resize(int, int, ImageResizeSettings) , RasterImage.Rotate(float, bool, Color) , RasterImage.Rotate(float) , RasterImage.BinarizeFixed(byte) , RasterImage.BinarizeOtsu() , RasterImage.BinarizeBradley(double) , RasterImage.BinarizeBradley(double, int) , RasterImage.Blend(Point, RasterImage, Rectangle, byte) , RasterImage.Blend(Point, RasterImage, byte) , RasterImage.Grayscale() , RasterImage.NormalizeHistogram() , RasterImage.AdjustBrightness(int) , RasterImage.AdjustContrast(float) , RasterImage.AdjustGamma(float, float, float) , RasterImage.AdjustGamma(float) , RasterImage.GetSkewAngle() , RasterImage.NormalizeAngle() , RasterImage.NormalizeAngle(bool, Color) , RasterImage.Filter(Rectangle, FilterOptionsBase) , RasterImage.ReplaceColor(Color, byte, Color) , RasterImage.ReplaceColor(int, byte, int) , RasterImage.ReplaceNonTransparentColors(Color) , RasterImage.ReplaceNonTransparentColors(int) , RasterImage.ToBitmap() , RasterImage.PremultiplyComponents , RasterImage.UseRawData , RasterImage.UpdateXmpData , RasterImage.XmpData , RasterImage.RawIndexedColorConverter , RasterImage.RawCustomColorConverter , RasterImage.RawFallbackIndex , RasterImage.RawDataSettings , RasterImage.UsePalette , RasterImage.RawDataFormat , RasterImage.RawLineSize , RasterImage.IsRawDataAvailable , RasterImage.HorizontalResolution , RasterImage.VerticalResolution , RasterImage.HasTransparentColor , RasterImage.HasAlpha , RasterImage.TransparentColor , RasterImage.ImageOpacity , Image.CanLoad(string) , Image.CanLoad(string, LoadOptions) , Image.CanLoad(Stream) , Image.CanLoad(Stream, LoadOptions) , Image.Create(ImageOptionsBase, int, int) , Image.Create(Image[]) , Image.Create(MultipageCreateOptions) , Image.Create(string[], bool) , Image.Create(string[]) , Image.Create(Image[], bool) , Image.GetFileFormat(string) , Image.GetFileFormat(Stream) , Image.GetFittingRectangle(Rectangle, int, int) , Image.GetFittingRectangle(Rectangle, int[], int, int) , Image.Load(string, LoadOptions) , Image.Load(string) , Image.Load(Stream, LoadOptions) , Image.Load(Stream) , Image.GetProportionalWidth(int, int, int) , Image.GetProportionalHeight(int, int, int) , Image.RemoveMetadata() , Image.CanSave(ImageOptionsBase) , Image.Resize(int, int) , Image.Resize(int, int, ResizeType) , Image.Resize(int, int, ImageResizeSettings) , Image.GetDefaultOptions(object[]) , Image.GetOriginalOptions() , Image.ResizeWidthProportionally(int) , Image.ResizeHeightProportionally(int) , Image.ResizeWidthProportionally(int, ResizeType) , Image.ResizeHeightProportionally(int, ResizeType) , Image.ResizeWidthProportionally(int, ImageResizeSettings) , Image.ResizeHeightProportionally(int, ImageResizeSettings) , Image.RotateFlip(RotateFlipType) , Image.Rotate(float) , Image.Crop(Rectangle) , Image.Crop(int, int, int, int) , Image.Save() , Image.Save(string) , Image.Save(string, ImageOptionsBase) , Image.Save(string, ImageOptionsBase, Rectangle) , Image.Save(Stream, ImageOptionsBase) , Image.Save(Stream, ImageOptionsBase, Rectangle) , Image.GetSerializedStream(ImageOptionsBase, Rectangle, out int) , Image.SetPalette(IColorPalette, bool) , Image.BitsPerPixel , Image.Bounds , Image.Container , Image.Height , Image.Palette , Image.UsePalette , Image.Size , Image.Width , Image.InterruptMonitor , Image.BufferSizeHint , Image.AutoAdjustPalette , Image.HasBackgroundColor , Image.FileFormat , Image.BackgroundColor , DataStreamSupporter.CacheData() , DataStreamSupporter.Save() , DataStreamSupporter.Save(Stream) , DataStreamSupporter.Save(string) , DataStreamSupporter.Save(string, bool) , DataStreamSupporter.DataStreamContainer , DataStreamSupporter.IsCached , DisposableObject.Dispose() , DisposableObject.Disposed , object.GetType() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()

Examples

Decompress BMP image which was previously compressed using DXT1 compression algorithm.

using (var image = Image.Load("CompressedTiger.bmp"))
   {
       image.Save("DecompressedTiger.bmp", new BmpOptions());
   }

Compress BMP image using DXT1 compression algorithm.

using (var image = Image.Load("Tiger.bmp"))
   {
       image.Save("CompressedTiger.bmp", new BmpOptions { Compression = BitmapCompression.Dxt1 });
   }

The example shows how to remove any object from the image using Graphics Path with Telea algorithm.

var imageFilePath = "ball.png";
   using (var image = Image.Load(imageFilePath))
   {
       var pngImage = (PngImage)image;
       var mask = new GraphicsPath();
       var firstFigure = new Figure();
       firstFigure.AddShape(new EllipseShape(new RectangleF(350, 170, 570 - 350, 400 - 170)));
       mask.AddFigure(firstFigure);
       var options = new TeleaWatermarkOptions(mask);
       var result = WatermarkRemover.PaintOver(pngImage, options);
       result.Save(outputPath);
   }

The example shows how to export a BmpImage with the Rgb compression type.

string sourcePath = "input.png";
   using (Image pngImage = Image.Load(sourcePath))
   {
       pngImage.Save(outputPath, new BmpOptions { Compression = BitmapCompression.Rgb });
   }

The example shows how to remove any object from the image using Graphics Path with Content Aware fill algorithm.

var imageFilePath = "ball.png";
   using (var image = Image.Load(imageFilePath))
   {
       var pngImage = (PngImage)image;
       var mask = new GraphicsPath();
       var firstFigure = new Figure();
       firstFigure.AddShape(new EllipseShape(new RectangleF(350, 170, 570 - 350, 400 - 170)));
       mask.AddFigure(firstFigure);
       var options = new ContentAwareFillWatermarkOptions(mask)
       {
           MaxPaintingAttempts = 4
       };
       var result = WatermarkRemover.PaintOver(pngImage, options);
       result.Save(outputPath);
   }

The following example shows how to create a BMP image of the specified size.

string dir = "c:\\temp\\";
   using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
   {
       int width = bmpImage.Width;
       int height = bmpImage.Height;
       for (int y = 0; y < height; y++)
       {
           for (int x = 0; x < width; x++)
           {
               int hue = (255 * x) / width;
               bmpImage.SetPixel(x, y, Aspose.Imaging.Color.FromArgb(255, hue, 0, 0));
           }
       }
       using (System.IO.FileStream stream = new System.IO.FileStream(dir + "output.bmp", System.IO.FileMode.Create))
       {
           bmpImage.Save(stream);
       }
   }

The example shows how to export a BmpImage from a Png file while keeping the alpha channel, save a Bmp file with transparency.

string sourcePath = "input.png";
   using (Image pngImage = Image.Load(sourcePath))
   {
       pngImage.Save(outputPath);
       pngImage.Save(outputPath, new BmpOptions());
       pngImage.Save(outputPath, new BmpOptions() { Compression = BitmapCompression.Bitfields });
   }

Constructors

BmpImage(string)

Start using the BmpImage class effortlessly with this constructor thatinitializes a new instance. Perfect for developers who want to get up andrunning with Aspose.Imaging.FileFormats.Bmp.BmpImage objects quickly and efficiently.

public BmpImage(string path)
   {
   }

Parameters

path string

The path to load image from and initialize pixel and palette data with.

Examples

The example shows how to load a BmpImage from a file.

string dir = "c:\\temp\\";
   using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(dir + "sample.bmp"))
   {
       bmpImage.Save(dir + "sample.output.32bpp.bmp");
   }

Exceptions

ArgumentNullException

The raster image is null.

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(string, ushort, BitmapCompression, double, double)

Effortlessly create a new instance of the Aspose.Imaging.FileFormats.Bmp.BmpImage class with this constructor,using specified parameters like path, bitsPerPixel, and compression. Ideal for developerslooking to initialize BmpImage objects quickly and efficiently, with precise controlover image characteristics.

public BmpImage(
      string path,
      ushort bitsPerPixel,
      BitmapCompression compression,
      double horizontalResolution,
      double verticalResolution
   )
   {
   }

Parameters

path string

The path to load image from and initialize pixel and palette data with.

bitsPerPixel ushort

The bits per pixel.

compression BitmapCompression

The compression to use.

horizontalResolution double

The horizontal resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

verticalResolution double

The vertical resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

Examples

The example shows how to load a BmpImage from a file with the specified bit depth and resolution.

string dir = "c:\\temp\\";
   using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage =
       new Aspose.Imaging.FileFormats.Bmp.BmpImage(dir + "sample.bmp", 24, Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb, 96.0, 96.0))
   {
       bmpImage.Save(dir + "sample.output.24bpp.96dpi.bmp");
   }

Exceptions

ArgumentNullException

The raster image is null.

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(Stream)

Begin using the Aspose.Imaging.FileFormats.Bmp.BmpImage class effortlessly by initializing a new instancewith this constructor, using a stream as input. Perfect for developers seekinga convenient way to work with BmpImage objects from various data sources,ensuring flexibility and ease of integration.

public BmpImage(Stream stream)
   {
   }

Parameters

stream Stream

The stream to load image from and initialize pixel and palette data with.

Examples

The example shows how to load a BmpImage from a file stream.

string dir = "c:\\temp\\";
   using (System.IO.Stream stream = System.IO.File.OpenRead(dir + "sample.bmp"))
   {
       using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(stream))
       {
           bmpImage.Save(dir + "sample.output.32bpp.bmp");
       }
   }

Exceptions

ArgumentNullException

The raster image is null.

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(Stream, ushort, BitmapCompression, double, double)

Start working with the Aspose.Imaging.FileFormats.Bmp.BmpImage class seamlessly by creatinga new instance using a stream, along with specified parameters like bitsPerPixeland compression. Perfect for developers seeking a straightforward way to handleBmpImage objects, ensuring flexibility and efficiency in their projects.

public BmpImage(
      Stream stream,
      ushort bitsPerPixel,
      BitmapCompression compression,
      double horizontalResolution,
      double verticalResolution)
   {
   }

Parameters

stream Stream

The stream to load image from and initialize pixel and palette data with.

bitsPerPixel ushort

The bits per pixel.

compression BitmapCompression

The compression to use.

horizontalResolution double

The horizontal resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

verticalResolution double

The vertical resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

Examples

The example shows how to load a BmpImage from a file stream with the specified bit depth and resolution.

string dir = "c:\\temp\\";
   using (System.IO.Stream stream = System.IO.File.OpenRead(dir + "sample.bmp"))
   {
       using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage =
               new Aspose.Imaging.FileFormats.Bmp.BmpImage(stream, 24, Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb, 96.0, 96.0))
       {
           bmpImage.Save(dir + "sample.output.24bpp.96dpi.bmp");
       }
   }

Exceptions

ArgumentNullException

The raster image is null.

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(RasterImage)

Effortlessly create a new instance of the Aspose.Imaging.FileFormats.Bmp.BmpImage classby initializing it with a RasterImage object. Perfect for developers lookingto seamlessly convert existing raster images to the BmpImage format, ensuringcompatibility and ease of integration into their projects.

public BmpImage(RasterImage rasterImage)
{
}
In this case, there were no changes to be made as the input already follows standard C# conventions.

Parameters

rasterImage RasterImage

The image to initialize pixel and palette data with.

Examples

The example shows how to load a BmpImage from another instance of RasterImage.

string dir = "c:\\temp\\";
   Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
   createOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream(), true);
   using (Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Create(createOptions, 100, 100))
   {
       Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(rasterImage);
       Aspose.Imaging.Brushes.SolidBrush brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
       gr.FillRectangle(brush, rasterImage.Bounds);
       using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(rasterImage))
       {
           bmpImage.Save(dir + "output.32bpp.bmp");
       }
   }

Exceptions

ArgumentNullException

The raster image is null.

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(RasterImage, ushort, BitmapCompression, double, double)

Start working with the Aspose.Imaging.FileFormats.Bmp.BmpImage class seamlessly by creating a new instanceusing a rasterImage along with specified parameters like bitsPerPixel and compression.Perfect for developers seeking a straightforward way to handle BmpImage objects,ensuring flexibility and efficiency in their projects.

public BmpImage(
    RasterImage rasterImage,
    ushort bitsPerPixel,
    BitmapCompression compression,
    double horizontalResolution,
    double verticalResolution)
{
}

Parameters

rasterImage RasterImage

The image to initialize pixel and palette data with.

bitsPerPixel ushort

The bits per pixel.

compression BitmapCompression

The compression to use.

horizontalResolution double

The horizontal resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

verticalResolution double

The vertical resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

Examples

The example shows how to load a BmpImage from another instance of RasterImage with the specified bit depth and compression.

string dir = "c:\\temp\\";
   Aspose.Imaging.ImageOptions.PngOptions createOptions = new Aspose.Imaging.ImageOptions.PngOptions();
   createOptions.Source = new Aspose.Imaging.Sources.StreamSource(new System.IO.MemoryStream(), true);
   using (Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)Aspose.Imaging.Image.Create(createOptions, 100, 100))
   {
       Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(rasterImage);
       Aspose.Imaging.Brushes.SolidBrush brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
       gr.FillRectangle(brush, rasterImage.Bounds);
       using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(rasterImage, 24, Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb, 96.0, 96.0))
       {
           bmpImage.Save(dir + "output.24bpp.96dpi.bmp");
       }
   }

Exceptions

ArgumentNullException

The raster image is null.

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(int, int)

Start using the Aspose.Imaging.FileFormats.Bmp.BmpImage class effortlessly by creating a new instancewith specified width and height parameters. Ideal for developers seekinga convenient way to generate BmpImage objects of custom dimensions, ensuringflexibility and ease of integration into their projects.

public BmpImage(int width, int height)
   {
   }

Parameters

width int

The image width.

height int

The image height.

Examples

The example shows how to create a BmpImage of the specified size.

string dir = "c:\\temp\\";
   using (var bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
   {
       var gr = new Aspose.Imaging.Graphics(bmpImage);
       var brush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
       gr.FillRectangle(brush, bmpImage.Bounds);
       bmpImage.Save(dir + "output.bmp");
   }

The following example shows how to palletize a BMP image to reduce its output size.

using Aspose.Imaging.FileFormats.Bmp;
   using Aspose.Imaging.Brushes;
   using Aspose.Imaging;
   using Aspose.Imaging.ImageOptions;
   using System.IO;
   BmpImage bmpImage = new BmpImage(100, 100);
   LinearGradientBrush brush = new LinearGradientBrush(
       new Point(0, 0),
       new Point(bmpImage.Width, bmpImage.Height),
       Color.Red,
       Color.Green);
   Graphics gr = new Graphics(bmpImage);
   gr.FillRectangle(brush, bmpImage.Bounds);
   IColorPalette palette = ColorPaletteHelper.GetCloseImagePalette(bmpImage, 256);
   BmpOptions saveOptions = new BmpOptions();
   saveOptions.Palette = palette;
   saveOptions.BitsPerPixel = 8;
   using (MemoryStream stream = new MemoryStream())
   {
       bmpImage.Save(stream, saveOptions);
       Console.WriteLine("The palettized image size is {0} bytes.", stream.Length);
   }
   using (MemoryStream stream = new MemoryStream())
   {
       bmpImage.Save(stream);
       Console.WriteLine("The non-palettized image size is {0} bytes.", stream.Length);
   }

Exceptions

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(int, int, ushort, IColorPalette)

Begin using the Aspose.Imaging.FileFormats.Bmp.BmpImage class seamlessly by initializing a new instancewith parameters such as width, height, bit depth, and palette. Perfect fordevelopers seeking a straightforward way to create BmpImage objects withcustom dimensions and color configurations, ensuring flexibility and efficiency in their projects.

public BmpImage(
    int width,
    int height,
    ushort bitsPerPixel,
    IColorPalette palette
)
{
}

Parameters

width int

The image width.

height int

The image height.

bitsPerPixel ushort

The bits per pixel.

palette IColorPalette

The color palette.

Examples

The example shows how to create a BmpImage of the specified size with the specified palette.

string dir = "c:\\temp\\";
   Aspose.Imaging.Color[] paletteColors = new Aspose.Imaging.Color[]
   {
      Aspose.Imaging.Color.Red,
      Aspose.Imaging.Color.Green
   };
   Aspose.Imaging.IColorPalette palette = new Aspose.Imaging.ColorPalette(paletteColors);
   using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 1, palette))
   {
      Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
      Aspose.Imaging.Brushes.SolidBrush redBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
      gr.FillRectangle(redBrush, new Aspose.Imaging.Rectangle(0, 0, bmpImage.Width, bmpImage.Height / 2));
      Aspose.Imaging.Brushes.SolidBrush greenBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Green);
      gr.FillRectangle(greenBrush, new Aspose.Imaging.Rectangle(0, bmpImage.Height / 2, bmpImage.Width, bmpImage.Height / 2));
      bmpImage.Save(dir + "output.monochrome.bmp");
   }

Exceptions

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

BmpImage(int, int, ushort, IColorPalette, BitmapCompression, double, double)

Effortlessly create a new instance of the Aspose.Imaging.FileFormats.Bmp.BmpImage class with this constructor,specifying parameters like width, height, bitsPerPixel, and palette. Perfect for developersseeking a convenient way to generate BmpImage objects with custom dimensionsand color configurations, ensuring flexibility and ease of integration into their projects.

public BmpImage(
    int width,
    int height,
    ushort bitsPerPixel,
    IColorPalette palette,
    BitmapCompression compression,
    double horizontalResolution,
    double verticalResolution)

Parameters

width int

The image width.

height int

The image height.

bitsPerPixel ushort

The bits per pixel.

palette IColorPalette

The color palette.

compression BitmapCompression

The compression to use.

horizontalResolution double

The horizontal resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

verticalResolution double

The vertical resolution. Note due to the rounding the resulting resolution may slightly differ from the passed.

Examples

The example shows how to create a BmpImage using various options.

string dir = "c:\\temp\\";
   Aspose.Imaging.Color[] paletteColors = new Aspose.Imaging.Color[]
   {
      Aspose.Imaging.Color.Red,
      Aspose.Imaging.Color.Green
   };
   Aspose.Imaging.IColorPalette palette = new Aspose.Imaging.ColorPalette(paletteColors);
   using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 1, palette, Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb, 96.0, 96.0))
   {
      Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
      Aspose.Imaging.Brushes.SolidBrush redBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
      gr.FillRectangle(redBrush, new Aspose.Imaging.Rectangle(0, 0, bmpImage.Width, bmpImage.Height / 2));
      Aspose.Imaging.Brushes.SolidBrush greenBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Green);
      gr.FillRectangle(greenBrush, new Aspose.Imaging.Rectangle(0, bmpImage.Height / 2, bmpImage.Width, bmpImage.Height / 2));
      bmpImage.Save(dir + "output.monochrome.96dpi.bmp");
   }

Exceptions

BmpImageException

The height must be positive.

ArgumentException

Palette must be specified for images with 8 bits per pixel or less.

Properties

BitmapInfoHeader

Quickly access essential details about your bitmap image with this straightforward function.Perfect for developers needing to retrieve header information for their images.

public BitmapInfoHeader BitmapInfoHeader
   {
      get;
   }

Property Value

BitmapInfoHeader

Examples

The following example gets the information from the BMP header and prints it to the console.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
      Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
      Aspose.Imaging.FileFormats.Bmp.BitmapInfoHeader header = bmpImage.BitmapInfoHeader;
      System.Console.WriteLine("The number of palette colors that are required for displaying the bitmap: {0}", header.BitmapColorsImportant);
      System.Console.WriteLine("The number of palette colors used in the bitmap: {0}", header.BitmapColorsUsed);
      System.Console.WriteLine("The bitmap compression: {0}", header.BitmapCompression);
      System.Console.WriteLine("The bitmap height: {0}", header.BitmapHeight);
      System.Console.WriteLine("The bitmap width: {0}", header.BitmapWidth);
      System.Console.WriteLine("The bitmap raw data size in bytes: {0}", header.BitmapImageSize);
      System.Console.WriteLine("The number of planes: {0}", header.BitmapPlanes);
      System.Console.WriteLine("The horizontal resolution of the bitmap, in pixels-per-meter: {0}", header.BitmapXPelsPerMeter);
      System.Console.WriteLine("The vertical resolution of the bitmap, in pixels-per-meter: {0}", header.BitmapYPelsPerMeter);
      System.Console.WriteLine("The number of bits per pixel: {0}", header.BitsPerPixel);
      System.Console.WriteLine("The extra bits masks: {0}", header.ExtraBitMasks);
      System.Console.WriteLine("The header size in bytes: {0}", header.HeaderSize);
   }

BitsPerPixel

Access the number of bits per pixel for the image with ease using this property.Perfect for developers seeking quick information about image quality and depth.

public override int BitsPerPixel
    {
        get;
    }

Property Value

int

Examples

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

The following example shows how the bitmap compression affects the output image size.

Aspose.Imaging.FileFormats.Bmp.BitmapCompression[] compressions = new []
   {
       Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb,
       Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rle8
   };
   Aspose.Imaging.Color[] paletterColors = new []
   {
       Aspose.Imaging.Color.Red,
       Aspose.Imaging.Color.Green
   };
   Aspose.Imaging.IColorPalette palette = new Aspose.Imaging.ColorPalette(paletterColors);
   foreach (var compression in compressions)
   {
       using (var bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 8, palette, compression, 0.0, 0.0))
       {
           using (Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage))
           {
               Aspose.Imaging.Brushes.SolidBrush redBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
               gr.FillRectangle(redBrush, bmpImage.Bounds);
               using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
               {
                   bmpImage.Save(stream);
                   Console.WriteLine("---------------------------------------------");
                   Console.WriteLine("The compression={0}", bmpImage.Compression);
                   Console.WriteLine("The number of bits per pixel={0}", bmpImage.BitsPerPixel);
                   Console.WriteLine("The image dimensions={0} x {1}", bmpImage.Width, bmpImage.Height);
                   Console.WriteLine("The raw line size={0}", bmpImage.RawLineSize);
                   Console.WriteLine("The output size in bytes={0}", stream.Length);
               }
           }
       }
   }

Compression

Retrieve the compression type used for the image effortlessly with this property.Perfect for developers needing to quickly access information about image compression.

public BitmapCompression Compression
   {
      get;
   }

Property Value

BitmapCompression

Examples

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

The following example shows how the bitmap compression affects the output image size.

using System.IO;
   using Aspose.Imaging;
   using Aspose.Imaging.FileFormats.Bmp;
   BitmapCompression[] compressions = new []
   {
       BitmapCompression.Rgb,
       BitmapCompression.Rle8
   };
   Color[] paletterColors = new []
   {
       Color.Red,
       Color.Green
   };
   IColorPalette palette = new ColorPalette(paletterColors);
   foreach (BitmapCompression compression in compressions)
   {
      using (BmpImage bmpImage = new BmpImage(100, 100, 8, palette, compression, 0.0, 0.0))
      {
         Graphics gr = new Graphics(bmpImage);
         SolidBrush redBrush = new SolidBrush(Color.Red);
         gr.FillRectangle(redBrush, bmpImage.Bounds);
         using (MemoryStream stream = new MemoryStream())
         {
            bmpImage.Save(stream);
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("The compression={0}", bmpImage.Compression);
            Console.WriteLine("The number of bits per pixel={0}", bmpImage.BitsPerPixel);
            Console.WriteLine("The image dimensions={0} x {1}", bmpImage.Width, bmpImage.Height);
            Console.WriteLine("The raw line size={0}", bmpImage.RawLineSize);
            Console.WriteLine("The output size in bytes={0}", stream.Length);
         }
      }
   }

FileFormat

Easily retrieve the file format value with this user-friendly property.Ideal for developers seeking quick access to information about the file format.

public override FileFormat GetFileFormat()
{
}

Property Value

FileFormat

Examples

The following example shows how to extract information about raw data format and alpha channel from a BMP image.

using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 32, null))
   {
       System.Console.WriteLine("FileFormat={0}, RawDataFormat={1}, HasAlpha={2}", bmpImage.FileFormat, bmpImage.RawDataFormat, bmpImage.HasAlpha);
   }
   using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 24, null))
   {
       System.Console.WriteLine("FileFormat={0}, RawDataFormat={1}, HasAlpha={2}", bmpImage.FileFormat, bmpImage.RawDataFormat, bmpImage.HasAlpha);
   }

HasAlpha

Gets a value indicating whether this instance has alpha.

public override bool HasAlpha
{
    get;
}

Property Value

bool

Height

Retrieve the height of the image effortlessly with this property. Ideal for developersneeding quick access to information about image dimensions.

public override int Height
   {
      get;
   }

Property Value

int

Examples

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
      Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
      System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
      System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
      System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
      System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
      System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
      System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
      double hres = bmpImage.HorizontalResolution;
      double vres = bmpImage.VerticalResolution;
      System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
      System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
      if (hres != 96.0 || vres != 96.0)
      {
         System.Console.WriteLine("Set resolution values to 96 dpi");
         bmpImage.SetResolution(96.0, 96.0);
         System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
         System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
      }
   }

The following example shows how the bitmap compression affects the output image size.

using System.IO;
   Aspose.Imaging.FileFormats.Bmp.BitmapCompression[] compressions = new []
   {
       Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb,
       Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rle8
   };
   Aspose.Imaging.Color[] paletterColors = new []
   {
       Aspose.Imaging.Color.Red,
       Aspose.Imaging.Color.Green
   };
   var palette = new Aspose.Imaging.ColorPalette(paletterColors);
   foreach (var compression in compressions)
   {
       using (var bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 8, palette, compression, 0.0, 0.0))
       {
           var gr = new Aspose.Imaging.Graphics(bmpImage);
           var redBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
           gr.FillRectangle(redBrush, bmpImage.Bounds);
           using (var stream = new MemoryStream())
           {
               bmpImage.Save(stream);
               System.Console.WriteLine("---------------------------------------------");
               System.Console.WriteLine("The compression={0}", bmpImage.Compression);
               System.Console.WriteLine("The number of bits per pixel={0}", bmpImage.BitsPerPixel);
               System.Console.WriteLine("The image dimensions={0} x {1}", bmpImage.Width, bmpImage.Height);
               System.Console.WriteLine("The raw line size={0}", bmpImage.RawLineSize);
               System.Console.WriteLine("The output size in bytes={0}", stream.Length);
           }
       }
   }

HorizontalResolution

This property allows you to easily get or set the horizontal resolution,measured in pixels per inch, of the Aspose.Imaging.RasterImage object. Ideal fordevelopers needing precise control over image resolution for their applications.

public override double HorizontalResolution
{
    get;
    set;
}

Property Value

double

Examples

The following example shows how to set horizontal/vertical resolution of a BMP image.

string dir = "c:\\temp\\";
   using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
   {
       var bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       double horizontalResolution = bmpImage.HorizontalResolution;
       double verticalResolution = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", horizontalResolution);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", verticalResolution);
       if (horizontalResolution != 96.0 || verticalResolution != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

Remarks

Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.

RawDataFormat

Easily obtain the format of your raw data with this user-friendly function.Perfect for developers looking to quickly access crucial information about their data format.

public override PixelDataFormat RawDataFormat
{
    get;
}

Property Value

PixelDataFormat

Examples

The following example shows how to extract information about raw data format and alpha channel from a BMP image.

using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 32, null))
   {
       System.Console.WriteLine("FileFormat={0}, RawDataFormat={1}, HasAlpha={2}", bmpImage.FileFormat, bmpImage.RawDataFormat, bmpImage.HasAlpha);
   }
   using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 24, null))
   {
       System.Console.WriteLine("FileFormat={0}, RawDataFormat={1}, HasAlpha={2}", bmpImage.FileFormat, bmpImage.RawDataFormat, bmpImage.HasAlpha);
   }

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

RawLineSize

Quickly access the size of each raw line in bytes with this straightforward property.Ideal for developers needing to efficiently handle raw image data.

public override int RawLineSize
    {
        get;
    }

Property Value

int

Examples

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

The following example shows how the bitmap compression affects the output image size.

Aspose.Imaging.FileFormats.Bmp.BitmapCompression[] compressions = new Aspose.Imaging.FileFormats.Bmp.BitmapCompression[]
   {
      Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb,
      Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rle8
   };
   Aspose.Imaging.Color[] paletterColors = new Aspose.Imaging.Color[]
   {
      Aspose.Imaging.Color.Red,
      Aspose.Imaging.Color.Green
   };
   Aspose.Imaging.IColorPalette palette = new Aspose.Imaging.ColorPalette(paletterColors);
   foreach (Aspose.Imaging.FileFormats.Bmp.BitmapCompression compression in compressions)
   {
      using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 8, palette, compression, 0.0, 0.0))
      {
         Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
         Aspose.Imaging.Brushes.SolidBrush redBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
         gr.FillRectangle(redBrush, bmpImage.Bounds);
         using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
         {
            bmpImage.Save(stream);
            Console.WriteLine("---------------------------------------------");
            Console.WriteLine("The compression={0}", bmpImage.Compression);
            Console.WriteLine("The number of bits per pixel={0}", bmpImage.BitsPerPixel);
            Console.WriteLine("The image dimensions={0} x {1}", bmpImage.Width, bmpImage.Height);
            Console.WriteLine("The raw line size={0}", bmpImage.RawLineSize);
            Console.WriteLine("The output size in bytes={0}", stream.Length);
         }
      }
   }

VerticalResolution

Easily retrieve or set the vertical resolution, measured in pixels per inch,of this Aspose.Imaging.RasterImage object with this property. Perfect for developers requiringprecise control over image resolution in their applications.

public override double VerticalResolution
    {
        get;
        set;
    }

Property Value

double

Examples

The following example shows how to set horizontal/vertical resolution of a BMP image.

string dir = "c:\\temp\\";
   using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       double horizontalResolution = bmpImage.HorizontalResolution;
       double verticalResolution = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", horizontalResolution);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", verticalResolution);
       if (horizontalResolution != 96.0 || verticalResolution != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

Remarks

Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.

Width

Access the width of the image easily with this property. Ideal for developersseeking quick information about the image dimensions.

public override int Width
   {
      get;
   }

Property Value

int

Examples

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

The following example shows how the bitmap compression affects the output image size.

Aspose.Imaging.FileFormats.Bmp.BitmapCompression[] compressions = new Aspose.Imaging.FileFormats.Bmp.BitmapCompression[]
   {
       Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb,
       Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rle8
   };
   Aspose.Imaging.Color[] paletterColors = new Aspose.Imaging.Color[]
   {
       Aspose.Imaging.Color.Red,
       Aspose.Imaging.Color.Green
   };
   Aspose.Imaging.IColorPalette palette = new Aspose.Imaging.ColorPalette(paletterColors);
   foreach (Aspose.Imaging.FileFormats.Bmp.BitmapCompression compression in compressions)
   {
       using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100, 8, palette, compression, 0.0, 0.0))
       {
           Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
           Aspose.Imaging.Brushes.SolidBrush redBrush = new Aspose.Imaging.Brushes.SolidBrush(Aspose.Imaging.Color.Red);
           gr.FillRectangle(redBrush, bmpImage.Bounds);
           using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
           {
               bmpImage.Save(stream);
               System.Console.WriteLine("---------------------------------------------");
               System.Console.WriteLine("The compression={0}", bmpImage.Compression);
               System.Console.WriteLine("The number of bits per pixel={0}", bmpImage.BitsPerPixel);
               System.Console.WriteLine("The image dimensions={0} x {1}", bmpImage.Width, bmpImage.Height);
               System.Console.WriteLine("The raw line size={0}", bmpImage.RawLineSize);
               System.Console.WriteLine("The output size in bytes={0}", stream.Length);
           }
       }
   }

Methods

GetDefaultOptions(object[])

Retrieve the default options effortlessly with this straightforward method.Ideal for developers seeking quick access to default image settings or configurations.

public override ImageOptionsBase GetDefaultOptions(object[] args)
    {
    }
Here's a more complex example with multiple lines and methods:
using System;
using System.Collections.Generic;
using Aspose.Words;
namespace MyNamespace
{
    public class MyClass
    {
        private readonly List<string> _myList = new List<string>();
        public void MyMethod(Document document)
        {
        }
        public override ImageOptionsBase GetDefaultOptions(object[] args)
        {
            var defaultImageOptions = base.GetDefaultOptions(args);
            defaultImageOptions.BackgroundColor = Color.White;
            defaultImageOptions.ResizePageWidth = ResizePageWidthOption.NoChange;
            return defaultImageOptions;
        }
    }
}

Parameters

args object []

The arguments.

Returns

ImageOptionsBase

Default options

OnCached()

Called when data is cached and all related resources may be de-allocated.

protected override void OnCached()
   {
   }

OnPaletteChanging(IColorPalette, IColorPalette)

Called when palette is changing.

protected override void OnPaletteChanging(
       IColorPalette oldPalette,
       IColorPalette newPalette)
   {
   }

Parameters

oldPalette IColorPalette

The old palette.

newPalette IColorPalette

The new palette.

SaveData(Stream)

Saves the data.

protected override void SaveData(Stream stream)
   {
   }

Parameters

stream Stream

The stream to save data to.

SetResolution(double, double)

Adjust the resolution of your Aspose.Imaging.RasterImage effortlessly with thisuser-friendly method. Perfect for developers seeking precise control overimage resolution in their applications.

public override void
    SetResolution(double dpiX, double dpiY)
{
}

Parameters

dpiX double

The horizontal resolution, in dots per inch, of the Aspose.Imaging.RasterImage.

dpiY double

The vertical resolution, in dots per inch, of the Aspose.Imaging.RasterImage.

Examples

The following example shows how to set horizontal/vertical resolution of a BMP image.

string dir = "c:\\temp\\";
   using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       double horizontalResolution = bmpImage.HorizontalResolution;
       double verticalResolution = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", horizontalResolution);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", verticalResolution);
       if (horizontalResolution != 96.0 || verticalResolution != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

The following example gets the general information about the image including pixel format, image size, resolution, compression etc.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Console.WriteLine("The pixel format: {0}", bmpImage.RawDataFormat);
       System.Console.WriteLine("The raw line size in bytes: {0}", bmpImage.RawLineSize);
       System.Console.WriteLine("The bitmap compression: {0}", bmpImage.Compression);
       System.Console.WriteLine("The bitmap width: {0}", bmpImage.Width);
       System.Console.WriteLine("The bitmap height: {0}", bmpImage.Height);
       System.Console.WriteLine("The number of bits per pixel: {0}", bmpImage.BitsPerPixel);
       double hres = bmpImage.HorizontalResolution;
       double vres = bmpImage.VerticalResolution;
       System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", hres);
       System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", vres);
       if (hres != 96.0 || vres != 96.0)
       {
           System.Console.WriteLine("Set resolution values to 96 dpi");
           bmpImage.SetResolution(96.0, 96.0);
           System.Console.WriteLine("The horizontal resolution, in pixels per inch: {0}", bmpImage.HorizontalResolution);
           System.Console.WriteLine("The vertical resolution, in pixels per inch: {0}", bmpImage.VerticalResolution);
       }
   }

ToBitmap()

Easily convert your raster image to a bitmap with this simple method.Perfect for developers needing to seamlessly transition between different image formats.

public override Bitmap ToBitmap()
{
}

Returns

Bitmap

The bitmap

Examples

The following example converts a BMP image to a GDI bitmap.

using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(@"c:\temp\sample.bmp"))
   {
       Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = (Aspose.Imaging.FileFormats.Bmp.BmpImage)image;
       System.Drawing.Bitmap bitmap = bmpImage.ToBitmap();
   }

UpdateDimensions(int, int)

Updates the image dimensions.

protected override void UpdateDimensions(
      int newWidth,
      int newHeight
   )
   {
   }

Parameters

newWidth int

The new image width.

newHeight int

The new image height.

 English