C# 创建当前窗体的图片

C# 创建当前窗体的图片,c#,winforms,C#,Winforms,是否可以从当前表单创建图像?例如,在onLoad上是否在根文件夹中创建窗体当前状态的图片?将用户看到的内容存储为图片 试试这个 using (var bmp = new Bitmap(this.Width, this.Height)) { this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); bmp.Save(@"c:\temp\screenshot.png");

是否可以从当前表单创建图像?例如,在onLoad上是否在根文件夹中创建窗体当前状态的图片?将用户看到的内容存储为图片

试试这个

using (var bmp = new Bitmap(this.Width, this.Height)) 
{
            this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
            bmp.Save(@"c:\temp\screenshot.png");
}
这将使用窗体的宽度和高度(如果您使用当前窗体将屏幕写入磁盘,则为This.width,This.height)并将其绘制为位图

试试这个

using (var bmp = new Bitmap(this.Width, this.Height)) 
{
            this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
            bmp.Save(@"c:\temp\screenshot.png");
}
这将使用窗体的宽度和高度(如果您使用当前窗体将屏幕写入磁盘,则为This.width,This.height)并将其绘制为位图


这将返回一个没有边框、滚动条、标题等的“FormShot”。要保存它,只需使用位图。保存方法

public static Bitmap TakeWindowScreenshot(Form window)

 {
   var b = new Bitmap(window.Width, window.Height);
   this.DrawToBitmap(b, new Rectangle(0, 0, window.Width, window.Height));

   Point p = window.PointToScreen(Point.Empty);

   Bitmap target = new Bitmap( window.ClientSize.Width, window.ClientSize.Height);
   using (Graphics g = Graphics.FromImage(target))
   {
     g.DrawImage(b, 0, 0,
                 new Rectangle(p.X - window.Location.X, p.Y - window.Location.Y, 
                               target.Width, target.Height),  
                GraphicsUnit.Pixel);
   }
   b.Dispose();
   return target;
}

这将返回一个没有边框、滚动条、标题等的“FormShot”。要保存它,只需使用Bitmap.save方法

public static Bitmap TakeWindowScreenshot(Form window)

 {
   var b = new Bitmap(window.Width, window.Height);
   this.DrawToBitmap(b, new Rectangle(0, 0, window.Width, window.Height));

   Point p = window.PointToScreen(Point.Empty);

   Bitmap target = new Bitmap( window.ClientSize.Width, window.ClientSize.Height);
   using (Graphics g = Graphics.FromImage(target))
   {
     g.DrawImage(b, 0, 0,
                 new Rectangle(p.X - window.Location.X, p.Y - window.Location.Y, 
                               target.Width, target.Height),  
                GraphicsUnit.Pixel);
   }
   b.Dispose();
   return target;
}

美好的它也适用于Application.OpenForms[0].DrawToBitmap(…),以获取正在运行的应用程序主窗口的屏幕截图。很好!它还适用于Application.OpenForms[0].DrawToBitmap(…),以获取正在运行的应用程序主窗口的屏幕截图。