Class BmpOptions
Namespace: Aspose.Imaging.ImageOptions
Assembly: Aspose.Imaging.dll (25.2.0)
The API for BMP and DIB raster image format creation options provides developers with a versatile toolset for generating custom Bitmap (BMP) and Device Independent Bitmap (DIB) images. With this API, you can precisely define image characteristics such as bits per pixel, compression level and compression type, tailoring the output to meet specific requirements. This feature-rich API empowers developers to create high-quality, customized raster images with ease and flexibility for diverse applications.
public class BmpOptions : ImageOptionsBase, IDisposable, IHasXmpData, IHasMetadata, ICloneable
Inheritance
object ← DisposableObject ← ImageOptionsBase ← BmpOptions
Implements
IDisposable, IHasXmpData, IHasMetadata, ICloneable
Inherited Members
ImageOptionsBase.Clone(), ImageOptionsBase.ReleaseManagedResources(), ImageOptionsBase.KeepMetadata, ImageOptionsBase.XmpData, ImageOptionsBase.Source, ImageOptionsBase.Palette, ImageOptionsBase.ResolutionSettings, ImageOptionsBase.VectorRasterizationOptions, ImageOptionsBase.BufferSizeHint, ImageOptionsBase.MultiPageOptions, ImageOptionsBase.FullFrame, ImageOptionsBase.ProgressEventHandler, DisposableObject.Dispose(), DisposableObject.ReleaseManagedResources(), DisposableObject.ReleaseUnmanagedResources(), DisposableObject.VerifyNotDisposed(), DisposableObject.Disposed, object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Examples
This example creates a new Image file at some disk location as specified by Source property of the BmpOptions instance. Several properties for BmpOptions instance are set before creating the actual image. Especially the Source property, that refers to the actual disk location in this case.```csharp [C#]
//Create an instance of BmpOptions and set its various properties
Aspose.Imaging.ImageOptions.BmpOptions bmpOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
bmpOptions.BitsPerPixel = 24;
//Create an instance of FileCreateSource and assign it as Source for the instance of BmpOptions
//Second Boolean parameter determines if the file to be created IsTemporal or not
bmpOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(@"C:\temp\output.bmp", false);
//Create an instance of Image and initialize it with instance of BmpOptions by calling Create method
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(bmpOptions, 500, 500))
{
//do some image processing
// save all changes
image.Save();
}
This example demonstrates the use of different classes from SaveOptions Namespace for export purposes. An image of type Gif is loaded into an instance of Image and then exported out to several formats.```csharp
[C#]
string dir = "c:\\temp\\";
//Load an existing image (of type Gif) in an instance of Image class
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.gif"))
{
//Export to BMP file format using the default options
image.Save(dir + "output.bmp", new Aspose.Imaging.ImageOptions.BmpOptions());
//Export to JPEG file format using the default options
image.Save(dir + "output.jpg", new Aspose.Imaging.ImageOptions.JpegOptions());
//Export to PNG file format using the default options
image.Save(dir + "output.png", new Aspose.Imaging.ImageOptions.PngOptions());
//Export to TIFF file format using the default options
image.Save(dir + "output.tif", new Aspose.Imaging.ImageOptions.TiffOptions(Aspose.Imaging.FileFormats.Tiff.Enums.TiffExpectedFormat.Default));
}
The following example shows how to convert a multipage vector image to BMP format in general way without referencing to a particular image type.```csharp [C#]
string dir = "C:\\aspose.imaging\\net\\misc\\ImagingReleaseQATester\\Tests\\testdata\\2548";
string inputFilePath = System.IO.Path.Combine(dir, "Multipage.cdr");
string outputFilePath = System.IO.Path.Combine(dir, "Multipage.cdr.bmp");
Aspose.Imaging.ImageOptionsBase exportOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(inputFilePath))
{
exportOptions.MultiPageOptions = null;
// Export only first two pages. In fact, only one page will be rasterized because BMP is not a multi-page format.
Aspose.Imaging.IMultipageImage multipageImage = image as Aspose.Imaging.IMultipageImage;
if (multipageImage != null && (multipageImage.Pages != null && multipageImage.PageCount > 2))
{
exportOptions.MultiPageOptions = new Aspose.Imaging.ImageOptions.MultiPageOptions(new Aspose.Imaging.IntRange(0, 2));
}
if (image is Aspose.Imaging.VectorImage)
{
exportOptions.VectorRasterizationOptions = (Aspose.Imaging.ImageOptions.VectorRasterizationOptions)image.GetDefaultOptions(new object[] { Aspose.Imaging.Color.White, image.Width, image.Height });
exportOptions.VectorRasterizationOptions.TextRenderingHint = Aspose.Imaging.TextRenderingHint.SingleBitPerPixel;
exportOptions.VectorRasterizationOptions.SmoothingMode = Aspose.Imaging.SmoothingMode.None;
}
image.Save(outputFilePath, exportOptions);
}
## Constructors
### <a id="Aspose_Imaging_ImageOptions_BmpOptions__ctor"></a> BmpOptions\(\)
Initializes a new instance of the Aspose.Imaging.ImageOptions.BmpOptions class.
```csharp
public BmpOptions()
Examples
The following example loads a BMP image and saves it back to BMP using various save options.```csharp [C#]
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;
// Create BmpOptions
Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Use 8 bits per pixel to reduce the size of the output image.
saveOptions.BitsPerPixel = 8;
// Set the closest 8-bit color palette which covers the maximal number of image pixels, so that a palettized image
// is almost visually indistinguishable from a non-palletized one.
saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(rasterImage, 256);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
saveOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
saveOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
image.Save(dir + "sample.bmpoptions.bmp", saveOptions);
}
The following example creates a palettized grayscale BMP image and then saves it to a file.```csharp
[C#]
string dir = "c:\\temp\\";
Aspose.Imaging.ImageOptions.BmpOptions createOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Save to a file
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(dir + "output.palette8bit.bmp", false);
// Use 8 bits per pixel to reduce the size of the output image.
createOptions.BitsPerPixel = 8;
// Set the standard 8-bit grayscale color palette which covers all grayscale colors.
// If the processed image contains only grayscale colors, then its palettized version
// is visually indistinguishable from a non-palletized one.
createOptions.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(false);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
createOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
createOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
// Create a BMP image of 100 x 100 px and save it to a file.
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(createOptions, 100, 100))
{
Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);
Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
new Aspose.Imaging.Point(0, 0),
new Aspose.Imaging.Point(image.Width, image.Height),
Aspose.Imaging.Color.Black,
Aspose.Imaging.Color.White);
// Fill the image with a grayscale gradient
graphics.FillRectangle(gradientBrush, image.Bounds);
image.Save();
}
BmpOptions(BmpOptions)
Initializes a new instance of the Aspose.Imaging.ImageOptions.BmpOptions class.
public BmpOptions(BmpOptions bmpOptions)
Parameters
bmpOptions
BmpOptions
The BMP options.
Properties
BitsPerPixel
Gets or sets the image bits per pixel count.
public int BitsPerPixel { get; set; }
Property Value
Examples
The following example loads a BMP image and saves it back to BMP using various save options.```csharp [C#]
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;
// Create BmpOptions
Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Use 8 bits per pixel to reduce the size of the output image.
saveOptions.BitsPerPixel = 8;
// Set the closest 8-bit color palette which covers the maximal number of image pixels, so that a palettized image
// is almost visually indistinguishable from a non-palletized one.
saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(rasterImage, 256);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
saveOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
saveOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
image.Save(dir + "sample.bmpoptions.bmp", saveOptions);
}
The following example creates a palettized grayscale BMP image and then saves it to a file.```csharp
[C#]
string dir = "c:\\temp\\";
Aspose.Imaging.ImageOptions.BmpOptions createOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Save to a file
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(dir + "output.palette8bit.bmp", false);
// Use 8 bits per pixel to reduce the size of the output image.
createOptions.BitsPerPixel = 8;
// Set the standard 8-bit grayscale color palette which covers all grayscale colors.
// If the processed image contains only grayscale colors, then its palettized version
// is visually indistinguishable from a non-palletized one.
createOptions.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(false);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
createOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
createOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
// Create a BMP image of 100 x 100 px and save it to a file.
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(createOptions, 100, 100))
{
Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);
Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
new Aspose.Imaging.Point(0, 0),
new Aspose.Imaging.Point(image.Width, image.Height),
Aspose.Imaging.Color.Black,
Aspose.Imaging.Color.White);
// Fill the image with a grayscale gradient
graphics.FillRectangle(gradientBrush, image.Bounds);
image.Save();
}
The following example shows how to palletize a BMP image to reduce its output size.```csharp [C#]
// Create a BMP image 100 x 100 px.
using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
{
// The linear gradient from the left-top to the right-bottom corner of the image.
Aspose.Imaging.Brushes.LinearGradientBrush brush =
new Aspose.Imaging.Brushes.LinearGradientBrush(
new Aspose.Imaging.Point(0, 0),
new Aspose.Imaging.Point(bmpImage.Width, bmpImage.Height),
Aspose.Imaging.Color.Red,
Aspose.Imaging.Color.Green);
// Fill the entire image with the linear gradient brush.
Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
gr.FillRectangle(brush, bmpImage.Bounds);
// Get the closest 8-bit color palette which covers as many pixels as possible, so that a palettized image
// is almost visually indistinguishable from a non-palletized one.
Aspose.Imaging.IColorPalette palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(bmpImage, 256);
// 8-bit palette contains at most 256 colors.
Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
saveOptions.Palette = palette;
saveOptions.BitsPerPixel = 8;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
bmpImage.Save(stream, saveOptions);
System.Console.WriteLine("The palettized image size is {0} bytes.", stream.Length);
}
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
bmpImage.Save(stream);
System.Console.WriteLine("The non-palettized image size is {0} bytes.", stream.Length);
}
}
// The output looks like this:
// The palettized image size is 11078 bytes.
// The non-palettized image size is 40054 bytes.
### <a id="Aspose_Imaging_ImageOptions_BmpOptions_Compression"></a> Compression
Gets or sets the compression type. The default compression type is Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Bitfields, that allows saving a Aspose.Imaging.FileFormats.Bmp.BmpImage with transparency.
```csharp
public BitmapCompression Compression { get; set; }
Property Value
Examples
Decompress BMP image which was previously compressed using DXT1 compression algorithm.```csharp [C#]
using (var image = Image.Load("CompressedTiger.bmp"))
{
image.Save("DecompressedTiger.bmp", new BmpOptions());
}
Compress BMP image using DXT1 compression algorithm.```csharp
[C#]
using (var image = Image.Load("Tiger.bmp"))
{
image.Save("CompressedTiger.bmp", new BmpOptions { Compression = BitmapCompression.Dxt1 });
}
The example shows how to export a BmpImage with the Rgb compression type.```csharp [C#]
string sourcePath = "input.png";
// Load a PNG image from a file.
using (Image pngImage = Image.Load(sourcePath))
{
// BMP image is saved with transparency support by default, that is achieved by using the BitmapCompression.Bitfields compression method.
// To save a BMP image with the Rgb compression method, the BmpOptions with the Compression property set to BitmapCompression.Rgb should be specified.
pngImage.Save(outputPath, new BmpOptions() { Compression = BitmapCompression.Rgb });
}
The example shows how to export a BmpImage from a Png file while keeping the alpha channel, save a Bmp file with transparency.```csharp
[C#]
string sourcePath = "input.png";
// Load a PNG image from a file.
using (Image pngImage = Image.Load(sourcePath))
{
// BMP image is saved with transparency support by default.
// If you want to explicitly specify such mode, the BmpOptions's Compression property should be set to BitmapCompression.Bitfields.
// The BitmapCompression.Bitfields compression method is the default compression method in the BmpOptions.
// So the same result of exporting a Bmp image with transparency can be achieved by either one of the following ways.
// With an implicit default options:
pngImage.Save(outputPath);
// With an explicit default options:
pngImage.Save(outputPath, new BmpOptions());
// Specifying the BitmapCompression.Bitfields compression method:
pngImage.Save(outputPath, new BmpOptions() { Compression = BitmapCompression.Bitfields });
}
The following example loads a BMP image and saves it back to BMP using various save options.```csharp [C#]
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;
// Create BmpOptions
Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Use 8 bits per pixel to reduce the size of the output image.
saveOptions.BitsPerPixel = 8;
// Set the closest 8-bit color palette which covers the maximal number of image pixels, so that a palettized image
// is almost visually indistinguishable from a non-palletized one.
saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(rasterImage, 256);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
saveOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
saveOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
image.Save(dir + "sample.bmpoptions.bmp", saveOptions);
}
The following example creates a palettized grayscale BMP image and then saves it to a file.```csharp
[C#]
string dir = "c:\\temp\\";
Aspose.Imaging.ImageOptions.BmpOptions createOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Save to a file
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(dir + "output.palette8bit.bmp", false);
// Use 8 bits per pixel to reduce the size of the output image.
createOptions.BitsPerPixel = 8;
// Set the standard 8-bit grayscale color palette which covers all grayscale colors.
// If the processed image contains only grayscale colors, then its palettized version
// is visually indistinguishable from a non-palletized one.
createOptions.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(false);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
createOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
createOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
// Create a BMP image of 100 x 100 px and save it to a file.
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(createOptions, 100, 100))
{
Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);
Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
new Aspose.Imaging.Point(0, 0),
new Aspose.Imaging.Point(image.Width, image.Height),
Aspose.Imaging.Color.Black,
Aspose.Imaging.Color.White);
// Fill the image with a grayscale gradient
graphics.FillRectangle(gradientBrush, image.Bounds);
image.Save();
}