2014-04-23 75 views
0

這應該是非常直接和容易做到的,但它並沒有令人害怕的工作。我想要做的就是畫一些東西到PictureBox,然後獲得PictureBox內容的快照。下面是我有:DrawToBitmap爲什麼不繪製其他控件的內容?

Graphics g = pictureBox1.CreateGraphics(); 
g.DrawImage(_image, _imageRectangle); 

using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, g)) 
{ 
    pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle); 
    bmp.Save(_filePath); 
} 

繪製圖像出現在畫面精細,但一個空白PictureBox被吸引到_filePath。這是怎麼回事?

+0

如果我猜,我會說pictureBox1.ClientRectangle會有問題。如果要用新的矩形(0,0,pictureBox1.Width,pictureBox1.Height)替換它,會怎麼樣? – attila

+1

不要使用'CreateGraphics' - 您需要處理'Paint'事件並在那裏的圖片框上繪製圖片。 – Blorgbeard

回答

0

你可以這樣來做:

Graphics g = Graphics.FromImage(pictureBox1.Image); 
g.DrawLine(Pens.Yellow, 0,0, 600,600); 

using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, g)) 
{ 
    pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle); 
    bmp.Save(@"C:\test.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 
} 
+0

...會不會有一條黃線扔圖像? –

+0

是的,我正在通過圖像繪製一條黃線以表明它的工作原理。我沒有你的_image或_imageRectangle,所以我替換了另一個繪圖函數。 –

相關問題