2011-05-20 82 views
3

我正在研究一個簡單的繪畫應用程序。除了保存,我得到了一切工作。我正在做一個面板內的所有繪畫操作。我需要將它保存爲圖像。這個怎麼做?在繪畫應用程序c中保存圖像#

+4

你使用的是winforms還是wpf? – aL3891 2011-05-20 09:04:09

回答

7

使用此代碼

Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);//to create bmp of same size as panel 
Rectangle rect=new Rectangle(0,0,panel1.Width,panel1.Height); //to set bounds to image 
panel1.DrawToBitmap(bmp,rect);  // drawing panel1 imgae into bmp of bounds of rect 
bmp.Save("C:\\a.png", System.Drawing.Imaging.ImageFormat.Png); //save location and type 
+0

非常感謝,它的工作 – Kanishka 2011-05-20 10:04:39

1

事情是這樣的:

public void SaveAs() 
    { 
     SaveFileDialog diag = new SaveFileDialog(); 
     DialogResult dr = diag.ShowDialog(); 

     if (dr.Equals(DialogResult.OK)) 
     { 

      string _filename = diag.FileName; 

      // filename not specified. Use FileName = ... 
      if (_filename == null || _filename.Length == 0) 
       throw new Exception("Unspecified file name"); 

      // cannot override RO file 
      if (File.Exists(_filename) 
       && (File.GetAttributes(_filename) 
       & FileAttributes.ReadOnly) != 0) 
       throw new Exception("File exists and is read-only!"); 

      // check supported image formats 
      ImageFormat format = FormatFromExtension(_filename); 
      if (format == null) 
       throw new Exception("Unsupported image format"); 

      // JPG images get special treatement 
      if (format.Equals(ImageFormat.Jpeg)) 
      { 
       EncoderParameters oParams = new EncoderParameters(1); 
       oParams.Param[0] = new EncoderParameter(
        System.Drawing.Imaging.Encoder.Quality, 100L); 
       ImageCodecInfo oCodecInfo = GetEncoderInfo("image/jpeg"); 
       yourImage.Save(_filename, oCodecInfo, oParams); 
      } 
      else 
       yourImage.Save(_filename, format); 

     } 
    } 
+1

究竟在哪裏讀取面板中的數據?我正在做一個面板上的所有圖像相關的操作, – Kanishka 2011-05-20 09:11:13

1

如果你使用WPF,你可以在RenderTargetBitmap採取alook。它可以使任何視覺成位圖,然後就可以使用@danyogiaxs保存awnser

CNC中

我還發現this SO post 做同樣的的WinForms

+0

我使用Windows窗體 – Kanishka 2011-05-20 09:11:35

相關問題