项目面板的 WPF RenderTargetBitmap 但省略某些元素

发布于 2024-10-21 08:59:59 字数 1447 浏览 0 评论 0原文

我正在使用 RenderTargetBitmap.Render(panel) 来拍摄项目面板的快照。

但是我想省略某些元素(例如 OmitWhileRenderingToBitmap 属性设置为 false 的自定义项目)。

有没有一些简单的方法可以做到这一点,即从项目面板中删除项目,拍摄快照,然后将它们添加回来?

编辑:本质上,这是我的自定义列表框类中的关键位。拆下过滤器位,一切都按预期工作。

    protected void UpdatePanelBitmapSource()
    {
        if (this._itemsPanel != null) //_itemsPanel here is the canvas within the ListBox. Not rendering the ListBox for a reason.
        {
            Matrix m = PresentationSource.FromVisual(this._itemsPanel).CompositionTarget.TransformToDevice;

            var existingFilter = this.Items.Filter;

            // Add filter
            this.Items.Filter = item =>
            {
                var mgvListBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as MGVListBoxItem;

                if (mgvListBoxItem != null)
                    return !mgvListBoxItem.IsEditable;
                else
                    return false;
            };

            var panelBitmap = new RenderTargetBitmap(
                (int)Math.Floor(this._itemsPanel.ActualWidth),
                (int)Math.Floor(this._itemsPanel.ActualHeight),
                m.M11 * 96.0,
                m.M22 * 96.0,
                PixelFormats.Default);
            panelBitmap.Render(this._itemsPanel);

            // Remove filter
            this.Items.Filter = existingFilter;

            this.PanelBitmapSource = panelBitmap;
        }
    }

I am using RenderTargetBitmap.Render(panel) to take a snap of an items panel.

However I want to omit certain elements (custom items with say OmitWhileRenderingToBitmap property set to false).

Is there some easy way of doing this short of removing the items from the items panel, taking the snap and then adding them back?

Edit: Essentially this is the key bit in my custom listbox class. Remove the filter bits and everything works as expected.

    protected void UpdatePanelBitmapSource()
    {
        if (this._itemsPanel != null) //_itemsPanel here is the canvas within the ListBox. Not rendering the ListBox for a reason.
        {
            Matrix m = PresentationSource.FromVisual(this._itemsPanel).CompositionTarget.TransformToDevice;

            var existingFilter = this.Items.Filter;

            // Add filter
            this.Items.Filter = item =>
            {
                var mgvListBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as MGVListBoxItem;

                if (mgvListBoxItem != null)
                    return !mgvListBoxItem.IsEditable;
                else
                    return false;
            };

            var panelBitmap = new RenderTargetBitmap(
                (int)Math.Floor(this._itemsPanel.ActualWidth),
                (int)Math.Floor(this._itemsPanel.ActualHeight),
                m.M11 * 96.0,
                m.M22 * 96.0,
                PixelFormats.Default);
            panelBitmap.Render(this._itemsPanel);

            // Remove filter
            this.Items.Filter = existingFilter;

            this.PanelBitmapSource = panelBitmap;
        }
    }

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

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

发布评论

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

评论(2

冬天旳寂寞 2024-10-28 08:59:59

只需应用一个过滤器:

// Add filter
yourItemsControl.Items.Filter = o =>
    {
        YourType item = (YourType)o;
        return !item.OmitWhileRenderingToBitmap;
    };


// Render to bitmap
// ...

// Remove filter
yourItemsControl.Items.Filter = null;

Just apply a filter:

// Add filter
yourItemsControl.Items.Filter = o =>
    {
        YourType item = (YourType)o;
        return !item.OmitWhileRenderingToBitmap;
    };


// Render to bitmap
// ...

// Remove filter
yourItemsControl.Items.Filter = null;
时光沙漏 2024-10-28 08:59:59

最后我做了这样的事情

        protected void OnUpdatePanelBitmapSource()
    {
        if (this._itemsPanel != null)
        {
            Matrix m = PresentationSource.FromVisual(this._itemsPanel).CompositionTarget.TransformToDevice;

            var hiddenItems = new List<MGVListBoxItem>();

            foreach(var item in this.Items)
            {
                var mgvListBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as MGVListBoxItem;
                if (mgvListBoxItem != null && mgvListBoxItem.IsEditable && mgvListBoxItem.IsVisible)
                {
                    mgvListBoxItem.Visibility = Visibility.Hidden;
                    hiddenItems.Add(mgvListBoxItem);
                }
            }

            var panelBitmap = new RenderTargetBitmap(
                (int)Math.Floor(this._itemsPanel.ActualWidth),
                (int)Math.Floor(this._itemsPanel.ActualHeight),
                m.M11 * 96.0,
                m.M22 * 96.0,
                PixelFormats.Default);

            panelBitmap.Render(this._itemsPanel);

            foreach (var mgvListBoxItem in hiddenItems)
            {
                mgvListBoxItem.Visibility = Visibility.Visible;
            }

            this.PanelBitmapSource = panelBitmap;
        }
    }

In the end I did something like this

        protected void OnUpdatePanelBitmapSource()
    {
        if (this._itemsPanel != null)
        {
            Matrix m = PresentationSource.FromVisual(this._itemsPanel).CompositionTarget.TransformToDevice;

            var hiddenItems = new List<MGVListBoxItem>();

            foreach(var item in this.Items)
            {
                var mgvListBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as MGVListBoxItem;
                if (mgvListBoxItem != null && mgvListBoxItem.IsEditable && mgvListBoxItem.IsVisible)
                {
                    mgvListBoxItem.Visibility = Visibility.Hidden;
                    hiddenItems.Add(mgvListBoxItem);
                }
            }

            var panelBitmap = new RenderTargetBitmap(
                (int)Math.Floor(this._itemsPanel.ActualWidth),
                (int)Math.Floor(this._itemsPanel.ActualHeight),
                m.M11 * 96.0,
                m.M22 * 96.0,
                PixelFormats.Default);

            panelBitmap.Render(this._itemsPanel);

            foreach (var mgvListBoxItem in hiddenItems)
            {
                mgvListBoxItem.Visibility = Visibility.Visible;
            }

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