public ScaleOpacity ( float scaleOpacity ) : void | ||
scaleOpacity | float | Alpha scaling value |
return | void |
/// <summary> /// Changes the opacity of an image. /// </summary> /// <param name="image">Image to change</param> /// <param name="opacity">Opacity change to apply (0..255)</param> /// <remarks>Values out of range will be automatically corrected</remarks> public static void ChangeOpacity(Image image, int opacity) { using (var newImage = new Bitmap(image)) { using (var g = Graphics.FromImage(image)) { var imageAttr = new ImageAttributes(); var qm = new QColorMatrix(); qm.ScaleOpacity(opacity.Clamp(0, 255) / 255.0f); imageAttr.SetColorMatrix(qm.ToColorMatrix()); var destRect = new Rectangle(new Point(), image.Size); g.Clear(Color.Transparent); g.DrawImage(newImage, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); } } }