Cannot Change Form BackColor: VB Net 2008

S

sam

Hi
I write a small vb program with GDI+. Everything is Ok until I try to
change form backColor. I use the code below: (event form load)

myImage = New Bitmap(Me.ClientSize.Width, _
Me.ClientSize.Height)

Dim g As Graphics = Graphics.FromImage(myImage)
g.Clear(Me.BackColor)
Me.BackColor = Color.Cornsilk
g.Dispose()

Nothing happens. However, if I move "Me.BackColor = Color.Cornsilk"
before "g.Clear(Me.BackColor)", the backcolor is changed.

Please advice, Thank you in advance
Sam
 
J

James Hahn

I don't understand why you are creating the Graphics object.

All you need is the Me.BackColor = ... line in the Load event.
 
S

sam

Thank you for your response James.
I try to write an simple "paint" program. I use bitmap to store my
drawing on the Form an repaint the bitmap (myImage) when Form is
repaint. I have try to save the image from the Bitmap but when I open
it, its background is always black. I try to get aroaund this by clear
the bitmap with backcolor of the Form. That seem o work. Then, I think
if a user want to change Form backcolor after he draw on the Form. And,
then I got this problem.
Sam
 
J

James Hahn

You probably have something in the Paint event like:

Dim G as Graphics = e.Graphics
G.DrawImage(myimage, 0, 0)

This will overwrite the whole control with the image - there will be no
control background to see.

By setting the control background to the selected color BEFORE doing
g.Clear(Me.BackColor) in the Load event, you ensured that the image was set
to the new background color. Therefore that color (the new image color)
appeared in the control when the image was drawn.

When you tried to set the background color AFTER doing g.Clear(Me.BackColor)
the image color was being set to the original control background color, and
that's the color that was appearing when you painted the control with the
image in the Paint event. The control background was changed to the new
color, but you won't see it because you are painting over the whole control
with the image.
 
S

sam

Thank you very much, I get the idea.
Yes, I have

Dim G as Graphics = e.Graphics
G.DrawImage(myimage, 0, 0)


like you said in Form Paint event which is (to my understanding) is
called after Form Load. So, that why. Thank you again.

May I ask more question. Regrading of the problem I have when I save
image that a user draw in to a file and get BLACK background. How can I
work around that?
Sam
 
J

James Hahn

I presume you are saving myimage as an image file.

When you save the image you should get exactly what has been painted to the
image object. You use the term 'background': that's not really relevant to a
bitmap, but you can create the equivalent of a background by clearing the
image to a nominated color before you start painting. So if you create the
image with something like:

myImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
Dim g As Graphics = Graphics.FromImage(myImage)
g.Clear(Color.Cornsilk)

then that is the color of the image that the user will be painting over, and
it should be the color of the image that you see behind the user's painting
when you view the image file.

If you haven't cleared the image to any color before painting, then the only
thing you will see in the image file is what the user painted. The rest
doesn't exist. In that case the user will see the form's background as the
image background, but it isn't actually part of the image at all. What you
will then see as the 'background' when you look at the file you are creating
depends on how the image viewer you are using displays portions of the image
which don't exist, and perhaps your image viewer is showing these portions
as black.

Or, if you have not initialised the image with a color and you have saved
the file in a format that does not support non-existent portions of an image
(such as JPG) then the save process will apply a default color to those
missing portions and this is probably black.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top