底线是通过主窗口按下按钮,打开一个窗口,填充,制作一个屏幕,然后关闭(自动)。而问题是如果屏幕的代码放在Loaded事件中,那么窗口就没有时间完全打开,数据也不会显示在屏幕上。解决方案 2,和问题 2,我将屏幕的代码放在窗口关闭事件中。如果您使用 X 按钮自己关闭窗口,则获得屏幕。并且如果将 Close() 命令放在窗口构造函数中,那么当窗口被调用时,编译器会发誓在窗口关闭后不能调用 Show。
一般来说,如何正确解决这个问题。必需:打开、屏蔽、关闭。窗口代码
public PrintWindow(string[] info)
{
InitializeComponent();
//заполнение окна через массив info
Close();
}
private void Window_Closed(object sender, EventArgs e)
{
//код для скрина
double screenLeft = this.Left + 9;//SystemParameters.VirtualScreenLeft;
double screenTop = this.Top + 30;//SystemParameters.VirtualScreenTop;
double screenWidth = this.Width;//SystemParameters.VirtualScreenWidth;
double screenHeight = this.Height;//SystemParameters.MaximizedPrimaryScreenHeight;//SystemParameters.VirtualScreenHeight;
using (Bitmap bmp = new Bitmap((int)screenWidth, (int)screenHeight))
{
using (Graphics g = Graphics.FromImage(bmp))
{
string filename = FIO.Text + ".png";
Opacity = 0;
g.CopyFromScreen((int)screenLeft, (int)screenTop, 18, 39, /*(int)screenWidth,(int) screenHeight,*/ bmp.Size);
bmp.Save(filename);
Opacity = 1;
}
}
}
我建议你使用 WPF 工具:
无论屏幕上实际显示什么,这都可以让您拍照。但是,是的,您只需要在加载控件后调用该方法(事件
Loaded):请注意,无论 Windows 缩放设置如何,您都可以在此处获得原始比例的图像。如果您想获得“文字”图片,则需要获取缩放因子并使用它们: