title: How to save Graphics object as image in C#?
body: <p>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 ? </p>
  
  <p>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.</p>
  
  <p>Best regards, Primoz </p>
  
  <hr>
  
  <p>EDIT:
  I also draw on this panel using this code</p>
  
  <pre><code>            Graphics g = chartTemperature.CreateGraphics();    
              g.DrawLine(p, prevPoint, e.Location);
              prevPoint = e.Location;
  </code></pre>
  
  <p>But then I don't get this into image. Why, and how to fix this ? </p>
  
  <hr>
  
  <p>EDIT 2:</p>
  
  <pre><code>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;
          }
      }
  } 
  </code></pre>
  
  <p>This is my drawing code to draw a custom line onto a Chart. Can you please help me to do it proper way ?</p>