ImageHelper

Ecng.Drawing

Platform-independent image utilities implemented entirely in managed C#. No GDI+, platform-native image libraries, or third-party NuGet packages are used.

Methods

AddTextWatermark
public static byte[] AddTextWatermark(byte[] data, string text, ImageFormats outputFormat, WebpEncodingOptions webpOptions, float fontSize, byte opacity, int margin, string fontFamily, int jpegQuality)
result = ImageHelper.AddTextWatermark(data, text, outputFormat, webpOptions, fontSize, opacity, margin, fontFamily, jpegQuality)

Renders a watermark with explicit WebP mode and quality.

data
Encoded source image or animation.
text
Single-line watermark text.
outputFormat
Requested output format.
webpOptions
WebP mode and quality.
fontSize
Positive maximum font size.
opacity
Text opacity, 0 through 255.
margin
Nonnegative margin.
fontFamily
Font family, including the bundled DejaVu Sans.
jpegQuality
JPEG quality, 1 through 100.

Returns: Encoded output retaining all animation frames.

AddTextWatermark
public static byte[] AddTextWatermark(byte[] data, string text, ImageProcessingLimits limits, float fontSize, byte opacity, int margin, string fontFamily, int jpegQuality)
result = ImageHelper.AddTextWatermark(data, text, limits, fontSize, opacity, margin, fontFamily, jpegQuality)

Renders a text watermark under explicit image and animation limits.

data
Encoded source image.
text
Single-line text.
limits
Limits on decoded images and animation work.
fontSize
Positive maximum font size.
opacity
Text opacity, 0 through 255.
margin
Nonnegative margin.
fontFamily
Installed font family or the built-in DejaVu Sans.
jpegQuality
JPEG encoding quality, 1 through 100.

Returns: Encoded output retaining animation frames and timing.

AddTextWatermark
public static byte[] AddTextWatermark(byte[] data, string text, byte[] font, ImageFormats outputFormat, float fontSize, byte opacity, int margin, int jpegQuality)
result = ImageHelper.AddTextWatermark(data, text, font, outputFormat, fontSize, opacity, margin, jpegQuality)

Renders a watermark with supplied font bytes and writes the requested image format.

data
The source image.
text
Single-line watermark text.
font
TrueType font bytes; not read when opacity is zero.
outputFormat
PNG, JPEG, BMP, GIF, WebP or ICO.
fontSize
Maximum font size.
opacity
Text opacity from 0 to 255.
margin
Margin around the text.
jpegQuality
JPEG quality from 1 to 100.

Returns: The image in the requested format, retaining all animation frames.

AddTextWatermark
public static byte[] AddTextWatermark(byte[] data, string text, ImageFormats outputFormat, float fontSize, byte opacity, int margin, string fontFamily, string fontFilePath, int jpegQuality)
result = ImageHelper.AddTextWatermark(data, text, outputFormat, fontSize, opacity, margin, fontFamily, fontFilePath, jpegQuality)

Renders a watermark and writes the requested image format.

data
The source image.
text
Single-line watermark text.
outputFormat
PNG, JPEG, BMP, GIF, WebP or ICO.
fontSize
Maximum font size.
opacity
Text opacity from 0 to 255.
margin
Margin around the text.
fontFamily
Installed font family.
fontFilePath
Optional TrueType font file.
jpegQuality
JPEG quality from 1 to 100.

Returns: The image in the requested format, retaining all animation frames.

AddTextWatermark
public static byte[] AddTextWatermark(byte[] data, string text, byte[] font, float fontSize, byte opacity, int margin, int jpegQuality)
result = ImageHelper.AddTextWatermark(data, text, font, fontSize, opacity, margin, jpegQuality)

Renders a text watermark with a font given as the bytes of its file - one an application keeps in its resources, for instance - where the other overload looks for a font by family or by path. The text is placed, made smaller and left out the same way.

data
A PNG, JPEG, BMP, GIF, WebP, ICO, TIFF or AVIF picture.
text
Single-line watermark text, 1024 characters at most.
font
A TrueType .ttf file. It is only read, and not at all with an opacity of 0.
fontSize
Maximum font size in points. A size below 6 pixels to the em is kept as it is.
opacity
Text opacity, 0..255. With 0 nothing is drawn and the picture is returned as it came.
margin
Margin around the text in pixels; a quarter of the smaller side of the picture at most.
jpegQuality
The quality a JPEG is written with, from 1 to 100 on the scale of libjpeg: the higher, the closer to the picture and the larger the file. A picture of another format takes no notice of it.

Returns: The picture as it came, if nothing is drawn on it. Otherwise the picture in the format it came in: PNG, baseline JPEG, 32-bit BMP, ICO, WebP, or all frames of an animated GIF or PNG with its timing and loop count. A JPEG that had an Exif orientation is then turned upright and has none. The ICC profile of a JPEG or a PNG stays with the picture if it is one for RGB; other metadata does not. Modified TIFF and AVIF inputs are written as PNG.

AddTextWatermark
public static byte[] AddTextWatermark(byte[] data, string text, float fontSize, byte opacity, int margin, string fontFamily, string fontFilePath, int jpegQuality)
result = ImageHelper.AddTextWatermark(data, text, fontSize, opacity, margin, fontFamily, fontFilePath, jpegQuality)

Renders anti-aliased semi-transparent white text into the bottom-right corner of a picture - of every frame, if it is animated. The text is laid out left to right in one line and is made smaller if it does not fit between the margins, down to 6 pixels to the em. A text that has no room even then is not drawn, and neither is one that would lay no opacity on any pixel: too small to reach a point a pixel is sampled at, or too faint for the part of a pixel it covers. The picture is then returned as it came. A text that is laid on and cannot be told from what is under it - white on white - is drawn, and the picture is written again.

data
A PNG, JPEG, BMP, GIF, WebP, ICO, TIFF or AVIF picture.
text
Single-line watermark text, 1024 characters at most.
fontSize
Maximum font size in points. A size below 6 pixels to the em is kept as it is.
opacity
Text opacity, 0..255. With 0 nothing is drawn, no font is looked for and the picture is returned as it came.
margin
Margin around the text in pixels; a quarter of the smaller side of the picture at most.
fontFamily
Font family name, Verdana by default. If it is not installed, the first installed of a list of common sans-serif families is taken instead, and failing those the font built into the library, a subset of DejaVu Sans. "DejaVu Sans" itself is always there: installed or built in.
fontFilePath
Optional path to a TrueType .ttf file to use instead of an installed font.
jpegQuality
The quality a JPEG is written with, from 1 to 100 on the scale of libjpeg: the higher, the closer to the picture and the larger the file. A picture of another format takes no notice of it.

Returns: The picture as it came, if nothing is drawn on it. Otherwise the picture in the format it came in: PNG, baseline JPEG, 32-bit BMP, ICO, WebP, or all frames of an animated GIF or PNG with its timing and loop count. A JPEG that had an Exif orientation is then turned upright and has none. The ICC profile of a JPEG or a PNG stays with the picture if it is one for RGB; other metadata does not. Modified TIFF and AVIF inputs are written as PNG.

ConvertToPng
public static byte[] ConvertToPng(byte[] data, ImageProcessingLimits limits)
result = ImageHelper.ConvertToPng(data, limits)

Converts every displayed animation frame, or a static picture, to PNG under explicit limits.

data
Encoded image data.
limits
Limits on decoded images and animation work.

Returns: RGBA8 PNG or APNG, preserving frame timing and supported color profiles.

ConvertToPng
public static byte[] ConvertToPng(byte[] data)
result = ImageHelper.ConvertToPng(data)

Converts a picture to PNG.

data
A PNG, JPEG, BMP, GIF, WebP, ICO, TIFF or AVIF picture.

Returns: An RGBA8 PNG. A GIF becomes an animated PNG with all its frames, delays and as many plays; a PNG that is animated already is checked and returned as it is. The ICC profile of a JPEG or a PNG stays with the picture if it is one for RGB.

GetAnimationFrames
public static IReadOnlyList<ValueTuple<byte[], int>> GetAnimationFrames(byte[] data, ImageProcessingLimits limits)
result = ImageHelper.GetAnimationFrames(data, limits)

Decodes every displayed animation frame under explicit processing limits.

data
GIF, APNG or animated WebP bytes.
limits
Limits on decoded images and animation work.

Returns: Full displayed frames as PNG, each with its delay in milliseconds.

GetAnimationFrames
public static IReadOnlyList<ValueTuple<byte[], int>> GetAnimationFrames(byte[] data)
result = ImageHelper.GetAnimationFrames(data)

Decodes every displayed frame of a GIF, APNG or animated WebP.

data
A GIF, APNG or animated WebP.

Returns: The frames as they are shown - the whole canvas, as an RGBA8 PNG - each with the time it is shown for.

GetAnimationLoopCount
public static int GetAnimationLoopCount(byte[] data)
result = ImageHelper.GetAnimationLoopCount(data)

Reads how many times a GIF, APNG or animated WebP plays, without decoding pixels.

data
A GIF, APNG or animated WebP.

Returns: The number of plays, 0 for endlessly. A GIF without a loop extension plays once.

GetGifFrames
public static IReadOnlyList<ValueTuple<byte[], int>> GetGifFrames(byte[] data)
result = ImageHelper.GetGifFrames(data)

Decodes every frame of a GIF.

data
A GIF picture.

Returns: The frames as they are shown - the whole canvas, as an RGBA8 PNG - each with the time it is shown for.

GetGifLoopCount
public static int GetGifLoopCount(byte[] data)
result = ImageHelper.GetGifLoopCount(data)

Reads the loop count of the NETSCAPE2.0 extension of a GIF, without decoding its frames.

data
A GIF picture.

Returns: How many times the animation is repeated after its first run, 0 for endlessly; -1 if the file has no such extension. Byte[]) gives the number of plays instead.

GetImageFormat
public static ImageFormats GetImageFormat(byte[] data)
result = ImageHelper.GetImageFormat(data)

Identifies a supported file signature without decoding pixels or validating the complete file.

data
The image bytes.

Returns: The detected format, or Unknown.

GetImageFormat
public static ImageFormats GetImageFormat(ReadOnlySpan<byte> data)
result = ImageHelper.GetImageFormat(data)

Identifies a supported file signature without decoding pixels or validating the complete file.

data
The image bytes.

Returns: The detected format, or Unknown.

GetImageSize
public static Size GetImageSize(byte[] data)
result = ImageHelper.GetImageSize(data)

Reads the display size of a supported image without decoding pixels.

data
The picture.

Returns: The size the picture is shown with, in pixels. For a JPEG whose Exif orientation lays it on its side that is the size after turning it upright.

GetImageSize
public static Size GetImageSize(ReadOnlySpan<byte> data)
result = ImageHelper.GetImageSize(data)

Reads the display size of a supported image without decoding pixels.

data
The picture.

Returns: The size the picture is shown with, in pixels. For a JPEG whose Exif orientation lays it on its side that is the size after turning it upright.

ResizeImage
public static byte[] ResizeImage(byte[] data, int maxWidth, int maxHeight, ImageProcessingLimits limits, int jpegQuality)
result = ImageHelper.ResizeImage(data, maxWidth, maxHeight, limits, jpegQuality)

Reduces an image or animation with explicit limits and retains its format.

data
Encoded source image.
maxWidth
Positive maximum width.
maxHeight
Positive maximum height.
limits
Limits on decoded images and animation work.
jpegQuality
JPEG encoding quality, 1 through 100.

Returns: Encoded output retaining animation frames.

ResizeImage
public static byte[] ResizeImage(byte[] data, int maxWidth, int maxHeight, int jpegQuality)
result = ImageHelper.ResizeImage(data, maxWidth, maxHeight, jpegQuality)

Reduces a picture to fit the given bounds, keeping its proportions. A picture that fits already is returned as it is; none is ever enlarged.

data
A PNG, JPEG, BMP, GIF, WebP, ICO, TIFF or AVIF picture.
maxWidth
The most the result may be wide, in pixels.
maxHeight
The most the result may be high, in pixels.
jpegQuality
The quality a JPEG is written with, from 1 to 100 on the scale of libjpeg: the higher, the closer to the picture and the larger the file. A picture of another format takes no notice of it.

Returns: The picture as it came, if it fits already. Otherwise the picture in the format it came in: PNG, baseline JPEG, 32-bit BMP, ICO, WebP, or all frames of an animated GIF or PNG with its timing and loop count. A JPEG that had an Exif orientation is then turned upright and has none. The ICC profile of a JPEG or a PNG stays with the picture if it is one for RGB; other metadata does not. Modified TIFF and AVIF inputs are written as PNG.

ResizeImage
public static byte[] ResizeImage(byte[] data, int maxWidth, int maxHeight, ImageFormats outputFormat, WebpEncodingOptions webpOptions, int jpegQuality)
result = ImageHelper.ResizeImage(data, maxWidth, maxHeight, outputFormat, webpOptions, jpegQuality)

Reduces or converts a picture with explicit WebP compression settings.

data
Encoded source image or animation.
maxWidth
Positive maximum output width.
maxHeight
Positive maximum output height.
outputFormat
Requested output format.
webpOptions
WebP mode and quality, used for WebP output.
jpegQuality
JPEG quality, 1 through 100.

Returns: Encoded output retaining all animation frames.

ResizeImage
public static byte[] ResizeImage(byte[] data, int maxWidth, int maxHeight, ImageFormats outputFormat, ImageProcessingLimits limits, int jpegQuality)
result = ImageHelper.ResizeImage(data, maxWidth, maxHeight, outputFormat, limits, jpegQuality)

Reduces an image or animation with explicit limits and an explicit output format.

data
Encoded source image.
maxWidth
Positive maximum width.
maxHeight
Positive maximum height.
outputFormat
PNG, JPEG, BMP, GIF, WebP or ICO.
limits
Limits on decoded images and animation work.
jpegQuality
JPEG encoding quality, 1 through 100.

Returns: Encoded output retaining animation when the format supports it.

ResizeImage
public static byte[] ResizeImage(byte[] data, int maxWidth, int maxHeight, ImageFormats outputFormat, int jpegQuality)
result = ImageHelper.ResizeImage(data, maxWidth, maxHeight, outputFormat, jpegQuality)

Reduces a picture to fit the bounds and writes it in the requested format.

data
The source image.
maxWidth
Maximum output width.
maxHeight
Maximum output height.
outputFormat
PNG, JPEG, BMP, GIF, WebP or ICO. Animated output requires PNG, GIF or WebP.
jpegQuality
JPEG quality from 1 to 100.

Returns: The resized or converted image, retaining all animation frames.

ResizeImage
public static byte[] ResizeImage(byte[] data, int maxWidth, int maxHeight, ImageFormats outputFormat, ImageProcessingLimits limits, WebpEncodingOptions webpOptions, int jpegQuality)
result = ImageHelper.ResizeImage(data, maxWidth, maxHeight, outputFormat, limits, webpOptions, jpegQuality)

Reduces or converts a picture with explicit processing limits and WebP settings.

data
Encoded source image or animation.
maxWidth
Positive maximum output width.
maxHeight
Positive maximum output height.
outputFormat
Requested output format.
limits
Explicit image and animation processing budgets.
webpOptions
WebP mode and quality, used for WebP output.
jpegQuality
JPEG quality, 1 through 100.

Returns: Encoded output retaining all animation frames.

TryGetImageSize
public static bool TryGetImageSize(byte[] data, Size size)
result = ImageHelper.TryGetImageSize(data, size)

Tries to read the display dimensions from an image header without decoding pixels. A readable header does not guarantee that pixel decoding will succeed.

data
The image bytes; null is treated as unreadable.
size
The display size, or Empty on failure.

Returns: Whether the format and dimensions could be read.

TryGetImageSize
public static bool TryGetImageSize(ReadOnlySpan<byte> data, Size size)
result = ImageHelper.TryGetImageSize(data, size)

Tries to read the display dimensions from an image header without decoding pixels. A readable header does not guarantee that pixel decoding will succeed.

data
The image bytes; null is treated as unreadable.
size
The display size, or Empty on failure.

Returns: Whether the format and dimensions could be read.

Fields

DefaultJpegQuality
public const int DefaultJpegQuality
value = ImageHelper.DefaultJpegQuality

The quality a JPEG is written with unless another is asked for.