Click here to Skip to main content
15,569,858 members
Articles / Programming Languages / C#
Posted 25 May 2011

Tagged as


16 bookmarked

Change Opacity of Image in C#

Rate me:
Please Sign up or sign in to vote.
4.91/5 (22 votes)
27 May 2011CPOL
A tip which enables you to change Opacity of Image in C#
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#[^]


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

Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionThanks! Pin
Member 91641515-Mar-17 2:26
Member 91641515-Mar-17 2:26 
QuestionLicence of your code Pin
aazam rafiee zade7-Nov-16 9:32
aazam rafiee zade7-Nov-16 9:32 
QuestionImage Opacity Routine - Memory Leak! Pin
Member 1171437628-May-15 19:15
Member 1171437628-May-15 19:15 
QuestionThanks, very good but ImageUtils namespace doesn't appear on C# Pin
Member 1049790721-Mar-14 14:04
professionalMember 1049790721-Mar-14 14:04 
QuestionThanks Pin
remoz22-Mar-13 2:31
remoz22-Mar-13 2:31 
QuestionTrying to modify the function for an auto fade in effect. Pin
Zerophase6-Feb-13 15:20
Zerophase6-Feb-13 15:20 
QuestionIn web form i am unable to pass the image parameter how can i do this Pin
sreedharmasula5-Jan-13 22:46
sreedharmasula5-Jan-13 22:46 
GeneralThanks Ravi! Not exactly for the tip, but for the idea of st... Pin
Manfred Rudolf Bihy28-May-11 13:06
professionalManfred Rudolf Bihy28-May-11 13:06 
GeneralRe: Thanks :) Pin
RaviRanjanKr28-May-11 19:42
professionalRaviRanjanKr28-May-11 19:42 
GeneralInteresting solution! Typically I tend to use CCS/jQuery cli... Pin
DrABELL27-May-11 16:02
DrABELL27-May-11 16:02 
GeneralRe: Thank you DrABELL :) Pin
RaviRanjanKr27-May-11 19:59
professionalRaviRanjanKr27-May-11 19:59 
GeneralIsn't it similar to this article: <a href="http://howtoideas... Pin
Tarun.K.S25-May-11 22:47
Tarun.K.S25-May-11 22:47 
GeneralRe: Yeah! its very similar to the refer article infact I've just... Pin
RaviRanjanKr25-May-11 23:31
professionalRaviRanjanKr25-May-11 23:31 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.