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

Create bitmap in memory – set compositing Mode of bitmap using Graphics - Bitmap class in –

Author: Shehzad Hemani
Download Source Code :

In this simple article you will learn that how to create a bitmap in memory and how to change a bitmap compositing mode in –

 Create Bitmap:

First you have to create an object of bitmap class. Bitmap class constructor takes three parameters height, width and pixel format. Pixel format should be Format32bppArgb so that it will allow you to modify bitmap in memory. In memory means it will not direct modify on form. First it will change in memory then will display on form. So it is fast way to modify the bitmap.


Create two objects of graphics class one is from form and other is from bitmap file so that you will be able to modify the bitmap. To create a graphics object from file we have a function of graphics class named fromimage (). This function takes an object of image class and returns an object of graphics class.

Compositing Mode:

Compositing mode basically helps you to decide that how to draw the composited images on the form. There different types of compositing mode. To set the mode we have a property of graphics class names compositingMode. This property takes a values of enumeration named CompositingMode.

Draw Image:

After setting the property draw the image on form using function of graphics class named draw image. This function takes five parameters. Image class object, x, y coordinates of form where image will draw, height and width.

Now write the following code on Form Paint Event:


private void OnPaint(object sender, PaintEventArgs e)


            Graphics gForm = e.Graphics;

            gForm.FillRectangle(Brushes.White, this.ClientRectangle);

            for (int i = 1; i <= 7; ++i)


                Rectangle r = new Rectangle(i * 40 - 15, 0, 15,this.ClientRectangle.Height);

                gForm.FillRectangle(Brushes.Orange, r);


            Bitmap bmp = new Bitmap(260, 260, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            Graphics gBmp = Graphics.FromImage(bmp);

            gBmp.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;

            gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);





Private Sub OnPaint(ByVal sender As ObjectByVal As PaintEventArgs)
Dim gForm As Graphics e.Graphics
Dim As Integer = 1
Do While (i <7)            
Dim As Rectangle = New Rectangle(((i * 40)  _
15), 015Me.ClientRectangle.Height)
        gForm.FillRectangle(Brushes.Orange, r)
Dim bmp As Bitmap = New Bitmap(260260, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim gBmp As Graphics Graphics.FromImage(bmp)
2020, bmp.Width, bmp.Height)
End Sub

This is simple code to create bitmap and set compositing mode.

Now write the following code on FORM LOAD event:


private void Form1_Load(object sender, EventArgs e)


            this.Text = "Devasp  Application";



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

This simple article tells that how to create a bitmap in memory and how to change a bitmap compositing mode in –

Add Article Comment:
Name :
Email Address :
Comments :
<< Modify image color properties using color matrix in –

Disclaimer - Privacy
© 2002-2017