How do I pass and reference a Panel?
-
I have a form with a panel called test_panel1. I need to display a bitmap on that panel and I have this code that works.
private void SetBitmap(Bitmap bmap) { Graphics graph = this.test_panel1.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
I now need to add another panel, lets call it test_panel2 and I need to do the same thing. But I'd like to call SetBitmap and pass it the panel to put the bitmap on Something like:SetBitmap(mybitmap,test_panel2)
orSetBitmap(mybitmap,test_panel1)
and have the function look likeprivate void SetBitmap(Bitmap bmap,Panel pnl) { Graphics graph = this.pnl.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
But C# doesn't like it and I don't know what the proper way to do this is? Any help is very very welcomed. -
I have a form with a panel called test_panel1. I need to display a bitmap on that panel and I have this code that works.
private void SetBitmap(Bitmap bmap) { Graphics graph = this.test_panel1.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
I now need to add another panel, lets call it test_panel2 and I need to do the same thing. But I'd like to call SetBitmap and pass it the panel to put the bitmap on Something like:SetBitmap(mybitmap,test_panel2)
orSetBitmap(mybitmap,test_panel1)
and have the function look likeprivate void SetBitmap(Bitmap bmap,Panel pnl) { Graphics graph = this.pnl.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
But C# doesn't like it and I don't know what the proper way to do this is? Any help is very very welcomed.Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
-
Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
-
Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
Hi, if you create an instance of a class that offers a public Dispose() method, you MUST call that method when you're done. This also applies to Control.CreateGraphics() and Graphics.FromImage() and the like !!! :)
Luc Pattyn [My Articles]
-
Try this: private void SetBitmap(Bitmap bmap, Panel pn) { Graphics graph = pn.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
This won't work properly. If the form is invalidated, the panel will be erased, unless this is called from a paint event, in which case, it should just use the existing graphics object.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
What you've been told will not work if your form is ever obscured by another form.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I have a form with a panel called test_panel1. I need to display a bitmap on that panel and I have this code that works.
private void SetBitmap(Bitmap bmap) { Graphics graph = this.test_panel1.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
I now need to add another panel, lets call it test_panel2 and I need to do the same thing. But I'd like to call SetBitmap and pass it the panel to put the bitmap on Something like:SetBitmap(mybitmap,test_panel2)
orSetBitmap(mybitmap,test_panel1)
and have the function look likeprivate void SetBitmap(Bitmap bmap,Panel pnl) { Graphics graph = this.pnl.CreateGraphics(); graph.DrawImage(bmap, 0, 0); }
But C# doesn't like it and I don't know what the proper way to do this is? Any help is very very welcomed.You should handle the paint event of the panel, and draw the bitmap onto the panel there. CreateGraphics will not redraw the image if your form is obscured by another form, as no paint event will occur.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )