How to resize the bitmap used for Paint event

  • Thread starter Thread starter active
  • Start date Start date
A

active

This is what I do in a PictureBox New:
b1 = New Drawing.Bitmap(Width, Height, Me.CreateGraphics())

g1 = Graphics.FromImage(b1)



Someplace I do

g1.DrawString.........



Then in Paint I do:

e.Graphics.DrawImage(b1, 0, 0)



Do I have to do something in Dispose??



What should I do if the Box is resized??





Thanks

Cal
 
active said:
This is what I do in a PictureBox New:
b1 = New Drawing.Bitmap(Width, Height, Me.CreateGraphics())

g1 = Graphics.FromImage(b1)


I guess you declared g1 and b1 at class level? (a field in the class)

In Sub New I'd probably write

dim g as graphics
g = me.creategraphics
b1 = New Drawing.Bitmap(Width, Height, g)
g.dispose '<- dispose here
g1 = Graphics.FromImage(b1)
'...

Someplace I do

g1.DrawString.........



Then in Paint I do:

e.Graphics.DrawImage(b1, 0, 0)



Do I have to do something in Dispose??

I'd call g1.dispose and b1.dispose.
What should I do if the Box is resized??

I'd call g1.dispose, b1.dispose and recreate the bitmap. So, the Bitmap
creating lines should be a separate proc called from the ctor and in
OnResize.
 
I realize that the quick test I had did seemed to work but it was not
correct.
I have a usercontrol containing a picturebox that I want to draw on.
What happens is I'm drawing on the UserControl instead.



I guess you declared g1 and b1 at class level? (a field in the class)

In Sub New I'd probably write

dim g as graphics
g = me.creategraphics

can't use g1 here? Why do we need g?
b1 = New Drawing.Bitmap(Width, Height, g)

I don't understand why dispose and then get a new one.
What is this for??
g.dispose '<- dispose here
g1 = Graphics.FromImage(b1)
'...




I'd call g1.dispose, b1.dispose and recreate the bitmap. So, the Bitmap
creating lines should be a separate proc called from the ctor and in
OnResize.

recreate the graphics in the bitmap is not possible. Think of Photoshop and
the user resizes after drawing.


Thanks for helping,
Cal
 
active said:
I realize that the quick test I had did seemed to work but it was
not correct.
I have a usercontrol containing a picturebox that I want to draw
on. What happens is I'm drawing on the UserControl instead.

Then you can paint in the Picturebox' paint event.

An alternative is to derive your own class from the Picturebox class and
override OnPaint - but currently this does not make the difference.
can't use g1 here? Why do we need g?

It was _you_ who created a new Graphics object. In your first posting you
wrote

b1 = New Drawing.Bitmap(Width, Height, Me.CreateGraphics())

I changed this and assigned the return value of Me.CreateGraphics to the
variable to be able to call the Dispose method afterwards.

I don't understand why dispose and then get a new one.
What is this for??

See comment above: You also created two Graphics objects. I guess you did it
because you wanted this for the reason described in the documentation for
the constructor when passing a graphics object: The resolution of the
graphics object is applied to the bitmap.
recreate the graphics in the bitmap is not possible. Think of
Photoshop and the user resizes after drawing.

What does this mean? You can copy the old Bitmap into the new Bitmap if you
don't want to loose the content.
 
Then you can paint in the Picturebox' paint event.
I had figured that out and it makes sense and works OK.

It was _you_ who created a new Graphics object. In your first posting you
wrote

b1 = New Drawing.Bitmap(Width, Height, Me.CreateGraphics())

I changed this and assigned the return value of Me.CreateGraphics to the
variable to be able to call the Dispose method afterwards.
Got it! Thanks


What does this mean? You can copy the old Bitmap into the new Bitmap if you
don't want to loose the content.

Sounds good to me. thanks

Thank a lot
Cal
 
* " active said:
This is what I do in a PictureBox New:
b1 = New Drawing.Bitmap(Width, Height, Me.CreateGraphics())

g1 = Graphics.FromImage(b1)



Someplace I do

g1.DrawString.........



Then in Paint I do:

e.Graphics.DrawImage(b1, 0, 0)



Do I have to do something in Dispose??

You will have to dispose 'g1'.

\\\
g1.Dispose()
///
What should I do if the Box is resized??

Who knows what you want to do?
 
Back
Top