ImageExtensionsSaveAsPng(Image, Stream) Method

Saves the specified image using the built-in PNG 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 SaveAsPng(
	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 PNG encoder is available in the current operating system.

The saved PNG image will have 32 BPP format if the source image can have transparency; otherwise, it will have 24 BPP format.

On Windows PNG is never saved with indexed format.

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

Format1bppIndexed
On Windows, when reloading the saved image the pixel format will turn Format32bppArgb. On Linux, when reloading the saved image the pixel format is preserved, though transparency will be lost.
Format4bppIndexed
On Windows, when reloading the saved image the pixel format will turn Format32bppArgb. On Linux, when reloading the saved image the pixel format is preserved, though transparency will be lost.
Format8bppIndexed
On Windows, when reloading the saved image the pixel format will turn Format32bppArgb. On Linux, when reloading the saved image the pixel format is preserved, though transparency will be lost.
Format16bppGrayScale
Before saving the image pixel format will be converted to Format24bppRgb 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
When reloading the saved image the pixel format will turn Format32bppArgb.
Format24bppRgb
When reloading the saved image the pixel format is preserved.
Format32bppRgb
When reloading the saved image the pixel format will turn Format32bppArgb.
Format32bppArgb
When reloading the saved image the pixel format is preserved.
Format32bppPArgb
On Windows, when reloading the saved image the pixel format will turn Format32bppArgb. On Linux, before saving the image pixel format will be converted to Format32bppArgb; otherwise, the alpha channel in the saved image would be corrupted.
Format48bppRgb
Before saving the image pixel format will be converted to Format24bppRgb because the built-in encoder would save a 32 BPP image with incorrect colors otherwise.
Format64bppArgb
Before saving the image pixel format will be converted to Format32bppArgb because the built-in encoder would save the image incorrectly otherwise.
Format64bppPArgb
Before saving the image pixel format will be converted to Format32bppArgb because the built-in encoder would save the image incorrectly otherwise.

Exceptions

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

See Also