首页 新闻 会员 周边

WPF DrawingVisual 保存成图片的问题, 文字被截了一小部分

0
悬赏园豆:60 [待解决问题]

如題文字最下一行,少了一部分。

如果RenderTargetBitmap的大小设置设置成固定大小是不会出现上面的问题。

但是这里要自动根据DrawingVisual 的大小创建图片的大小,就会出现上面的问题。

或者有没有其他方法将文字绘一个图片,WPF方法。

请问该如果解决。

private void DrawString()
{
DrawingVisual dv
= new DrawingVisual();
DrawingContext dc
= dv.RenderOpen();

dc.DrawText(
new FormattedText("微風迎客,軟語伴茶\r\n微風迎客,軟語伴茶\r\n微風迎客,軟語伴茶\r\n微風迎客,軟語伴茶",
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(textBox1.FontFamily, textBox1.FontStyle, textBox1.FontWeight, textBox1.FontStretch),
100d,
Brushes.SteelBlue),
new Point(0, 0));
dc.Close();

System.IO.FileStream fs
= new System.IO.FileStream(@"F:\text.png", System.IO.FileMode.Create);
RenderTargetBitmap bmp
= new RenderTargetBitmap((int)(dv.ContentBounds.Width), (int)(dv.ContentBounds.Height), 96d, 96d, PixelFormats.Pbgra32);
bmp.Render(dv);
BitmapEncoder encoder
= new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(fs);
fs.Close();
}

 

stam的主页 stam | 初学一级 | 园豆:100
提问于:2010-10-13 15:19
< >
分享
所有回答(2)
0

帮顶...

Terry717 | 园豆:205 (菜鸟二级) | 2010-10-14 11:32
0

试了一下~

RenderTargetBitmap bmp = new RenderTargetBitmap((int)(dv.ContentBounds.Width), (int)(dv.ContentBounds.Height), 90d,90d, PixelFormats.Default);

dpiX

类型:System.Double
位图的水平 DPI。

dpiY

类型:System.Double
位图的垂直 DPI。

调节这两个参数就可以获得正常的图片。

之前文字缺失具体是因为什么,这两者有什么关系暂时还不知道。

peony007 | 园豆:397 (菜鸟二级) | 2010-10-15 14:43
多謝,已解決了, 原來FormattedText上有大小,只有用這個大小去生成圖片就沒有問題。。。
支持(0) 反对(0) stam | 园豆:100 (初学一级) | 2010-10-22 11:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册