C# 如何在没有user32.dll(非托管代码)的情况下捕获屏幕截图

C# 如何在没有user32.dll(非托管代码)的情况下捕获屏幕截图,c#,.net,winforms,.net-4.0,screenshot,C#,.net,Winforms,.net 4.0,Screenshot,概述 我正在尝试捕获外部应用程序的屏幕截图,并将图像加载到.NET PictureBox user32.dll非托管代码 有很多使用user32.dll的例子,我正在寻找一个不使用user32.dll文件的.NET答案 附录1 我在下面提供了用于演示的示例代码。当程序被执行时,它解析进程并将带有有效窗口标题的进程保存到下拉列表中 当您从下拉列表中选择应用程序名称并单击按钮时,它将尝试捕获屏幕截图应用程序并加载到.NET PictureBox中 表格1.cs 使用系统; 使用System.Col

概述 我正在尝试捕获外部应用程序的屏幕截图,并将图像加载到.NET PictureBox

user32.dll非托管代码 有很多使用user32.dll的例子,我正在寻找一个不使用user32.dll文件的.NET答案

附录1 我在下面提供了用于演示的示例代码。当程序被执行时,它解析进程并将带有有效窗口标题的进程保存到下拉列表中

当您从下拉列表中选择应用程序名称并单击按钮时,它将尝试捕获屏幕截图应用程序并加载到.NET PictureBox中

表格1.cs

使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用系统图;
使用System.Windows.Forms;
命名空间Windows窗体应用程序3
{
公共部分类Form1:Form
{
字典应用程序=新建字典();
公共表格1()
{
初始化组件();
foreach(Process.getprocesss()中的进程)
{
if(string.IsNullOrEmpty(process.MainWindowTitle))
继续;
apps.Add(process.MainWindowTitle、process.MainWindowHandle);
comboBox1.Items.Add(process.MainWindowTitle);
}
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
IntPtr IntPtr=apps[comboBox1.Items[comboBox1.SelectedIndex].ToString();
Graphics g=Graphics.FromHwnd(intptr);
//获取应用程序2的宽度和高度
int width=(int)g.VisibleClipBounds.width;
int height=(int)g.VisibleClipBounds.height;
尺寸s=新尺寸(宽度、高度);
//创建新位图
位图wincapture=新位图(宽度、高度、g);
//代码已尝试。
//g、 CopyFromScreen(0,0,0,0,新尺寸(宽度、高度));
//g、 DrawImage(wincapture、0、0、宽度、高度);
//捕捉到应用程序2后,在图片框中显示图像。
pictureBox1.Image=wincapture;
}
}
}
Form1.Designer.cs

命名空间窗口窗体应用程序3
{
部分类Form1
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.pictureBox1=新系统.Windows.Forms.PictureBox();
this.button1=new System.Windows.Forms.Button();
this.comboBox1=new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
这个.SuspendLayout();
// 
//pictureBox1
// 
this.pictureBox1.Location=新系统图纸点(13,13);
this.pictureBox1.Name=“pictureBox1”;
this.pictureBox1.Size=新系统图纸尺寸(267183);
this.pictureBox1.SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex=0;
this.pictureBox1.TabStop=false;
// 
//按钮1
// 
this.button1.Location=新系统图纸点(102227);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(82,23);
this.button1.TabIndex=2;
this.button1.Text=“查看外部应用程序”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
// 
//组合框1
// 
this.comboBox1.FormattingEnabled=true;
this.comboBox1.Location=新系统.图纸.点(13202);
this.comboBox1.Name=“comboBox1”;
this.comboBox1.Size=新系统图纸尺寸(267,21);
this.comboBox1.TabIndex=3;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(292256);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name=“Form1”;
this.Text=“App#1”;
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button按钮1;
private System.Windows.Forms.ComboBox comboBox1;
}
}

这是我用来截图的代码