Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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. I'm 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.

  • *Gurus*
Posted

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

 

   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.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • 5 months later...
Posted

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

Posted
Divil when I use that code, the Graphics thing in the declaring gets highlighted in red. I don't understand the error but when I check it out it says that "Type Expected"...what does that mean?
"Reality is fake, Dreams are for real"
Posted

Does you're project import System.Drawing, if it does change it to System.Drawing.Graphics.FromImage(b).

 

1) I think you're 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

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
  • *Experts*
Posted
Divil when I use that code, the Graphics thing in the declaring gets highlighted in red. I don't understand the error but when I check it out it says that "Type Expected"...what does that mean?
Are you using VB.NET?? :confused:
Posted

Help

 

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

 

   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 can't 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

Posted


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

I'm not skilled in C#, I have no liking for C syntax but I think this right

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted (edited)

I just checked it in C# and this worked:

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 it's case sensitive, that's 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. It's just confusing

Edited by AndreRyan
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...