At the moment I open automatically a new form, loading a chart, make a screenshot only in RAM and sending it to my private chat in telegram. All good so far, but is there a way, to open the form silent (also not shown and not focusing / invisible) and screenshot the silent form?
This is atm my code and working fine but without a silent function. anyone can help me?
_f2 = new Form2();
_f2.Show();
while(true)
{
if(Form2Closed == true)
{
Form2Closed = false;
break;
}
await Task.Delay(1000);
}
// loading chart stuff
var frm = Form2.ActiveForm;
using (MemoryStream stream = new MemoryStream())
{
using (var bmp = new Bitmap(frm.Width, frm.Height))
{
frm.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(stream, ImageFormat.Png);// "screenshot.png");
stream.Seek(0, SeekOrigin.Begin);
bmp.Dispose();
// sending stuff
}
}
>Solution :
You can make the form invisible by setting its Opacity property to 0:
var frm = new Form2();
frm.ShowInTaskbar = false;
frm.Opacity = 0;
frm.Show();
using (var bmp = new Bitmap(frm.Width, frm.Height))
{
frm.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
// Do something with the bitmap.
}
The DrawToBitmap method should work just fine even while the form is hidden.
Note that it would also work if you set the Visible property to false (e.g., by calling frm.Hide();) right after showing the form, but that would cause the form to appear for a split second. Setting Opacity to 0 before calling Show() ensures that the form is invisible from the get-go. Alternatively, you could override SetVisibleCore as explained here.