【发布时间】:2012-07-09 22:12:35
【问题描述】:
这是我的代码:
public string selectedProgram;
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, Rectangle rect);
private void button2_Click(object sender, EventArgs e)
{
Process[] process = Process.GetProcesses();
foreach (var p in process)
{
selectedProgram = listView1.SelectedItems.ToString();
Rectangle bonds = new Rectangle();
GetWindowRect(Handle, bonds);
Bitmap bmp = new Bitmap(bonds.Width, bonds.Height);
using (var gfx = Graphics.FromImage(bmp))
{
gfx.CopyFromScreen(bonds.Location, Point.Empty, bonds.Size);
pictureBox1.Image = bmp;
frm2.Show();
frm2.pictureBox1.Image = pictureBox1.Image;
}
}
我在GetWindowRect(Handle, bonds); 上收到错误消息或一些绿色突出显示:
A call to PInvoke function 'Screen Shot!WindowsFormsApplication1.Form3::GetWindowRect' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
如何解决这个问题,以便获取其他应用程序窗口的屏幕截图?
【问题讨论】:
-
在你的循环中你说
selectedProgram = listView1.SelectedItems.ToString()。这应该是SelectedItem(即不是复数),否则 selectedProgram 将始终是System.Windows.Forms.ListView.SelectedListViewItemCollection。
标签: c# winforms screenshot visual-c#-express-2010