SHARE
TWEET

Change Bitmap Opacity

ForeverZer0 Feb 19th, 2012 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top