Click to See Complete Forum and Search --> : bitmap


rowgram
December 7th, 2005, 02:46 PM
I've got a bitmap of an XY plot that I would like to fix in place on a form (at some point later, there will be a cursor that moves around the plot depending upon the xy values input).

Here's the loading of the bitmap :
Graphics* gr = CreateGraphics();
Bitmap* plot = new Bitmap(S"plot.bmp");
gr->DrawImage(plot, 50, 20);
this->Update();

I can incorporate this 2 ways :

via a button click event handler :

in this case, everything works fine except any window opened in fron tof the form causes the bitmap to erase - it's as if the this->Update() function is performing a re-paint ?


via the Form_Paint event handler (this would be my preferred way) :

in this case, the form loads fine, then I open a file via OpenFileDialog & Iget an unhandled exception : invalid parameter for the statement :
if(ofd->ShowDialog==DialogResult::OK)
This occurs even if I change OK to Cancel or anyother Dialog Result value.
It seems like the constant re-painting of the bitmap doesn't allow for any other dialog to operate - is this true ?

Regards,
ak

Marc G
December 7th, 2005, 03:32 PM
Using the paint handler is defintely the right way to do this. Basically: all drawing in windows should be done in your paint handler to avoid the 'tearing' problem when moving a window in front of your window.

rowgram
December 7th, 2005, 04:02 PM
This is my intent, but why does the introduction of an openfile dialog cause the error ? Am I loading & updating the bitmap correctly ?
Does this->Update() not allow for any other process ?

Marc G
December 7th, 2005, 04:13 PM
You should not call Update in your paint handler, it will recursively keep calling your paint handler.

rowgram
December 7th, 2005, 04:33 PM
Here is what I have inside the paint handler :

Graphics* gr = CreatGraphics();
Bitmap* plot = new Bitmap(S"plot.bmp");
gr->DrawImage(plot, 50, 20);
Update();

Is this the way to use Update() ?

Marc G
December 8th, 2005, 02:58 AM
No. You should not call Update in your paint handler. Remove it.
When you do call Update in your paint handler, your paint handler will be called recursively.

rowgram
December 8th, 2005, 08:46 AM
I'm confused - could you please clarify your statement ?

In your earlier message you say to call Update in the paint handler, your latest statement says not to call Update in the paint handler.

Marc G
December 8th, 2005, 09:14 AM
Sorry, that was my fault. I made a typo in my first reply and have now fixed it. :blush:

So to set it straight: do not call Update in your paint handler.

rowgram
December 8th, 2005, 10:16 AM
No problem. OK, so now I have :

Graphics*gr = CreateGraphics();
Bitmap* plot = new Bitmap(S"plot.bmp");
gr->DrawImage(plot, 50, 20);

inside Form_Paint. But there still seems to be a conflict with OpenFileDialog.
The program compiles, runs & I see the bitmap displayed - you're right, now there is no 'tearing', but the problem starts when I open a file that I need : it results in an unhandled exception : Invalid Parameter used . This shows up on the statement :

if (ofd->ShowDialog()==DialogResult::OK)

(ofd is my instance of OpenFileDialog. I get the feeling there is some competing of resources...but I don't know, I'm new to C++ . Any suggestions ?

rowgram
December 8th, 2005, 06:19 PM
Here's what I got to work :

inside the Form_Paint handler :

Graphics * gr = e->Graphics;
if (bitmap !+ NULL)
{ gr->DrawImage(bitmap, x-left, x-right, width, height);}

inside a button click handler :

bitmap = new Bitmap(S"file name.bmp");
this->Invalidate();
this->Update();

inside the Form definition :
private: static Bitmap* bitmap;

Marc G
December 9th, 2005, 03:34 AM
if (bitmap !+ NULL)
I guess you mean:
if (bitmap != NULL)