项目面板的 WPF RenderTargetBitmap 但省略某些元素
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需应用一个过滤器:
Just apply a filter:
最后我做了这样的事情
In the end I did something like this