Draw bitmap in memory, then display

jhasty

Member
Joined
Feb 23, 2003
Messages
5
I am trying to figure out how to draw simple lines on a bitmap in memory, then display the bitmap on a control (probably a PictureBox). I also need to be able to save the memory bitmap to a .bmp file. Im using VB .NET.

I have been completely unable to figure out how to set up the various objects to do this. BitMap? Image? Graphics? I am so lost.

If someone could show me a simple example, I think I could figure it out from there. How about an example that would create a bitmap in memory, draw a line on the bitmap, and then show the bitmap on a PictureBox control?

Any help would be greatly appreciated.
 
This example shows creating an in-memory bitmap, drawing a circle on it, then assigning the bitmap to a picturebox.

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim b As New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(b)

        g.DrawEllipse(Pens.Red, New RectangleF(0, 0, b.Width, b.Height))

        PictureBox1.Image = b
        b.Save("c:\temp.bmp")

        g.Dispose()
    End Sub

If you uncomment the commented line, the bitmap will be saved to the hard disk.
 
Hi

Hi..

just wonder i draw a circle in the picturebox1

1) and i want to store the circle in memory (only circle)

when i store into bmp...

i want to see the circle with transparent backrground...

i need to do some image processing?

The way on top works, just not sure does it store with the background color or image together?

2) Just wondering, if 1 drag 1 circle...

i want to calculate the x,y of the circle...

maybe 1 circle with 10 nodes of (x,y) and store in array

how can i do that?

Any help?

Regards,
Chua Wen Ching :p
 
Divil when I use that code, the Graphics thing in the declaring gets highlighted in red. I dont understand the error but when I check it out it says that "Type Expected"...what does that mean?
 
Does youre project import System.Drawing, if it does change it to System.Drawing.Graphics.FromImage(b).

1) I think youre asking how to make the background transparent? Put b.MakeTransparent(Color.White) before saving, you may also need to save it as a picture format that allows transparency like GIF
 
Originally posted by Diablicolic
Divil when I use that code, the Graphics thing in the declaring gets highlighted in red. I dont understand the error but when I check it out it says that "Type Expected"...what does that mean?
Are you using VB.NET?? :confused:
 
Help

Originally posted by divil
This example shows creating an in-memory bitmap, drawing a circle on it, then assigning the bitmap to a picturebox.

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim b As New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(b)

        g.DrawEllipse(Pens.Red, New RectangleF(0, 0, b.Width, b.Height))

        PictureBox1.Image = b
        b.Save("c:\temp.bmp")

        g.Dispose()
    End Sub

If you uncomment the commented line, the bitmap will be saved to the hard disk.

Any idea how that code works in c#?

the b.width - i cant find the width!

and i cannot place b into FromImage(b) // invalid arguments

C:\WenChing\Store Image in Memory\Form1.cs(94): Argument 1: cannot convert from System.Drawing.Bitmap[*,*] to System.Drawing.Image


Any help?

Regards,
Chua Wen Ching :p
 
C#:
        Bitmap b = New Bitmap(100, 100);
        Graphics g = Graphics.FromImage(b);

        g.DrawEllipse(Pens.Red, New RectangleF(0, 0, b.Width, b.Height));

        g.Dispose();

        //b.MakeTransparent(Color.White);
        PictureBox1.Image = b;
        //b.Save("c:\temp.bmp");
Im not skilled in C#, I have no liking for C syntax but I think this right
 
I just checked it in C# and this worked:
C#:
Bitmap b = new Bitmap(100, 100);
        Graphics g = Graphics.FromImage(b);

        g.DrawEllipse(Pens.Red, 0, 0, b.Width, b.Height);

        g.Dispose();

        //b.MakeTransparent(Color.White);
        this.pictureBox1.Image= b;
        //b.Save("c:\temp.bmp");
The thing I really hate in C syntax is that its case sensitive, thats what always gets me, capitals when they should be lowercase. Whats the point of having a variable named Var1 and var1 or New must be new. Its just confusing
 
Last edited by a moderator:
Back
Top