0

I am working on feedback form. When it's opened - user can check the flag to take screenshot of the screen. The problem is, that screenshot is taken with feedback form, and I need to capture screen without it.

I've found some code to take screenshot. It works. To solve this, I've tried to make this form invisible or closed during taking screenshot. Also, I've set it's opacity to 0 and height to 0. I think taking screenshot operation is too fast, so visually, the form cannot be hidden in such period of time.

    public void TakeScreenshot()
    {
        double screenLeft = SystemParameters.VirtualScreenLeft;
        double screenTop = SystemParameters.VirtualScreenTop;
        double screenWidth = SystemParameters.VirtualScreenWidth;
        double screenHeight = SystemParameters.VirtualScreenHeight;

        using (Bitmap bmp = new Bitmap((int)screenWidth, (int)screenHeight))
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen((int)screenLeft, (int)screenTop, 0, 0, bmp.Size);
                bmp.Save(@"D:\printscreen.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
    }


    public void SendMessage()
    {
        MagicOpacity = 0;
        MailAddress from = new MailAddress(Mail, CurrentUser.Name);
        MailAddress to = new MailAddress("a@mail.com");
        MailMessage message = new MailMessage(from, to);

        if (SendWithScreenshot)
        {
            TakeScreenshot();
            message.Attachments.Add(new Attachment("D:\\printscreen.jpg"));
        }

        MagicOpacity = 1;
    }

EDIT: The form expands by Opacity of Border, used as toggle, the form is content of Border(opacity == 0(1) -> height = 0(500))

    public void ExpandFeedBackForm()
    {
        MagicOpacity = MagicOpacity == 0 ? 1 : 0;
    }

<Canvas>
    <Button Name="FeedBackButton" Command="{Binding ExpandFeedBackFormCommand}" FontWeight="Bold" FontSize="10" Height="18.5" Margin="-3,-8,0,0"/>
    <Border BorderBrush="Gray" BorderThickness="2" Opacity="{Binding MagicOpacity}" Name="border" Margin="-350,0,-178,-9" Background="White"
                    Canvas.Bottom="{Binding ElementName=FeedBackButton, Path=ActualHeight}">
        <Border.Style>
            <Style TargetType="Border">
                <Style.Triggers>
                    <Trigger Property="Opacity"  Value="1">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="500" Duration="0:0:0.4"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger> 
                </Style.Triggers>
            </Style>
        </Border.Style>
        <v:FeedBackView/>
    </Border>
</Canvas>

MagicOpacity here is used for triggering animation using command(that's because Height property cannot be binded). But that is working, so it doesn't matter.

Even if I do not set the MagicOpacity to 1 after taking screenshot, the form - is in the picture.

So the question is that is it possible to take screenshot of window without top-most window?

  • 2
    Have you even tried to search? This one is among the first results I've found: https://stackoverflow.com/a/911225/5922736 – Sergey Shevchenko Feb 12 '19 at 15:22
  • id have gone the obvious route and hidden the form for the nanosecond of time i was taking the screen print – BugFinder Feb 12 '19 at 15:34
  • 1
    Or you could do what everyone else is doing for `WPF` and render the visual to a bitmap! [SO post](https://stackoverflow.com/a/3617902/2029607). No need to call Win32 API. – XAMlMAX Feb 12 '19 at 15:36
  • @XAMlMAX, you can't render the desktop to a bitmap using WPF only, because the desktop is not a `Visual`. – dymanoid Feb 12 '19 at 16:20
  • Please provide the code of your `MagicOpacity` property and the corresponding code/XAML for hiding the form. – dymanoid Feb 12 '19 at 16:23
  • And also please specify exactly: do you want a screenshot of the whole desktop (including e.g. multiple monitors) or a screenshot of a particular window? – dymanoid Feb 12 '19 at 16:26
  • @dymanoid OP asks about the window. Taking a screenshot of a desktop to then find the bit where the window is located is a bit of an overkill, so the OP can pass the desired window in instead. Sorry if my comment wasn't clear. – XAMlMAX Feb 13 '19 at 08:58
  • @KevinCook, the first sentence: "user can check the flag to take screenshot **of the screen**". – dymanoid Feb 13 '19 at 16:15
  • Take a screenshot before you even open your form and then if needed, use it. Otherwise, let them use snipping tool and paste an image into a capture area on your form to save (which would be better, because the users could have multiple monitors and then you'd have to figure out which screen to grab from etc.) – Kevin Cook Feb 13 '19 at 20:36
  • Does screen mean desktop?) So, I don't need to capture desktop. @dymanoid – Харламов Даниил Feb 14 '19 at 06:37
  • I did some actions, using messaging between viewmodel and code behind, so the max result is that form hides ALMOST fully. So I think, that I need to use threading for rendering disappearing form, but I don't know how to do it right, any ideas? – Харламов Даниил Feb 14 '19 at 06:41
  • Solved! I've made TakingScreenshot sleeping for a little, and method, which calls TakingScreenshot - async)) Thanks, all)) – Харламов Даниил Feb 14 '19 at 07:54

0 Answers0