473,395 Members | 1,956 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,395 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 31080
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.