As I could not reproduce the problem with MSPaint.exe and the clipboard, here is my test program, complete, reduced to just one file (not counting project file):
namespace HideAndCopyForm {
using System;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {
public MainForm() {
Button btn = new Button();
btn.Text = "&Test";
btn.Parent = this;
btn.Click += (sender, eventArgs) => {
this.Opacity = 0;
System.Windows.Forms.Clipboard.SetImage(ScreenShot());
this.Opacity = 1;
};
}
Image ScreenShot() {
var screenSize = Screen.PrimaryScreen.Bounds.Size;
var image = new Bitmap(screenSize.Width, screenSize.Height);
using (var g = Graphics.FromImage(image)) {
g.CopyFromScreen(Point.Empty, Point.Empty, screenSize);
}
return image;
}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
Instructions: create project from scratch, makes sure there are only 3 referenced assemblies shown above, remove all files, add exactly one source file shown above.
Interesting observation:
[STAThread]
is essential for the clipboard operation, it cannot work in
[MTAThread]
.
No problem if the application window is maximized, no problem if MSPaint.exe window is in background.
—SA