Click here to Skip to main content
Click here to Skip to main content

Change Opacity of Image in C#

By , 27 May 2011
 
This is a simple tip/trick which enables you to change Opacity of Image using C# by using System.Drawing and System.Drawing.Imaging NameSpaces.
Take a look at how ChangeOpacity(Image img, float opacityvalue) method will enable you to change opacity of Image.
In this given code, System.Drawing Namespace is used to access Bitmap and Graphics classes whereas System.Drawing.Imaging Namespaces is used to access ColorMatrix and ImageAttributes.
ChangeOpacity method has two parameters including img for Image on which opacity will apply, and opacityvalue for set opacity level of form.
 
using System;
using System.Drawing;
using System.Drawing.Imaging;
 
namespace ImageUtils
{
    class ImageTransparency
    {
        public static Bitmap ChangeOpacity(Image img, float opacityvalue)
        {
            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;
        }
    }
}
 
When you will use this method in your program to change opacity of an image in pictureBox control, you need to write code as given below:
 
float opacityvalue = float.Parse(txtopacityvalue.Text) / 100;
pictureBox1.Image = ImageUtils.ImageTransparency.ChangeOpacity(Image.FromFile("filename"),opacityvalue);  //calling ChangeOpacity Function 
 
To learn how to use the given code in your program:
Click here[^] to download source code.
 
[Edited-"Adding Reference Link"]
Reference Article: There-Image Opacity Using C#[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

RaviRanjankr
Software Developer
India India
Member
Microsoft Student Partner | CS Student | MCTS | CP MVP | Crazy Learner | Dreamer
 
An Indian, who loves his country, believes in freedom, He is an enthusiast Techie and crazy learner. He is passionate about Technologies and social media. He holds bachelor degree of CS in Information Technology and now pursuing Master degree in Computer Application.
 
He always excited and keen Interested in learning and sharing knowledge. He loves to write blog, learn new things, listen music, taking arts and Playing Games..
 
He keep himself on the desk of his imagination, hanging around with some inceptions.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThanksmemberremoz22 Mar '13 - 1:31 
QuestionTrying to modify the function for an auto fade in effect.memberZerophase6 Feb '13 - 14:20 
QuestionIn web form i am unable to pass the image parameter how can i do thismembersreedharmasula5 Jan '13 - 21:46 
GeneralThanks Ravi! Not exactly for the tip, but for the idea of st...mvpManfred R. Bihy28 May '11 - 12:06 
GeneralRe: Thanks :)mvpRaviRanjankr28 May '11 - 18:42 
GeneralInteresting solution! Typically I tend to use CCS/jQuery cli...memberDrABELL27 May '11 - 15:02 
GeneralRe: Thank you DrABELL :)mvpRaviRanjankr27 May '11 - 18:59 
GeneralIsn't it similar to this article: <a href="http://howtoideas...memberTarun.K.S25 May '11 - 21:47 
GeneralRe: Yeah! its very similar to the refer article infact I've just...mvpRaviRanjankr25 May '11 - 22:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 27 May 2011
Article Copyright 2011 by RaviRanjankr
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid