从 UIElement 获取图像被裁剪为显示大小

发布于 2024-11-29 00:52:54 字数 695 浏览 2 评论 0原文

我正在使用以下代码拍摄 wpf 控件的图像:

BitmapEncoder imgEncoder = new PngBitmapEncoder();
RenderTargetBitmap bmpSource = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 150, 150, PixelFormats.Pbgra32);
bmpSource.Render(element);
imgEncoder.Frames.Add(BitmapFrame.Create(bmpSource));
using (MemoryStream ms = new MemoryStream())
{
    imgEncoder.Save(ms);
    bytes = ms.ToArray();
    ms.Position = 0;
    Image i = Image.FromStream(ms);
    i.Save(@"C:\" + Guid.NewGuid().ToString() + "LARGE.png");
}

问题是 ActualHeight/Width 属性给出了渲染的高度和宽度,即显示的部分。我想保存整个控件的图像,即使某些控件在屏幕上不可见(即它放置在滚动查看器中)。

如何获取控件的完整大小/高度,以便 bmpSource.Render() 将整个控件呈现为图像?

I am taking an image of a wpf control using this code:

BitmapEncoder imgEncoder = new PngBitmapEncoder();
RenderTargetBitmap bmpSource = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 150, 150, PixelFormats.Pbgra32);
bmpSource.Render(element);
imgEncoder.Frames.Add(BitmapFrame.Create(bmpSource));
using (MemoryStream ms = new MemoryStream())
{
    imgEncoder.Save(ms);
    bytes = ms.ToArray();
    ms.Position = 0;
    Image i = Image.FromStream(ms);
    i.Save(@"C:\" + Guid.NewGuid().ToString() + "LARGE.png");
}

The trouble is ActualHeight/Width property gives the rendered height and width i.e. the displayed part. I want to save an image of the whole control even if some of the control is not visible on the screen i.e. it is placed in a scrollviewer.

How can I get the full size / height of a control so the bmpSource.Render() renders the whole control to an image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-12-06 00:52:54
     private static void SaveUsingEncoder(string fileName, FrameworkElement UIElement, BitmapEncoder encoder)
    {
        int height = (int)UIElement.ActualHeight;
        int width = (int)UIElement.ActualWidth;

        // These two line of code make sure that you get completed visual bitmap.
        // In case your Framework Element is inside the scroll viewer then some part which is not
        // visible gets clip.  
        UIElement.Measure(new System.Windows.Size(width, height));
        UIElement.Arrange(new Rect(new System.Windows.Point(), new Point(width, height)));

        RenderTargetBitmap bitmap = new RenderTargetBitmap(width,

                                                                height,
                                                                96, // These decides the dpi factors 
                                                                96,// The can be changed when we'll have preview options.
                                                                PixelFormats.Pbgra32);
        bitmap.Render(UIElement);

        SaveUsingBitmapTargetRenderer(fileName, bitmap, encoder);
    }


     private static void SaveUsingBitmapTargetRenderer(string fileName, RenderTargetBitmap renderTargetBitmap, BitmapEncoder bitmapEncoder)
    {
        BitmapFrame frame = BitmapFrame.Create(renderTargetBitmap);
        bitmapEncoder.Frames.Add(frame);
        // Save file .
        using (var stream = File.Create(fileName))
        {
            bitmapEncoder.Save(stream);
        }
    }

将此函数称为

     SaveUsingEncoder(fileName, frameworkElement, new PngBitmapEncoder());

希望这会有所帮助。

     private static void SaveUsingEncoder(string fileName, FrameworkElement UIElement, BitmapEncoder encoder)
    {
        int height = (int)UIElement.ActualHeight;
        int width = (int)UIElement.ActualWidth;

        // These two line of code make sure that you get completed visual bitmap.
        // In case your Framework Element is inside the scroll viewer then some part which is not
        // visible gets clip.  
        UIElement.Measure(new System.Windows.Size(width, height));
        UIElement.Arrange(new Rect(new System.Windows.Point(), new Point(width, height)));

        RenderTargetBitmap bitmap = new RenderTargetBitmap(width,

                                                                height,
                                                                96, // These decides the dpi factors 
                                                                96,// The can be changed when we'll have preview options.
                                                                PixelFormats.Pbgra32);
        bitmap.Render(UIElement);

        SaveUsingBitmapTargetRenderer(fileName, bitmap, encoder);
    }


     private static void SaveUsingBitmapTargetRenderer(string fileName, RenderTargetBitmap renderTargetBitmap, BitmapEncoder bitmapEncoder)
    {
        BitmapFrame frame = BitmapFrame.Create(renderTargetBitmap);
        bitmapEncoder.Frames.Add(frame);
        // Save file .
        using (var stream = File.Create(fileName))
        {
            bitmapEncoder.Save(stream);
        }
    }

Call this function as

     SaveUsingEncoder(fileName, frameworkElement, new PngBitmapEncoder());

Hope this will help.

淤浪 2024-12-06 00:52:54

您可以尝试暂时将控件从其上下文中取出(如果绑定可能会导致问题),将其转换为可见点或将其滚动到视图中进行渲染。

You could try temporarily taking the control out of its context (might cause problems if bound),transform it to a visivible point or scroll it into view for rendering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文