How to save a file from a Panel? (complicated sorta)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jacob Tryme
    New Member
    • Feb 2011
    • 3

    How to save a file from a Panel? (complicated sorta)

    Ok so i was following this tutorial here: http://www.youtube.com/watch?v=Ezea99FtCCE to make a little paint software. But When I go to save the picture on the Panel (not a PictureBox) It only saves the image or a blank file. Not the drawings i made. So i need help so it saves the image and the drawing. I dont know what code i have to paste since its basically the same as the videos.
    Except i save the images as jpgs or pngs.
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #2
    Well, apart from the fact that that "tutorial" was completely...cr ap, I think I can help you. I don't know how exactly you're drawing on your panel. Are you using the mousedown/mouseup event handlers and a graphics object? If so then the solution should be pretty simple. Within the method that saves the image, after you draw the panel to the bitmap, use the graphics object to redraw your drawing to the bitmap.

    Take a look at this program I wrote and look at the File > Save to PNG option:

    Own this domain today. We make your shopping experience easy. Friendly and quick customer service.


    Click and hld your mouse to start drawing. Scroll the wheel to resize the pen, release the mouse button to stop drawing. Right click to change either the bakground or pen color. Look at the options in the "Options" menu. As you draw each point that your mouse moves to is saved to a list, along with the amount of time between the current point and the previous point. This allows you to draw the drawing back, with the exact same timing as when it was first drawn, if you select that option. Is this something like what you're looking for?

    (make sure you do the Save to PNG option, not just "Save", that option will save the drawing to a .draw file that will only open in this program.)

    Comment

    • Jacob Tryme
      New Member
      • Feb 2011
      • 3

      #3
      How about i upload my project. Because its a bit differnet...
      Attached Files

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        It's not complicated, you're just not doing things right. There area few ways you can fix this.

        The first is by keeping a list of points that the user has drawn, then when you go to save loop though that list and draw each of those points to your bitmap (the one that you get from the panel1.DrawToBi tmap() method). That's a better choice.

        The second option is create a global Bitmap and Graphics object, call their constructors in the Form constructor (Form1()). Construct the bitmap to be the same width and height of the panel. set panel1's backgroundimage to the bitmap. Finally, Set the graphics object to Graphics.fromIm age(panel1.Back groundImage).

        Now, in your mouse move event remove the Graphics g = panel1.CreateGr aphics() and g.Dispose() calls (that's where you were going wrong), just do the g.Fillllipse(), then add this line:

        panel1.Invalida te(new Rectangle(e.x, e.y, 10, 10));

        Which will force the panel to repaint itself when you draw a point.

        Finally in your loadToolStripMe nuIem_Click event handler, comment out the panel1.Backgrou ndImage = Im line and add this one:

        g.DrawImage(Im, 0, 0, panel1.Width, panel1.Height);

        Basically what you're doing is creating a bitmap the same size as the panel, setting it as the background image, then creating a graphics object out of that image. Using any of the Graphics methods will draw to the bitmap (which is the panel's background image). You're literally drawing directly onto the background image. In your save event comment ut everything in the try block and do this:

        panel1.Backgrou ndImage.Save(sa ve.FileName);

        That's it. Now, keep in mind, this is a hacked together solution so that I wouldn't have to modify too much of your existing code. There are MUCH better (more efficient) ways of doing this (like the first option I gave you). I really recommend you take another look at my application, turn on the file list option and watch what happens as you draw. The source code for that program is available, but I don't want to give it to you just yet. Play around with what I've taught you here and see if you can learn anything.

        Comment

        • Jacob Tryme
          New Member
          • Feb 2011
          • 3

          #5
          Thanks that helped. I would like to see your code.

          Comment

          Working...