Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取System.Drawing.Graphics对象绘制的图像?_C#_Graphics_System.drawing - Fatal编程技术网

C# 如何获取System.Drawing.Graphics对象绘制的图像?

C# 如何获取System.Drawing.Graphics对象绘制的图像?,c#,graphics,system.drawing,C#,Graphics,System.drawing,请参见以下方法: void Paint(System.Drawing.Graphics g) { //How can I start record what 'g' will draw to an image object? g.DrawLine(0,0,50,50); g.DrawImage(...); .. .. etc. } 现在,我如何才能获得关于“g”所画内容的图像? 谢谢:)你可以试试 using (Graphics g=Graphics.FromImage(inImage))

请参见以下方法:

void Paint(System.Drawing.Graphics g)
{
//How can I start record what 'g' will draw to an image object?
g.DrawLine(0,0,50,50);
g.DrawImage(...);
..
..
etc.

}
现在,我如何才能获得关于“g”所画内容的图像?
谢谢:)

你可以试试

using (Graphics g=Graphics.FromImage(inImage)) 
{ 
  g.Clear(Color.White); 
  g.DrawLine(0,0,50,50); 
} 
这将在图像上画一条线。只要确保图像足够大

此外,您还可以通过覆盖OnPaint事件并从eventArgs获取图形对象,直接绘制到窗体。

您可以这样做

Bitmap bmp;
...
{
 InitializeComponent();
 bmp = new Bitmap(this.Width,this.Height,Graphics.FromHwnd(this.Handle));
}

void Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
  Graphics g = Graphics.FromImage(bmp);
  g.DrawLine(0,0,50,50);
  ..
  .. 
  e.Graphics.DrawImage(bmp,0,0);
}

我认为你必须更具体一些,即你想在屏幕上查看图像吗?是否要将图像保存到文件中?假设我有一个图形对象“g”,并且我绘制了直线、矩形等,如果我想获得关于绘制内容的准备图像,则不需要重新绘制直线、矩形。。