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
 

Draw shapes to bitmap image in memory using Graphics class in VB.net – C#.net.

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

In this simple article you will learn that how to draw shapes to bitmap in memory using Graphics class in VB.net – C#.net.

 Bitmap:

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.

Graphics:

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:

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:

C#

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);

            bmp.Dispose();

            gBitmap.Dispose();

            brsh.Dispose();

            tmBrush.Dispose();

        }

VB

Private Sub OnPaint(ByVal sender As ObjectByVal As PaintEventArgs)
        
Dim As Graphics e.Graphics
        g.FillRectangle(Brushes.White, 
Me.ClientRectangle)
        
Dim bmp As Bitmap = New Bitmap(300300, PixelFormat.Format32bppArgb)
        
Dim gBitmap As Graphics Graphics.FromImage(bmp)
        gBitmap.CompositingMode 
CompositingMode.SourceCopy
        
Dim tm As Color Color.Blue
        
Dim tmBrush As Brush = New SolidBrush(tm)
        gBitmap.FillRectangle(tmBrush, 
1515120120)
        
Dim rd As Color Color.DarkTurquoise
        
Dim brsh As Brush = New SolidBrush(rd)
        gBitmap.FillEllipse(brsh, 
7050150160)
        g.DrawImage(bmp, 
1010, bmp.Width, bmp.Height)
        bmp.Dispose
        gBitmap.Dispose
        brsh.Dispose
        tmBrush.Dispose
    
End Sub

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

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 draw shapes to bitmap in memory using Graphics class in VB.net – C#.net.

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

Disclaimer - Privacy
© 2002-2017 DevASP.net