【问题标题】:Create a picture of the current form [duplicate]创建当前表单的图片[重复]
【发布时间】:2014-04-23 08:49:04
【问题描述】:

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

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    试试这个,

    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,如果您使用当前表单将屏幕写入磁盘)并将其绘制到位图!

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.drawtobitmap.aspx

    【讨论】:

    • 不错!它也适用于 Application.OpenForms[0].DrawToBitmap(...) 以获取正在运行的应用程序主窗口的屏幕截图。
    【解决方案2】:

    这将返回一个没有边框、滚动条、标题等的“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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-14
      • 2017-04-19
      • 2012-10-25
      • 2012-07-23
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多