WPF截图JPG自UIElement用C# [英] WPF Screenshot JPG from UIElement with C#

查看:310
本文介绍了WPF截图JPG自UIElement用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的WPF应用程序的一部分创建一个JPG。就像一个截图,只有个别的UIElement 秒。我开始在这里: http://www.grumpydev.com/2009/ 01/03 /了结WPF的截图/

I'm trying to create a JPG from part of my WPF Applications. Like a screenshot, only of individual UIElements. I started here: http://www.grumpydev.com/2009/01/03/taking-wpf-screenshots/

我是用自己的扩展方法,它必需可以让你获得一个byte []与 UIElement.GetJpgImage()。这可以被利用来FILESTREAM的JPG图像写入。如果我使整个窗口的JPG,它看起来就好了!然而,这是不理想的,因为它只是捕捉用户看到的内容。事情是因为ScrollViewer中,或者因为他们的父母是动画的小尺寸不会出现不可见的。

I am using his extension method, which essential allows you to get a byte[] with UIElement.GetJpgImage(). This can then be written using a filestream to a JPG image. If I make a JPG of the whole window, it looks just fine! However, this is not ideal because it just captures what the user sees. Things that are not visible because of the scrollviewer or because their parent was animated to a small size won't show up.

如果我采取了截图,比方说,我使用的布局网格:

If I take a "screenshot" of, say, a grid that I use for layout:

我得到这个垃圾用黑色背景。我不希望出现这种情况。此外,如果我已经崩溃了使用动画此网格的身高,我不会在所有的东西。这些实际上是模板复选框,他们应该有黑色文本在他们之上,和电网的背景应该是白色的。下面是别人写的返回被写入一个文件流的byte []数组的代码:

I get this crap with a black background. I don't want that. Furthermore, if I've collapsed this grid's height using animation, I won't get anything at all. Those are actually templated checkboxes, they should have black text above them, and the background of the grid should be white. Here's the code that someone else wrote to return the byte[] array that gets written to a filestream:

public static byte[] GetJpgImage(this UIElement source, double scale, int quality)
{
    double actualHeight = source.RenderSize.Height;
    double actualWidth = source.RenderSize.Width;

    double renderHeight = actualHeight * scale;
    double renderWidth = actualWidth * scale;

    RenderTargetBitmap renderTarget = new RenderTargetBitmap((int) renderWidth, (int) renderHeight, 96, 96, PixelFormats.Pbgra32);
    VisualBrush sourceBrush = new VisualBrush(source);

    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();

    using (drawingContext)
    {
        drawingContext.PushTransform(new ScaleTransform(scale, scale));
        drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
    }
    renderTarget.Render(drawingVisual);

    JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
    jpgEncoder.QualityLevel = quality;
    jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));

    Byte[] _imageArray;

    using (MemoryStream outputStream = new MemoryStream())
    {
        jpgEncoder.Save(outputStream);
        _imageArray = outputStream.ToArray();
    }

    return _imageArray;
}



在某处那里,我们得到一个黑色的背景。任何有识之士

Somewhere in there, we're getting a black background. Any insight?

编辑:如果我设置网格的背景属性为白色,截图出来的预期。然而,这是不可行的设置一切的背景下,我需要采取的截图。

If I set the grid's background property to white, the screenshot comes out as expected. However, it's not feasible to set everything's background that I need to take a screenshot of.

推荐答案

只是一个猜测,我想一个黑色背景将表示没有设置为在此过程中的任何字节数组的部分。数组中的初始零将显示为黑色。

Just a guess, I would think that a black background would represent portions of the byte array that are not set to anything in this process. The initial zeros in the array would appear as black.

要避免这种情况,我建议初始化为0xFF(byte.MaxValue)值的数组。

To avoid this, I suggest initializing the array with 0xFF (byte.MaxValue) values.

更新:

从看这个近,我想你应该画一个白色矩形到图像渲染用户界面元素之前。这应该反正工作。

From looking at this closer, I think you should draw a white rectangle onto the image before you render the UI element. That ought to work anyway.

只是这行代码之前

drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight))); 



把这样的事情

put something like this

drawingContext.DrawRectangle(Brushes.White, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight))); 

这篇关于WPF截图JPG自UIElement用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆