How to create a bitmap programmatically in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bradford

    #1

    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.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
  • Jeff Louie

    #2
    Re: How to create a bitmap programmaticall y in C#

    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!

    Comment

    • MS News \(taruntius\)

      #3
      Re: How to create a bitmap programmaticall y in C#

      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" <annonymous@ms. com> wrote in message news:OCnuh8aIFH A.2456@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

      Comment

      • Bradford

        #4
        Re: How to create a bitmap programmaticall y in C#

        Thanks for the replys. I'm now able to move forward with my project.
        Much appreciated!
        Bradford

        Comment

        Working...