开发者

WPF RenderTargetBitmap of an Items Panel but omit certain elements

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;
        }
    }


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;


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;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜