Advertisement
ForeverZer0

Change Bitmap Opacity

Feb 19th, 2012
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4.  
  5. namespace ImageUtils
  6. {
  7.     class ImageTransparency
  8.     {
  9.         public static Bitmap ChangeOpacity(Image img, float opacityvalue)
  10.         {
  11.             // Create a new bitmap with same dimensions as original
  12.             Bitmap bmp = new Bitmap(img.Width,img.Height);
  13.             ColorMatrix colormatrix = new ColorMatrix();
  14.             colormatrix.Matrix33 = opacityvalue;
  15.             ImageAttributes imgAttribute = new ImageAttributes();
  16.             imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  17.             Rectangle srcRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  18.             using (Graphics graphics = Graphics.FromImage(bmp))
  19.             {
  20.                 graphics.DrawImage(img, srcRect, 0, 0, img.Width, img.Height,
  21.                     GraphicsUnit.Pixel, imgAttribute);
  22.             }
  23.             return bmp;
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement