Save image drawn on Panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kikx
    New Member
    • Aug 2012
    • 8

    Save image drawn on Panel

    Hello

    I have a paint program (winform appl.) made in C#/GDI+, where using MouseUp/Down/Move events I draw on a panel. Everything I need is functioning but I need help in saving (as a .bmp file) whatever is drawn on the panel. Could somebody help me with this? It would be appreciated very much. Source code available when and if needed. Thank you!
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Get the control as a bitmap and save it into a file:

    Code:
    public void SaveBitmap(Control control, string fileName)    
    {           
    	Graphics gr1 = control.CreateGraphics();
    	Bitmap bmp1 = new Bitmap(control.Width, control.Height);
    	control.DrawToBitmap(bmp1, new Rectangle(0, 0, control.Width, control.Height));                
    	bmp1.Save(fileName);
    }
    HTH

    Comment

    • kikx
      New Member
      • Aug 2012
      • 8

      #3
      I'm afraid I don't understand your reply well enough, I am quite the beginner with this..

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Your panel is a control, isnt it?

        well, what we are doing here is:

        1) create your control graphics and call them gr1
        2) create a bitmap, the same size as your control
        3) use the DrawToBitmap method
        4) save it as a *.bmp file

        This procedure will do it; just call it with the control and the file name as parameters.

        Comment

        • kikx
          New Member
          • Aug 2012
          • 8

          #5
          First, I have already created the graphics in the Form1 constructor (gr=panel1.Crea tegraphics();), does that change anything in the code you wrote?
          Second, is it possible to combine that save method you've written with a save dialog?

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by kikx
            First, I have already created the graphics in the Form1 constructor (gr=panel1.Crea tegraphics();), does that change anything in the code you wrote?
            Second, is it possible to combine that save method you've written with a save dialog?
            Sure, create a SaveFileDialog, call it, save the .file into a string and then call the method using that string.

            Comment

            • kikx
              New Member
              • Aug 2012
              • 8

              #7
              @kadghar
              I have tried but looks like I can't get it to work. Would you be willing to look at the source code and tell me how to make it work with a save dialog? I would be very thankful!

              Comment

              Working...