例子有两部分,Part1是文字处理,Part2是图片处理
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
//Part1
var text = new FormattedText("测试",
System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(new FontFamily("黑体"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), 20, new SolidColorBrush(Colors.Red));
dc.PushTransform(new ScaleTransform(-1, 1) { CenterX = text.Width / 2 });//左右翻转 注意翻转中心为文字中心
dc.DrawText(text, new Point(0, 0));
dc.Pop();
dc.DrawText(text, new Point(text.Width, 0));//再写一个不翻转的
//Part2
////对开门 = 一个门 + 一个左右翻转的门
//BitmapImage bi = new BitmapImage(GetPackUri("简欧式门.jpg"));//图片是外部图片,图片的门把手在右边,并且门是对称的
//Rect destR = new Rect(bi.PixelWidth, 0, bi.PixelWidth, bi.PixelHeight);
//dc.PushTransform(new ScaleTransform(-1, 1) { CenterX = bi.PixelWidth + bi.PixelWidth / 2 });//翻转
//dc.DrawImage(bi, destR);//画入
//dc.DrawImage(bi, destL);
dc.Close();//不要忘记关
RenderTargetBitmap rtb = new RenderTargetBitmap((int)Math.Ceiling(text.Width * 2), (int)Math.Ceiling(text.Height), 96.0, 96.0, PixelFormats.Default);//转成图片资源
rtb.Render(dv);
imgTest.Source = rtb;//图片控件显示
//wpf引用外部文件
{
string strUrl = "pack://siteoforigin:,,,";
if (!url.StartsWith("/"))
{
strUrl += "/";
}
strUrl += url;
return new Uri(strUrl, UriKind.RelativeOrAbsolute);
}