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
}
}
2
Answers
check this thread:
How to take a screenshot of a WPF control?
RenderTargetBitmap.Render
should works with minimalised window :).You can make the form invisible by setting its
Opacity
property to 0: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 tofalse
(e.g., by callingfrm.Hide();
) right after showing the form, but that would cause the form to appear for a split second. SettingOpacity
to 0 before callingShow()
ensures that the form is invisible from the get-go. Alternatively, you could overrideSetVisibleCore
as explained here.