例子有兩部分,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);
}