473,804 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a bitmap programmaticall y in C#

I have an Windows application that displays lots of different colored lines in a Panel by using the Graphics.DrawLi ne 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 programmaticall y 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 31113
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.FromIm age(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******** ******@TK2MSFTN GP09.phx.gbl...
I have an Windows application that displays lots of different colored lines in a Panel by using the Graphics.DrawLi ne 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 programmaticall y 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
575
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 I want to rewrote it with C#. My question is 1. using of Bitmap (Int32, Int32, Int32, PixelFormat, IntPtr) 2. how to implement memory copy efficiently in C#
2
2134
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 ,(IntPtr)pbyteBuffer); pictureBox->set_Image(myBit2 );
3
1973
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 one element high where the element is black and use the drawimage method. I don't want to read the bitmap from a file. Is there a way to create this bitmap programmaticaly? --
2
2076
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 Bitmap(100,100,Imaging.PixelFormat.Format32bppPArgb) Dim g As Graphics = Graphics.FromImage(bmap) g.Clear(Color.Transparent)
2
8378
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 Form at runtime. 3. Then adding several controls like Button, RadioButton,CheckBox etc to it. 4. Now i want to create bitmap with all controls showing on it which are added to this form.
0
1285
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). Using a "DataView" object I can attach a "video viewer" to about every control in a Form, using a "integer reference" to attach the viewer object to the control you want to use as a viewer. Question: is there any way to use the...
8
3764
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 needed to create multiples of this type of user option I have simply hard coded three or four set options for the user. However, I would like to have the option for the user to have as many options available as they like. In order to do this I...
1
2078
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
3724
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 pixel at row 3 col 5 = colour number is a and save it to disk as a 256 colour bitmap without any compresion as c:\a.bmp. no interface. i have microsoft visual c# 2008 express edition. help? thank you.
0
9712
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10089
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5530
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.