2014-04-23 44 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,所以我替换了另一个绘图函数。 –

相关问题