By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

How to create a bitmap programmatically in C#

P: n/a
I have an Windows application that displays lots of different colored lines in a Panel by using the Graphics.DrawLine method. The location and length of each line comes from a database query that takes two minutes to complete.

I am using the Panel's Paint event to call my drawing method, which accepts PaintEventArgs as a parameter. The problem is that every time the Window is moved or hidden, it needs to be repainted, which takes a long time because the database is queried every time.

To avoid this, I thought that I would try to create a bitmap programmatically from the query in the Form's Load event, and load that into the panel whenever it is painted/repainted in order to prevent unnecessary requerying of the database. I don't know if this is a good solution and I don't know how exactly to go about it. Should I save the queried data to a Stream, or directly to a Bitmap? Can anyone point me in the right direction?

Bradford
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bradford... One approach is to store drawable objects in an array. Each
drawable object knows how to draw itself. On repaint, just iterate over
the array of drawable objects. So for a line, create a Line class that
implements IDrawable. Store the state of the line in the line object so
that the line knows where it goes and what it looks like. The OS clips
the drawing to the dirty drawing rect so that only the dirty portion of
the screen is redrawn. If you call invalidate the entire screen will be
redrawn.

Regards,
Jeff
The problem is that every time the Window is moved or hidden, it needs
to be repainted, which takes a long time because the database is queried
every time.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2

P: n/a
Well, creating a bitmap is not hard:

Bitmap B = new Bitmap(width, height);

And drawing to it is not that hard either:

B.SetPixel(x,y,Color.FromARGB(redvalue, greenvalue, bluevalue));

You can also get a Graphics object from the Bitmap, and use that Graphics object to draw to the bitmap with managed GDI calls. If you have to do stuff with lines and shapes, this is definitely the way to go:

using (Graphics G = Graphics.FromImage(myBitmap)) {
G.DrawLine(...);
G.FillElliipse(...);
// etc.
}

As to your final question, "Should I save the queried data to a Stream, or directly to a Bitmap?", it depends on what you need to do with that data. If all you ever need to do with those numbers is to draw your Bitmap, then you may as well just create the Bitmap when the form loads and keep that around. But later on while the form is running, if you want the user to be able to interact with that data in any way (say, allowing them to perform sums or averages or whatever on those numbers), then you need to keep the raw data around.

My inclination would probably be to keep both. You keep the bitmap around so that you can quickly handle move/resize/redraw situations, and you keep the data around so the user can do things with it. Also, this mechanism makes it easy for you to make your application periodically re-query the database re-generate the bitmap.
"Bradford" <an********@ms.com> wrote in message news:OC**************@TK2MSFTNGP09.phx.gbl...
I have an Windows application that displays lots of different colored lines in a Panel by using the Graphics.DrawLine method. The location and length of each line comes from a database query that takes two minutes to complete.

I am using the Panel's Paint event to call my drawing method, which accepts PaintEventArgs as a parameter. The problem is that every time the Window is moved or hidden, it needs to be repainted, which takes a long time because the database is queried every time.

To avoid this, I thought that I would try to create a bitmap programmatically from the query in the Form's Load event, and load that into the panel whenever it is painted/repainted in order to prevent unnecessary requerying of the database. I don't know if this is a good solution and I don't know how exactly to go about it. Should I save the queried data to a Stream, or directly to a Bitmap? Can anyone point me in the right direction?

Bradford
Nov 16 '05 #3

P: n/a
Thanks for the replys. I'm now able to move forward with my project.
Much appreciated!
Bradford
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.