Geekpedia Programming Tutorials






Set Image Opacity Using C#

A C# function for changing the opacity on an image to the specified value. It uses the System.Drawing, System.Drawing.Imaging and System.Drawing.Drawing2D namespaces.

On Wednesday, April 16th 2008 at 12:28 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.8 with 4 votes)
Contextual Ads
More C# Resources
Advertisement
  1. public static Image SetImgOpacity(Image imgPic, float imgOpac)
  2. {
  3.       Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
  4.       Graphics gfxPic = Graphics.FromImage(bmpPic);
  5.       ColorMatrix cmxPic = new ColorMatrix();
  6.       cmxPic.Matrix33 = imgOpac;
  7.  
  8.       ImageAttributes iaPic = new ImageAttributes();
  9.       iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  10.       gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
  11.       gfxPic.Dispose();
  12.  
  13.       return bmpPic;
  14. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by http://codelog.blogial.com on Friday, October 17th 2008 at 04:42 AM

Nice post. Was really helpful.

by Ankit Rajpoot on Sunday, November 9th 2008 at 10:41 AM

Very nice post. Saved my time. Thanks

by Dharmin on Sunday, December 27th 2009 at 01:21 PM

Very very helpful! Thanks a lot!

by shayan on Sunday, February 14th 2010 at 03:50 PM

Very Nice Thank you

by darki699 on Monday, March 1st 2010 at 08:58 AM

Add:
iaPic.Dispose();

to prevent memory leaks.

^^

by Hirushan on Friday, May 14th 2010 at 10:53 PM

Thanks Man!! Great job!!

by Alexandre on Tuesday, July 6th 2010 at 05:33 AM

Ce code est tout simplement génial.
Maintenant je peut créer une image PNG avec une couche alpha transparente.

BRAVO !!!

by Eladio Meza on Monday, July 26th 2010 at 09:48 PM

Justo lo que andaba buscando...muchas gracias

Buen aporte..

Es increible que se pueda hacer tan facil y hay gente que quiere vender un programa pagando licencia por este tipo de ejemplos...

Gracias


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons