Class ImageHelper
Provides utility methods for image manipulation, processing, and conversion operations.
Inheritance
Inherited Members
Namespace: Telerik.WinControls
Assembly: Telerik.WinControls.dll
Syntax
public static class ImageHelper
Remarks
Contains static methods for common image operations including alpha blending, masking, cropping, format conversion, and pixel-level manipulations. Supports various image formats and provides efficient bitmap manipulation methods using unsafe code for performance-critical operations.
Methods
ApplyAlpha(Bitmap, Single)
Applies a uniform alpha (transparency) value to all pixels in the specified bitmap.
Declaration
public static void ApplyAlpha(Bitmap bitmap, float fAlpha)
Parameters
System.Drawing.Bitmap
bitmap
The bitmap to modify. Must be in 32bpp ARGB format. |
System.Single
fAlpha
The alpha value to apply, ranging from 0.0 (fully transparent) to 1.0 (fully opaque). |
Remarks
Directly modifies the alpha channel of each pixel using unsafe code for optimal performance. The bitmap must be in 32bpp ARGB format. Commonly used for fade effects and transparency adjustments.
ApplyMask(Bitmap, Brush)
Applies a brush-based alpha mask to a bitmap, modifying the transparency of pixels based on the brush pattern.
Declaration
public static void ApplyMask(Bitmap bitmap, Brush brush)
Parameters
System.Drawing.Bitmap
bitmap
The bitmap to apply the mask to. |
System.Drawing.Brush
brush
The brush defining the alpha mask pattern. The brush's color values determine the transparency. |
Remarks
This method creates a temporary bitmap filled with the specified brush pattern, then uses the red channel values from this pattern as alpha values for the original bitmap. This allows for complex transparency effects based on gradients, patterns, or solid colors.
The masking process:
- Creates a temporary bitmap the same size as the original
- Fills the temporary bitmap with the brush pattern
- Uses the red channel values as alpha channel values for each corresponding pixel
This is useful for creating fade effects, vignettes, and complex transparency patterns.
AreColorsSame(Color, Color)
Declaration
public static bool AreColorsSame(Color color1, Color color2)
Parameters
System.Drawing.Color
color1
|
System.Drawing.Color
color2
|
Returns
System.Boolean
|
BitmapInvertColors(Bitmap)
Declaration
public static Bitmap BitmapInvertColors(Bitmap original)
Parameters
System.Drawing.Bitmap
original
|
Returns
System.Drawing.Bitmap
|
ChangeImagePixels(Image, Color)
Declaration
public static Image ChangeImagePixels(Image image, Color color)
Parameters
System.Drawing.Image
image
|
System.Drawing.Color
color
|
Returns
System.Drawing.Image
|
ConvertToDib(Image)
Converts the image to Device Independent Bitmap format of type BITFIELDS. This is (wrongly) accepted by many applications as containing transparency
Declaration
public static byte[] ConvertToDib(Image image)
Parameters
System.Drawing.Image
image
Image to convert to DIB |
Returns
System.Byte[]
The image converted to DIB, in bytes. |
CopyImageToClipboard(Image)
Copies given image to the clipboard. Different applications understand different image formats, so this method applies the three most common image formats to the data object - Bitmap, PNG and DIB.
Declaration
public static void CopyImageToClipboard(Image image)
Parameters
System.Drawing.Image
image
The image to copy. |
Crop(Bitmap, Rectangle)
Crops a rectangular region from a bitmap and returns it as a new bitmap with high-quality rendering.
Declaration
public static Bitmap Crop(Bitmap image, Rectangle cropRectangle)
Parameters
System.Drawing.Bitmap
image
The source bitmap to crop from. |
System.Drawing.Rectangle
cropRectangle
The rectangular region to extract from the source image, in pixel coordinates. |
Returns
System.Drawing.Bitmap
A new System.Drawing.Bitmap containing the cropped region, with dimensions matching the crop rectangle. |
Remarks
This method creates a new bitmap with the exact dimensions of the crop rectangle and draws the specified region from the source image using high-quality rendering settings:
- Anti-aliased smoothing for clean edges
- High-quality bicubic interpolation for smooth scaling
- High-quality pixel offset mode for precise positioning
The crop rectangle coordinates are relative to the source image. If the rectangle extends beyond the source image boundaries, the out-of-bounds areas will be filled with transparent pixels.
The caller is responsible for disposing of the returned bitmap when no longer needed.
GetBitmapRegionPath(Bitmap, Color)
Declaration
public static GraphicsPath GetBitmapRegionPath(Bitmap bmp, Color transparent)
Parameters
System.Drawing.Bitmap
bmp
|
System.Drawing.Color
transparent
|
Returns
System.Drawing.Drawing2D.GraphicsPath
|
GetBytesFromImage(Image)
Declaration
public static byte[] GetBytesFromImage(Image image)
Parameters
System.Drawing.Image
image
|
Returns
System.Byte[]
|
GetClipboardImage()
Gets image from the clipboard and preserves pixel transparency. If there is no image data in the clipboard, returns null.
Declaration
public static Image GetClipboardImage()
Returns
System.Drawing.Image
The image from clipboard. |
GetClipboardImage(DataObject)
Retrieves an image from the given clipboard data object, in the order PNG, DIB, Bitmap, Image object.
Declaration
public static Bitmap GetClipboardImage(DataObject clipboardData)
Parameters
System.Windows.Forms.DataObject
clipboardData
The clipboard data. |
Returns
System.Drawing.Bitmap
The extracted image, or null if no supported image type was found. |
GetImageData(Bitmap, out Int32)
Gets the raw bytes from an image.
Declaration
public static byte[] GetImageData(Bitmap sourceImage, out int stride)
Parameters
System.Drawing.Bitmap
sourceImage
The image to get the bytes from. |
System.Int32
stride
Stride of the retrieved image data. |
Returns
System.Byte[]
The raw bytes of the image |
GetImageFromBytes(Byte[])
Declaration
public static Image GetImageFromBytes(byte[] bytes)
Parameters
System.Byte[]
bytes
|
Returns
System.Drawing.Image
|
HslToRgb(Double, Double, Double, Double)
Declaration
public static Color HslToRgb(double h, double s, double l, double a)
Parameters
System.Double
h
|
System.Double
s
|
System.Double
l
|
System.Double
a
|
Returns
System.Drawing.Color
|
IsDarkColor(Color)
Declaration
public static bool IsDarkColor(Color color)
Parameters
System.Drawing.Color
color
|
Returns
System.Boolean
|
PointInRegion(Region, Point)
Declaration
public static bool PointInRegion(Region region, Point client)
Parameters
System.Drawing.Region
region
|
System.Drawing.Point
client
|
Returns
System.Boolean
|
RegionFromBitmap(Bitmap, Color)
Declaration
public static Region RegionFromBitmap(Bitmap bmp, Color transparent)
Parameters
System.Drawing.Bitmap
bmp
|
System.Drawing.Color
transparent
|
Returns
System.Drawing.Region
|