Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Modify image color properties using color matrix in VB.net – C#.net.

Author: Shehzad Hemani
Download Source Code : 1101_source.zip

In this simple article you will learn that how to modify image color properties using color matrix in VB.net – C#.net.

 First create an object of bitmap class and graphics class.

Color Matrix:

Color matrix is a class which is used to modify the color properties of the image. Create an object of color matrix class. Constructor of color matrix class takes two dimensional array of float. This array contains the color information which should be applied to the image.

Now write the following code.

C#

float[][] matrixItems = { 

                    new float[] {0.2f, 0, 0, 0, 0}, 

                    new float[] {0, 0.8f, 0, 0, 0}, 

                    new float[] {0, 0, 1, 0, 0}, 

                    new float[] {0, 0, 0, 1, 0},  

                    new float[] {0, 0, 0, 0, 1}};

 ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

VB

Dim [() As Single
Dim 
colorMatrix As ColorMatrix = New ColorMatrix(matrixItems)

Image Attribute:

Now declare the object of image attribute class. This class is used to set the color properties of the image. To set the properties we have a function of image attribute class named SetColorMatrix (). This function takes three attributes. One is color matrix class object second is ColorMatrixFlag enumeration value and third is ColorAdjustType enumeration value.

After that create a texture brush and set the wrap mode property with value tile. Then call fill rectangle function of the graphics class. This will draw the changed image on the form.

Now write the following code on Form Paint Event:

C#

private void OnPaint(object sender, PaintEventArgs e)

        {

            Bitmap bmp = new Bitmap("Koala.jpg");

            Graphics g = e.Graphics;

            float[][] matrixItems = { 

                    new float[] {0.2f, 0, 0, 0, 0}, 

                    new float[] {0, 0.8f, 0, 0, 0}, 

                    new float[] {0, 0, 1, 0, 0}, 

                    new float[] {0, 0, 0, 1, 0},  

                    new float[] {0, 0, 0, 0, 1}};

            ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

            ImageAttributes imageAtt = new ImageAttributes();

            imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            TextureBrush tb = new TextureBrush(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), imageAtt);

            tb.WrapMode = WrapMode.Tile;

            g.FillRectangle(tb, this.ClientRectangle);

            bmp.Dispose();

            tb.Dispose(); 

        }

VB

Private Sub OnPaint(ByVal sender As ObjectByVal As PaintEventArgs)

Dim bmp As Bitmap = New Bitmap("Koala.jpg")
Dim As Graphics e.Graphics
Dim [() As Single
Dim 
colorMatrix As ColorMatrix = New ColorMatrix(matrixItems)
Dim imageAtt As ImageAttributes = New ImageAttributes
imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.
Default, ColorAdjustType.Bitmap)
Dim tb As TextureBrush = New TextureBrush(bmp, New Rectangle(00, bmp.Width, bmp.Height), imageAtt)
tb.WrapMode 
WrapMode.Tile
g.FillRectangle(tb, 
Me.ClientRectangle)
bmp.Dispose
tb.Dispose

End Sub

This is simple code to change color properties of an image.

Now write the following code on FORM LOAD event:

C#

private void Form1_Load(object sender, EventArgs e)

        {          

            this.Text = "Devasp  Application";

        }

VB

Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
        
Me.Text "Devasp  Application"
 
End Sub

This simple article tells that how to modify image color properties using color matrix in VB.net – C#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to make an isotropic image in VB - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net