mardi 24 février 2015

C# .NET How to capture (screen shot) external application's window without using unmanaged code (user32.dll)

I am trying to capture/copy (screen shot) an external application and save it to a PictureBox. App #1, will be using .NET 4.0.


Application #1 enter image description here


Load list box


C# pseudo Code for listbox:



using System.Web.UI.WebControls;

public Form1()
{
InitializeComponent();

foreach (Process process in Process.GetProcesses())
{
if (string.IsNullOrEmpty(process.MainWindowTitle))
continue;

listBox1.Items.Add(new ListItem(process.MainWindowTitle,
process.MainWindowHandle.ToString()));
}

}


Once user makes selection


C# pseudo code for listbox's button or event.



IntPtr intptr = _listBox1.Items[listBox1.SelectedIndex].value;

Graphics g = Graphics.FromHwnd(intptr);

// get width and height of app #2
int width = (int)g.VisibleClipBounds.Width;
int height = (int)g.VisibleClipBounds.Height;

Size s = new Size(width, height);

// create new bitmap
Bitmap wincapture = new Bitmap(width, height, g);


See notes below, needs code to copy App #2, to wincapture



// once App #2 has been captured, show image in picture box.
PictureBox1.Image = wincapture;


Question


How do I copy Application #2's window and save it in Application #1's Picture Box.


I am unable to use unmanaged code ex: user32.dll.


I am looking for a .NET answer.


Notes


I have tried using the following and it does not work or its missing something:



g.CopyFromScreen(0, 0, 0, 0, new Size(width, height));

g.DrawImage(wincapture, 0, 0, width, height);

Aucun commentaire:

Enregistrer un commentaire