ImageExtensionsSaveAsBmp(Image, Stream) Method

Saves the specified image into a stream using the built-in BMP encoder if available in the current operating system. Unlike the Save(Stream, ImageFormat) method, this one supports every PixelFormat.

Definition

Namespace: KGySoft.Drawing
Assembly: KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 8.1.0
C#
public static void SaveAsBmp(
	this Image image,
	Stream stream
)

Parameters

image  Image
The image to save. If contains multiple images, then only the current frame will be saved.
stream  Stream
The stream to save the image into.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Image. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

The image can only be saved if a built-in BMP encoder is available in the current operating system.

The saved BMP image is never RLE compressed.

The BMP format supports transparency only for the 64 BPP formats but the Windows BMP encoder stores alpha information also for the 32 BPP formats, which can be restored (see also the example below).

Images with different PixelFormats are handled as follows (on Windows, unless specified otherwise):

Format1bppIndexed
The pixel format is preserved, though palette entries with alpha are turned opaque.
Format4bppIndexed
The pixel format is preserved, though palette entries with alpha are turned opaque.
Format8bppIndexed
The pixel format is preserved, though palette entries with alpha are turned opaque.
Format16bppGrayScale
Before saving the image pixel format will be converted to Format8bppIndexed using a grayscale palette, because otherwise GDI+ would throw an exception.
Format16bppRgb555
Before saving the image pixel format will be converted to Format24bppRgb because the built-in encoder would save a 32 BPP image otherwise, which is just a waste of space.
Format16bppRgb565
Before saving the image pixel format will be converted to Format24bppRgb because the built-in encoder would save a 32 BPP image otherwise, which is just a waste of space.
Format16bppArgb1555
Before saving the image pixel format will be converted to Format32bppArgb. Though reloading such an image will not have transparency but it can be restored (see also the example below).
Format24bppRgb
When reloading the saved image the pixel format is preserved.
Format32bppRgb
When reloading the saved image the pixel format is preserved.
Format32bppArgb
When the saved image is reloaded by the built-in decoder the pixel format will be Format32bppRgb and the image will have no transparency. Actually alpha information is preserved and can be restored (see the example below).
Format32bppPArgb
When the saved image is reloaded by the built-in decoder, the pixel format will be Format32bppRgb and the image will have no transparency. Actually alpha information preserved and can be restored (see the example below).
Format48bppRgb
When reloading the saved image the pixel format will turn Format24bppRgb.
Format64bppArgb
When reloading the saved image the pixel format is preserved. Note that not every application supports or handles BMP format with 64 BPP correctly.
Format64bppPArgb
When reloading the saved image the pixel format will turn Format64bppArgb.

Example

The following example demonstrates how to restore transparency from 32 BPP bitmaps saved by the SaveAsBmp method:
C#
// this is a 32 BPP ARGB bitmap with transparency:
Bitmap toSave = Icons.Information.ExtractBitmap(new Size(256, 256));
Bitmap reloaded;

// Saving and reloading the transparent image as BMP:
using (var stream = new MemoryStream())
{
    bmp.SaveAsBmp(stream);
    stream.Position = 0;

    // realoaded Bitmap has now Format32bppRgb PixelFormat without transparency
    reloaded = new Bitmap(stream);
}

// Restoring transparency by using fast bitmap data accessors (not needed for 64 BPP images):
Bitmap restored = new Bitmap(reloaded.Width, reloaded.Height, PixelFormat.Format32bppArgb);
using (IReadableBitmapData dataSrc = reloaded.GetReadableBitmapData())
using (IWritableBitmapData dataDst = restored.GetWritableBitmapData())
{
    IReadableBitmapDataRow rowSrc = dataSrc.FirstRow;
    IWritableBitmapDataRow rowDst = dataDst.FirstRow;
    do
    {
        for (int x = 0; x < dataSrc.Width; x++)
        {
            // Note 1: If we used the indexer instead, then the source color would never be transparent.
            // Note 2: We can use any type of the same size so int/uint types would also do the trick.
            rowDst.WriteRaw(x, rowSrc.ReadRaw<Color32>(x));
        }
    } while (rowSrc.MoveNextRow() && rowDst.MoveNextRow());
}

Exceptions

ArgumentNullExceptionimage or stream is .
InvalidOperationExceptionNo built-in encoder was found or the saving fails in the current operating system.

See Also