Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. save bitmap ?

save bitmap ?

Scheduled Pinned Locked Moved C#
questiongraphics
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kendao
    wrote on last edited by
    #1

    i draw some rectangles and lines into the the panel object. but how can i save the content in the panel object to a bitmap file ? thanks....::)

    M H 2 Replies Last reply
    0
    • K kendao

      i draw some rectangles and lines into the the panel object. but how can i save the content in the panel object to a bitmap file ? thanks....::)

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      If you want to keep what you've drawn to your panel, then I guess you'll have to take a different approach altogether. Am I right that you're calling myPanel.CreateGraphics() and then use this Graphics object for painting? In this case, the actions are not recorded anywhere and your panel will be blank after the next paint event it receives (try hiding your panel behind another window and then restoring it...). I think you should use a Bitmap from the start and paint onto this Bitmap using a Graphics object retrieved by Graphics.FromImage(). In the paint event handler of your panel you paint the bitmap. That way you can also call myBitmap.Save(...) to save the contents of the bitmap. Regards, mav

      1 Reply Last reply
      0
      • K kendao

        i draw some rectangles and lines into the the panel object. but how can i save the content in the panel object to a bitmap file ? thanks....::)

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        What 'mav' said will work, but there's several problems with his approach. If you draw on a Graphics object that you obtained from Control.CreateGraphics, your elements will be drawn. But if the form needs repainting (perhaps it was covered up by another form), they won't be redrawn. You need to perform your drawing in an override of OnPaint (which is passed a PaintEventArgs that has a Graphics property that you should use for painting). In order to be able to save that same code to a Bitmap class and then to a file, you'll want to modularize your code like so:

        private void PaintRectangles(Graphics g)
        {
        g.DrawRectangle(...); // Draw rectangles or whatever here
        }
        protected override void OnPaint(PaintEventArgs e)
        {
        base.OnPaint(e);
        PaintRectangles(e.Graphics);
        }
        private void saveBtn_Click(object sender, EventArgs e)
        {
        using (Bitmap bmp = new Bitmap(panel1.Size.Width,
        panel1.Size.Height))
        {
        using (Graphics g = Graphics.FromImage(bmp))
        {
        PaintRectangles(g);
        }
        bmp.Save("filename.png", ImageFormat.Png); // Save as PNG file
        }
        }

        Now you've encapsulated your rectangle drawing code into a single method that can draw onto any Graphics object, including the form or panel itself (this is only an example, mind you) or a Bitmap. The using statements above make sure that both the Bitmap and Graphics objects are disposed (very important, otherwise memory may not be freed when necessary and unmanaged objects will linger, causing memory leaks) even if an exception is thrown.

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups