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

Draw shapes to bitmap image in memory using Graphics class in –

Author: Shehzad Hemani
Download Source Code :

In this simple article you will learn that how to draw shapes to bitmap in memory using Graphics class in –


First of all create an object of bitmap class. Constructor of bitmap class takes three parameters one is width second is height and third is pixel format.  Pixel format should be Format32bppArgb so that it will allow you to draw shapes to bitmap.


Now make an object of graphics class. Make this object form image for this purpose we have a function of graphics class named fromimage (). This function takes object of image class and returns object of graphics class.


CompositingMode tells you that how to draw composited images on the form. To set this we have a property of graphics class named CompositingMode. This property takes value of enumeration named CompositingMode.

After doing above steps create a solid brush and draw recangle, ellipse and different shapes which will be draw to the bitmap and then draw this bitmap on from using function of graphics class named draw image. This function takes five parameters. You can read about this function from one of my previous articles.

To demonstrate write the follwing code on From Paint Event:


private void OnPaint(object sender, PaintEventArgs e)


            Graphics g = e.Graphics;

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

            Bitmap bmp = new Bitmap(300, 300, PixelFormat.Format32bppArgb);

            Graphics gBitmap = Graphics.FromImage(bmp);

            gBitmap.CompositingMode = CompositingMode.SourceCopy;

            Color tm = Color.Blue;

            Brush tmBrush = new SolidBrush(tm);

            gBitmap.FillRectangle(tmBrush, 15, 15, 120, 120);

            Color rd = Color.DarkTurquoise;

            Brush brsh = new SolidBrush(rd);

            gBitmap.FillEllipse(brsh, 70, 50, 150, 160);             

            g.DrawImage(bmp, 10, 10, bmp.Width, bmp.Height);







Private Sub OnPaint(ByVal sender As ObjectByVal As PaintEventArgs)
Dim As Graphics e.Graphics
Dim bmp As Bitmap = New Bitmap(300300, PixelFormat.Format32bppArgb)
Dim gBitmap As Graphics Graphics.FromImage(bmp)
Dim tm As Color Color.Blue
Dim tmBrush As Brush = New SolidBrush(tm)
Dim rd As Color Color.DarkTurquoise
Dim brsh As Brush = New SolidBrush(rd)
1010, bmp.Width, bmp.Height)
End Sub

This is simple code to draw shapes to bitmap in memory.

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 draw shapes to bitmap in memory using Graphics class in –

Add Article Comment:
Name :
Email Address :
Comments :
<< Drag and draw shapes on form using mouse in - C

Disclaimer - Privacy
© 2002-2017