.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Change Opacity of Image in C#

Posted By:Ravi Ranjan Kumar       Posted Date: June 03, 2011    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

This Article enable you to change Opacity of Image using C#

This Article enable you to change opacity of Image in C# using System.Drawing and System.Drawing.Imaging NameSpaces.

Take a look how to create a ChangeOpacity Method will enable you to change opacity of Image.

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ImageUtils
    class ImageTransparency
        public static Bitmap ChangeOpacity(Image img, int width, int height, float opacityvalue)
            Bitmap bmp = new Bitmap(width, height);
            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);
            return bmp;

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. 

How to use Given Function

Simply add this Method in your program , 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 as Given in below Image.

float opacityvalue = float.Parse(txtopacityvalue.Text) / 100;
pictureBox1.Image = ImageUtils.ImageTransparency.ChangeOpacity(Image.FromFile("filename"), pictureBox1.Width, pictureBox1.Height, opacityvalue); // Calling ChangeOpacity Method.

After Compilation

Take a look how a Application work 

When user Select any Image by using Browse button, the Original Image will Display and After putting Opacity Percentage which is resides between both Images and Press on DisplayImage Effect will be seen in Second Images.

you can Upload SourceCode to learn how to use the given code in your program

Thanks :) 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend