/// <summary> /// Returns a new <see cref="T:System.Drawing.Image"/> with the specified effect. /// </summary> /// <param name="image">Image object to which the effect is applied.</param> /// <param name="effect">Effect type.</param> /// <returns> /// Returns a new <see cref="T:System.Drawing.Image"/>, result of applying the effect to specified image . /// </returns> public static Image ApplyEffect(this Image image, KnownEffectType effect) { SentinelHelper.ArgumentNull(image); Image imageWithEffect; using (var bmp = new Bitmap(image.Width, image.Height)) { using (var graphics = Graphics.FromImage(bmp)) { bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution); graphics.DrawImage( image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, ImageHelper.GetImageAttributesFromEffect(effect)); imageWithEffect = (Image)bmp.Clone(); } } return(imageWithEffect); }
/// <summary> /// Gets the manipulation of the colors in an image to an effect. /// </summary> /// <param name="effect">Effect type.</param> /// <returns> /// A <see cref="T:System.Drawing.Imaging.ImageAttributes" /> object that contains the information about how bitmap colors are manipulated. /// </returns> public static ImageAttributes GetImageAttributesFromEffect(KnownEffectType effect) { var newColorMatrix = new ColorMatrix(); switch (effect) { case KnownEffectType.None: break; case KnownEffectType.Disabled: newColorMatrix = DisabledColorMatrix; break; case KnownEffectType.GrayScale: newColorMatrix = GrayScaleColorMatrix; break; case KnownEffectType.GrayScaleRed: newColorMatrix = GrayScaleRedColorMatrix; break; case KnownEffectType.GrayScaleGreen: newColorMatrix = GrayScaleGreenColorMatrix; break; case KnownEffectType.Brightness: newColorMatrix = BrightnessColorMatrix; break; case KnownEffectType.MoreBrightness: newColorMatrix = MoreBrightnessColorMatrix; break; case KnownEffectType.Dark: newColorMatrix = DarkColorMatrix; break; case KnownEffectType.MoreDark: newColorMatrix = MoreDarkColorMatrix; break; case KnownEffectType.GrayScaleBlue: newColorMatrix = GrayScaleBlueColorMatrix; break; case KnownEffectType.OpacityPercent05: newColorMatrix.Matrix33 = 0.05f; break; case KnownEffectType.OpacityPercent10: newColorMatrix.Matrix33 = 0.10f; break; case KnownEffectType.OpacityPercent20: newColorMatrix.Matrix33 = 0.20f; break; case KnownEffectType.OpacityPercent30: newColorMatrix.Matrix33 = 0.30f; break; case KnownEffectType.OpacityPercent40: newColorMatrix.Matrix33 = 0.40f; break; case KnownEffectType.OpacityPercent50: newColorMatrix.Matrix33 = 0.50f; break; case KnownEffectType.OpacityPercent60: newColorMatrix.Matrix33 = 0.60f; break; case KnownEffectType.OpacityPercent70: newColorMatrix.Matrix33 = 0.70f; break; case KnownEffectType.OpacityPercent80: newColorMatrix.Matrix33 = 0.80f; break; case KnownEffectType.OpacityPercent90: newColorMatrix.Matrix33 = 0.90f; break; } ImageAttributes imageAttributes; ImageAttributes tempImageAttributes = null; try { tempImageAttributes = new ImageAttributes(); tempImageAttributes.SetColorMatrix(newColorMatrix); imageAttributes = (ImageAttributes)tempImageAttributes.Clone(); } finally { if (tempImageAttributes != null) { tempImageAttributes.Dispose(); } } return(imageAttributes); }