2014-11-23 29 views
0

我想繪製一些窗體對象,然後創建該窗體的圖像並將其保存到文件夾。發佈捕獲Windows窗體繪圖到圖像C#

這是一個兩部分的問題......爲什麼不是正在繪製到Windows窗體的圖像顯示在創建的圖像上?另外第二個問題是,我怎樣才能創建一個Windows窗體繪圖的圖像,從說點0,0到點500,500 沒有背景....

這是我現在的代碼.. ...

Form draw = new Drawing();  // Opens the drawing form 
draw.Show(); 

     try 
     { 
      //Try to draw something... 
      System.Drawing.Pen myPen;  
      myPen = new System.Drawing.Pen(System.Drawing.Color.Red); 
      System.Drawing.Graphics formGraphics = draw.CreateGraphics(); 
      formGraphics.DrawLine(myPen, 0, 0, 500, 500); 
      myPen.Dispose(); 
      formGraphics.Dispose(); 

      var path = this.outputFolder.Text;  // Create variable with output path 

      if (!Directory.Exists(path)) 
      { 
       DirectoryInfo di = Directory.CreateDirectory(path); // Create path if it doesn't exist 
      } 

      using (var bitmap = new Bitmap(draw.Width, draw.Height)) // Creating the .bmp file from windows form 
      { 
       draw.DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); 
       bitmap.Save(path + "\\" + i + ".bmp"); 
      } 
     } 
     catch { } 

你能看到這裏有什麼不對嗎?什麼似乎是禁止將繪圖保存到.bmp文件?

提前致謝!

+0

您的位圖的形式的大小,但在其他方面無關的形成;也不確定第一部分與什麼有關; CreateGraphics幾乎總是以某種形式繪製東西的錯誤方式 – Plutonix 2014-11-23 13:29:24

+0

你是什麼意思,第一部分與任何東西無關......你能詳細說明一下嗎?你如何建議我畫一些表格,並記住它需要稍後保存爲圖像。 – user3684557 2014-11-23 13:32:34

+0

你*繪製*的形式*你*也必須繪製到位圖;該形式不是一個將保留在其他地方繪製的畫布。如果你不想要背景(這是否意味着背景顏色?),然後設置使用MakeTransparent來設置它 - 任何具有該顏色的控件也將在圖像中爲白色/無色。你還可能必須另存爲PNG以保存透明度 – Plutonix 2014-11-23 13:40:58

回答

0

我結束了使用此處顯示Saving System.Drawing.Graphics to a png or bmp

 Form draw = new Drawing();  // Opens the drawing form 
     draw.Show(); 
     try 
     { 
      var path = this.outputFolder.Text; // Path where images will be saved to 

      if (!Directory.Exists(path)) 
      { 
       DirectoryInfo di = Directory.CreateDirectory(path);  // Create a directory if it does not already exist 
      } 

      Bitmap bitmap = new Bitmap(Convert.ToInt32(1024), Convert.ToInt32(1024), System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
      Graphics g = Graphics.FromImage(bitmap); 

      System.Drawing.Pen myPen; 
      myPen = new System.Drawing.Pen(System.Drawing.Color.Red); 
      g.DrawLine(myPen, 0, 0, 1024, 1024); 

      bitmap.Save(path + "\\" + i + ".png", ImageFormat.Png); 

     } 
     catch { } 

的例子,現在已經完美地工作對我來說:d