ImageExtensionsSaveAsJpeg(Image, Stream, Int32) Method

Saves the specified image using the built-in JPEG 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: 10.0.0-rc.1
C#
public static void SaveAsJpeg(
	this Image image,
	Stream stream,
	int quality = 90
)

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.
quality  Int32  (Optional)
An integer between 0 and 100 that determines the quality of the saved image. Higher value means better quality as well as bigger size. This parameter is optional.
Default value: 90.

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 JPEG encoder is available in the current operating system.

The saved JPEG image is will have always 24 BPP format.

The JPEG format uses a lossy compression (even using the best quality) and does not support transparency for any PixelFormat.

Transparent pixels will be black in the saved image. To use another background color use the MakeOpaque or ConvertPixelFormat methods before saving (see also the example below).

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

Format1bppIndexed
When reloading the saved image the pixel format will turn Format24bppRgb. Transparency will be lost.
Format4bppIndexed
When reloading the saved image the pixel format will turn Format24bppRgb. Transparency will be lost.
Format8bppIndexed
When reloading the saved image the pixel format will turn Format24bppRgb. Transparency will be lost.
Format16bppGrayScale
Before saving the image pixel format will be converted to Format24bppRgb because otherwise GDI+ would throw an exception.
Format16bppRgb555
On Windows, when reloading the saved image the pixel format will turn Format24bppRgb. On Linux, before saving the image pixel format will be converted to Format24bppRgb; otherwise, saving would fail.
Format16bppRgb565
On Windows, when reloading the saved image the pixel format will turn Format24bppRgb. On Linux, before saving the image pixel format will be converted to Format24bppRgb; otherwise, saving would fail.
Format16bppArgb1555
Before saving the image pixel format will be converted to Format24bppRgb; otherwise, the built-in encoder may save transparent pixels with nonzero color information incorrectly. Transparency will be lost.
Format24bppRgb
When reloading the saved image the pixel format is preserved.
Format32bppRgb
When reloading the saved image the pixel format will turn Format24bppRgb.
Format32bppArgb
Before saving the image pixel format will be converted to Format24bppRgb; otherwise, the built-in encoder may save pixels with alpha incorrectly. Transparency will be lost.
Format32bppPArgb
When reloading the saved image the pixel format will turn Format24bppRgb. Transparency will be lost.
Format48bppRgb
When reloading the saved image the pixel format will turn Format24bppRgb.
Format64bppArgb
Before saving the image pixel format will be converted to Format24bppRgb; otherwise, the built-in encoder may save pixels with alpha incorrectly. Transparency will be lost.
Format64bppPArgb
Before saving the image pixel format will be converted to Format24bppRgb; otherwise, the built-in encoder may save pixels with alpha incorrectly. Transparency will be lost.

Examples

The following example demonstrates how to save an image with custom background color using the SaveAsJpeg method:
C#
// this is a 32 BPP ARGB bitmap with transparency:
using Bitmap origBmp = Icons.Information.ExtractBitmap(new Size(256, 256));

// Turning the background white before saving (it would turn black otherwise):
using Bitmap toSave = origBmp.ConvertPixelFormat(PixelFormat.Format24bppRgb, Color.White);
// Or: origBmp.MakeOpaque(Color.White); // changes the original image instead of returning a new one

// Saving the image with the white background:
toSave.SaveAsJpeg(File.Create(@"C:\myimage.jpg"))

Exceptions

ArgumentNullExceptionimage or stream is .
InvalidOperationExceptionNo built-in encoder was found or the saving fails in the current operating system.
ArgumentOutOfRangeExceptionquality must be between 0 and 100.

See Also