Jihoon Yoon
Jihoon Yoon

Reputation: 37

WPF shadow and capture screen

I made a shadow window with this code

<Grid Background="Red" Margin="0">
    <Grid.Effect>
        <DropShadowEffect BlurRadius="10" ShadowDepth="1" Direction="270" Color="Red"/>
    </Grid.Effect>
    <Grid Margin="1" Background="White">

    </Grid>
</Grid>

Result is successful, but when i try to capture windows screenshot with alt+print scr there was a blank margin like this.

first http://puu.sh/9rK9b/4dbd9a3b46.png

I want to capture screen only inside grid area except shadow area like this.

second http://puu.sh/9rKcj/098796a6c7.png

Upvotes: 0

Views: 280

Answers (1)

MrDosu
MrDosu

Reputation: 3435

You need to replicate the functionality of alt+print scr by hand to get the behaviour you need.

First of all you need to hook into the message loop and intercept the press along these lines:

ComponentDispatcher.ThreadFilterMessage += new ThreadMessageEventHandler(OnThreadMessage);

static void OnThreadMessage(ref MSG msg, ref bool handled)
{
    if (!handled)
    {
        if (msg.message == WmHotKey)
        {
            // intercept alt+print screen here, do custom action
        }
    }
}

Then you need to generate the image you want from the ui element and set it to the clipboard along these lines (uiElement will be your Grid):

var bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(uiElement);
encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (var stream = new MemoryStream)
{
    encoder.Save(stream);
    var img = Image.FromStream(stream);
    Clipboard.SetImage(img);
}

Upvotes: 1

Related Questions