8

I have panel and various controls on it. I would like to save an image of this panel into a file, how can I do this ?

Ineed to do something like screenshot, but I need just image of certain panel in my application and I want to do this on a button click in my app.

Best regards, Primoz


EDIT: I also draw on this panel using this code

            Graphics g = chartTemperature.CreateGraphics();    
            g.DrawLine(p, prevPoint, e.Location);
            prevPoint = e.Location;

But then I don't get this into image. Why, and how to fix this ?


EDIT 2:

namespace Grafi
{
    public partial class Form1 : Form
    {

        bool isDrawing = false;
        Point prevPoint;

        public Form1()
        {
            InitializeComponent();
        }

        private void chartTemperature_MouseDown(object sender, MouseEventArgs e)
        {
            isDrawing = true;
            prevPoint = e.Location;
        }

        private void chartTemperature_MouseMove(object sender, MouseEventArgs e)
        {
            Pen p = new Pen(Color.Red, 2); 
            if (isDrawing)
            {
                Graphics g = chartTemperature.CreateGraphics();    
                g.DrawLine(p, prevPoint, e.Location);
                prevPoint = e.Location;

                numOfMouseEvents = 0;              
            }
            p.Dispose();
        }

        private void chartTemperature_MouseUp(object sender, MouseEventArgs e)
        {
            isDrawing = false;
        }
    }
} 

This is my drawing code to draw a custom line onto a Chart. Can you please help me to do it proper way ?

Primoz
  • 4,079
  • 17
  • 56
  • 67

2 Answers2

14

Use the Control.DrawToBitmap() method. For example:

    private void button1_Click(object sender, EventArgs e) {
        using (var bmp = new Bitmap(panel1.Width, panel1.Height)) {
            panel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
            bmp.Save(@"c:\temp\test.png");
        }
    }
Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • This doesn't work completly for me. I added problems that I have into my original question. – Primoz Nov 12 '10 at 12:21
  • 1
    You shouldn't be drawing like this. Try for example minimizing, then restoring the window. The line is gone. Same thing will happen when you move another window across yours on XP. Use the control's Paint event instead. – Hans Passant Nov 12 '10 at 12:32
  • I edited my initial question to add my drawing code to draw a custom line onto a Chart. Can you please help me to do it proper way ? – Primoz Nov 12 '10 at 12:47
  • 1
    Please start a new question about this. – Hans Passant Nov 12 '10 at 12:58
  • I opened it. http://stackoverflow.com/questions/4164864/what-is-the-proper-way-to-draw-a-line-with-mouse-in-c – Primoz Nov 12 '10 at 13:02
3

In response to your edit:

If you are drawing on the panel using a Graphics object returned by the CreateGraphics method, your graphics are not permanent. Anything that you draw on the object will be erased the next time that the control is redrawn. (For more detailed information on this subject, see: https://web.archive.org/web/20131226033137/http://bobpowell.net/picturebox.aspx and https://web.archive.org/web/20141006045615/http://bobpowell.net/creategraphics.aspx)

When you use the DrawToBitmap method as suggested by Hans Passant's answer, the panel control is getting redrawn, which is causing your drawings to be lost.

Instead, if you want your drawings to be permanent, you need to handle the Paint event of the panel control. This event is raised every time that the control needs to be redrawn, and provides an instance of PaintEventArgs that contains a Graphics object you can use to draw permanently on the control's surface in the same way that you were using the Graphics object returned by the CreateGraphics method.

Once you've corrected your drawing code, you can use Hans's solution.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574