473,796 Members | 2,532 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 31111
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
2075
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
8377
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
1283
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
3763
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
3723
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
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
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...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2924
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.