Problem of saving images in WPF (RenderTargetBitmap)zz
But sometimes you will find the output image was shifted or left blank. This is because that RenderTargetBitmap render the visual object based on cordinate of its parent object. Margin of itself, Padding or BorderThickness of its parent will all affect the rendered image. Although I think this is a bug of WPF, it seems the feature is by design as reference to RenderTargetBitmap layout offset influence.
There are three ways to fix the problem,
| Solution | Description |
|---|---|
| Add a Border | Simple, but the visual logical tree is changed. |
| Use a VisualBrush | Maintain the original visaul logical tree, but need to do more process. |
| Temporary change the reative postion by Measure() and Arrange() | Need to change back after rendering, and the two function is automatically called while repainting by WPF, so the real process is hard to tell. |
The first solution is simplist. RenderTargetBitmap is only shifted by the distance between visaul and its parent object, so you just need to add a fake parent and move the margin to the parent object.
If the original visual logical tree is like
<Grid>
<Canvas Margin="20" />
</Grid>
changed to
<Grid>
<Border Margin="20">
<Canvas />
</Border>
</Grid>
and just call the method of SaveTo as Save and read images in WPF.
private void SaveTo(Visual v, string f)
{
/// get bound of the visual
Rect b = VisualTreeHelper.GetDescendantBounds(v);
/// new a RenderTargetBitmap with actual size of c
RenderTargetBitmap r = new RenderTargetBitmap(
(int)b.Width, (int)b.Height,
96, 96, PixelFormats.Pbgra32);
/// render visual
r.Render(v);
/// new a JpegBitmapEncoder and add r into it
JpegBitmapEncoder e = new JpegBitmapEncoder();
e.Frames.Add(BitmapFrame.Create(r));
/// new a FileStream to write the image file
FileStream s = new FileStream(f,
FileMode.OpenOrCreate, FileAccess.Write);
e.Save(s);
s.Close();
}
Second solution is draw the visaul to a DrawingVisual object, and pass the object to the SaveTo function.
private DrawingVisual ModifyToDrawingVisual(Visual v)
{
/// new a drawing visual and get its context
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
/// generate a visual brush by input, and paint
VisualBrush vb = new VisualBrush(v);
dc.DrawRectangle(vb, null, b);
dc.Close();
return dv;
}
PS. The context will act after calling Close(). You can use the using statement to block the region. And the SaveTo method should be modified as
/// render visual
r.Render(ModifyToDrawingVisual(v));
The third solution is to temporarily change the reative postion by Measure and Arrange before Render,
private void ModifyPosition(FrameworkElement fe)
{
/// get the size of the visual with margin
Size fs = new Size(
fe.ActualWidth +
fe.Margin.Left + fe.Margin.Right,
fe.ActualHeight +
fe.Margin.Top + fe.Margin.Bottom);
/// measure the visual with new size
fe.Measure(fs);
/// arrange the visual to align parent with (0,0)
fe.Arrange(new Rect(
-fe.Margin.Left, -fe.Margin.Top,
fs.Width, fs.Height));
}
PS. The solution is only suitable for UIElement, and need to change the position back after rendering.
private void ModifyPositionBack(FrameworkElement fe)
{
/// remeasure a size smaller than need, wpf will
/// rearrange it to the original position
fe.Measure(new Size());
}
Because the size to be measured is smaller then the real size, WPF will rearrange the layout and align the position back. And the render part is modified as
/// render visual
ModifyPosition(v as FrameworkElement);
r.Render(v);
ModifyPositionBack(v as FrameworkElement);
About Measure() and Arrange(), detail is reference toUIElement.Measure Method
--
Reference
RenderTargetBitmap layout offset influence
RenderTargetBitmap tips
RenderTargetBitmap and XamChart - Broken, Redux
Problem of saving images in WPF (RenderTargetBitmap)zz的更多相关文章
- wpf RenderTargetBitmap保存控件为图片时图片尺寸不对的问题
使用RenderTargetBitmap渲染图片时,必须在需要渲染的控件外层包含一个border控件,不然渲染的图片返回就会出现问题. 如下: <Grid> <Grid.RowDef ...
- WPF:如何高速更新Model中的属性
原文:[WPF/MVVM] How to deal with fast changing properties In this article, I will describe a problem w ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A、Saving Tang Monk II 【状态搜索】
任意门:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 时间限制:1000ms 单点时限:1000ms 内存限制:25 ...
- 导出程序界面(UI)到图片
无意间看到这个需求,查阅了相关文章,有两篇不错的博客给出了解决方案,地址如下: 1.在WPF程序中将控件所呈现的内容保存成图像 2.随心所欲导出你的 UI 界面到 PDF 文件 主要使用的接口: Si ...
- 随心所欲导出你的 UI 界面到 PDF 文件
使用 C1PDF 控件可以导出文件到 PDF 文件,结合 .NET 平台特性你可以在任何客户端生成自定义报表.你可以打印任何 UI 界面,例如 DataGrid 导出到 PDF. 在本篇文章中我们将阐 ...
- TensorFlow 生成 .ckpt 和 .pb
原文:https://www.cnblogs.com/nowornever-L/p/6991295.html 1. TensorFlow 生成的 .ckpt 和 .pb 都有什么用? The . ...
- Emacs及扩展配置
Emacs及扩展配置 Table of Contents 1. 动机之反思 2. 它山之石 3. 扩展的管理 4. 我额外安装的通用扩展(在purcell基础上) 5. LaTex相关的问题和配置 6 ...
- 客户端类中中记录异常的方法: 使用Log4net
1.首先引用Log4Net 的命名空间 using log4net; 2.在使用的类中生命静态变量 log public class FileService { static re ...
- COM Error Code(HRESULT)部分摘录
Return value/code Description 0x00030200 STG_S_CONVERTED The underlying file was converted to compou ...
随机推荐
- 利用Levenshtein Distance (编辑距离)实现文档相似度计算
1.首先将word文档解压缩为zip /** * 修改后缀名 */ public static String reName(String path){ File file=new File(path) ...
- telnet 传输文件
用python创建个临时服务器,发送文件供telnet访问 import socket import base64 port = 10005 filename = 'libcrypto.so.1.0. ...
- 查找Linux中内存和CPU使用率最高的进程
下面的命令会查看到按照RAM和CPU降序方式的前最高几名进程的列表: [root@iZ25pvjcsyhZ ~]# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem ...
- 利用box-shadow实现伪边框透明到图片
前阵子突然看到了一个效果,一张图片,有一个边框,但是边框可以透明到图片.直接上图 贴代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- svn sync主从同步学习
svn备份的方式有三种: 1svnadmin dump 2)svnadmin hotcopy 3)svnsync. 优缺点分析============== 第一种svnadmin dump是官方推荐 ...
- iOS的一像素线
文/stark_yang(简书作者)原文链接:http://www.jianshu.com/p/b83dca88ef73著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 时常总结以前学过 ...
- [每日一记] Python报错 综述
提纲 -- Syntax errors -- Static semantic errors -- Full semantic errors -- 使用一门语言,不论是自然语言还是编程语言,我们需要注意 ...
- erlang 健壮性
erlang 提供了简单易用的并发编程模型,基本不需要再考虑多线程并发问题.但实际应用中并不是那么的完美,很多地方需要注意,就算标准库也有不少问题.很多在多线程编程中很多很容易解决的事情,在erlan ...
- Content-disposition
今天查看Struts2的文件上传部分 发现有个例子开头打印的信息中有Content-Disposition,一时好奇,所以了解了一下.顺便学习一下文件上传所需要的注意事项. Content-dispo ...
- 小众Tox——大众的“去中心化”聊天软件
★Tox是什么 一个反窥探的开源项目:一种基于DHT(BitTorrent)技术的即时通讯协议:一个为安全而生的加密通讯系统 .美国棱镜计划曝光后,一个名为 irungentoo 的牛人于17天后的2 ...