473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to create a bitmap programmatically in C#

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
3 31068
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
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
Thanks for the replys. I'm now able to move forward with my project.
Much appreciated!
Bradford
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Question with BinaryReader | last post by:
Is someone can give me example? I want to create Bitmap with pixel data saved in a binary file, the data file only have red data block and green data block. I have implemented it with VC6.0, now...
2
by: Soja | last post by:
How can i create a Bitmap from a rawbuffer-BYTE* (from framegrapper). I do following: Bitmap *myBit2 = new Bitmap(wImage,hImage,wImage*sizeof(BYTE),PixelFormat::Format8bppIndexed...
3
by: mark | last post by:
I want plot points on a cartesian surface with single pixels. I have been unable to find a graphics member which draws points. (Is there one?) I would like to create a bitmap one element wide and...
2
by: Willie jan | last post by:
Hi, i try to create a bitmap with transparent regions, but it does not work. the transparent region turns out to be black. anybody some ideas?? Dim bmap As...
2
by: Melisa | last post by:
Hi, How can i create bitmap of a window form with all its child controls without showing this form? 1. I am trying to create bitmap image of a window form. 2. I am creating a new instance of...
0
by: Victor Reboucas | last post by:
Hi, I'm a VB.Net developer and cannot find a way to create a bipmap from a pointer... My application does video rendering using Windows media encoder (that's out of the content of this message)....
8
by: William Foster | last post by:
Good evening all, I am creating a program with the ability to select an option from a ComboBox which will then create a secondary list of options in a CheckedListBox. Previously when I have...
1
by: diwakar09 | last post by:
how to create bitmap in vc++ (WIN32)of the content in the running window could you please tell me the steps and then i have to save this bitmap into the hard disk please help me. Thanx
3
by: radu | last post by:
i need a bit of help with few lines of code. 1. i have integer array a = numbers from 0 to 256 2. i have to create a 256 colours bitmap of 500x500 pixels, asign colours as matrix a. ex. bitmap at...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.