1

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
    }
}
Kababum
  • 47
  • 4
  • Unclear which technology this is meant to be. WPF? WebForms? WinForms? Modern UI? Something different? – Uwe Keim Nov 02 '22 at 16:09
  • (a) don't call `Show`. (b) you won't be able to use `ActiveForm`, because an invisible window can't be active. But that isn't a problem because you already have a reference to the form object. – Ben Voigt Nov 02 '22 at 16:10
  • @UweKeim: The combination of `Form` and `DrawToBitmap` points to WinForms. – Ben Voigt Nov 02 '22 at 16:11
  • @UweKeim I did not know that this is important, sorry... Actually this is WinForms (Net..) – Kababum Nov 02 '22 at 16:11
  • [This](https://stackoverflow.com/questions/32798971/displaying-a-2d-data-array-from-a-console-application/33414809#33414809) may be helpful – TaW Nov 02 '22 at 16:12
  • 1
    Side note: because `bmp` is wrapped in a `using` block, you don't need to call `.Dispose()` because `using` takes care of that for you. – 41686d6564 stands w. Palestine Nov 02 '22 at 16:28

2 Answers2

3

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.

  • 1
    Don't call `Show()` at all if you don't want it to become visible. – Ben Voigt Nov 02 '22 at 16:32
  • 1
    @BenVoigt Not calling `Show()` will cause `CreateHandle()` to not be called and controls on the form won't be displayed properly, if at all. That's why when overriding `SetVisibleCore`, it's important to call `CreateHandle()` yourself as shown in the linked answer. – 41686d6564 stands w. Palestine Nov 02 '22 at 16:34
0

check this thread: How to take a screenshot of a WPF control?

RenderTargetBitmap.Render should works with minimalised window :).

Skundlony
  • 61
  • 8