Change Opacity - CSharp System.Drawing.Imaging

CSharp examples for System.Drawing.Imaging:Bitmap

Description

Change Opacity

Demo Code


using System.Drawing.Imaging;
using System.Drawing;

public class Main{
        public static Bitmap ChangeOpacity(Image img, float opacityvalue)
        {//from   ww  w.ja v  a2s.  c  o m
            Bitmap bmp = new Bitmap(img.Width, img.Height); // Determining Width and Height of Source Image
            Graphics graphics = Graphics.FromImage(bmp);
            ColorMatrix colormatrix = new ColorMatrix();
            colormatrix.Matrix33 = opacityvalue;
            ImageAttributes imgAttribute = new ImageAttributes();
            imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            graphics.DrawImage(img, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttribute);
            graphics.Dispose();   // Releasing all resource used by graphics 
            return bmp;
        }
}

Related Tutorials