/// <summary> /// Apply a color matrix by copying from the source to the destination /// </summary> /// <param name="source">Image to copy from</param> /// <param name="sourceRect">NativeRect to copy from</param> /// <param name="destRect">NativeRect to copy to</param> /// <param name="dest">Image to copy to</param> /// <param name="colorMatrix">ColorMatrix to apply</param> public static void ApplyColorMatrix(this Bitmap source, NativeRect sourceRect, Bitmap dest, NativeRect destRect, ColorMatrix colorMatrix) { using (var imageAttributes = new ImageAttributes()) { imageAttributes.ClearColorMatrix(); imageAttributes.SetColorMatrix(colorMatrix); source.ApplyImageAttributes(sourceRect, dest, destRect, imageAttributes); } }
/// <summary> /// Adjust the brightness, contract or gamma of an image. /// Use the value "1.0f" for no changes. /// </summary> /// <param name="sourceBitmap">Original bitmap</param> /// <param name="brightness"></param> /// <param name="contrast"></param> /// <param name="gamma"></param> /// <returns>Bitmap with grayscale</returns> public static Bitmap Adjust(Bitmap sourceBitmap, float brightness, float contrast, float gamma) { //create a blank bitmap the same size as original // If using 8bpp than the following exception comes: A Graphics object cannot be created from an image that has an indexed pixel format. var newBitmap = BitmapFactory.CreateEmpty(sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format24bppRgb, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); using (var adjustAttributes = CreateAdjustAttributes(brightness, contrast, gamma)) { sourceBitmap.ApplyImageAttributes(NativeRect.Empty, newBitmap, NativeRect.Empty, adjustAttributes); } return(newBitmap); }
/// <summary> /// Apply image attributes to the image /// </summary> /// <param name="source">Image to apply matrix to</param> /// <param name="imageAttributes">ImageAttributes to apply</param> public static void ApplyColorMatrix(this Bitmap source, ImageAttributes imageAttributes) { source.ApplyImageAttributes(NativeRect.Empty, source, NativeRect.Empty, imageAttributes); }