我使用以下代码在DrawingVisual上绘制文字。我听说您可以使用RenderTargetBitmap
将其转换为位图以获得更好的效果。
public class ModelBeamSectionNamesInPlan : UIElement
{
private readonly VisualCollection _visuals;
public ModelBeamSectionNamesInPlan(BaseWorkspace space)
{
var typeface = Settings.BeamTextTypeface;
var cultureinfo = Settings.CultureInfo;
var flowdirection = Settings.FlowDirection;
var beamtextsize = Settings.BeamTextSize;
var beamtextcolor = Settings.InPlanBeamTextColor;
const double scale = 0.5;
_visuals = new VisualCollection(this);
foreach (var beam in Building.ModelBeamsInTheElevation)
{
var drawingVisual = new DrawingVisual();
using (var dc = drawingVisual.RenderOpen())
{
var text = beam.Section.Id;
var ft = new FormattedText(text, cultureinfo, flowdirection,
typeface, beamtextsize, beamtextcolor,
null, TextFormattingMode.Display)
{
TextAlignment = TextAlignment.Center
};
// Draw Text
dc.DrawText(ft, space.FlipYAxis(x, y));
}
_visuals.Add(drawingVisual);
}
}
protected override Visual GetVisualChild(int index)
{
return _visuals[index];
}
protected override int VisualChildrenCount
{
get
{
return _visuals.Count;
}
}
}
最后,我使用以下代码将文字添加到Canvas
:
var beamtexts = new ModelBeamSectionNamesInPlan(this);
MyCanvas.Children.Add(beamtexts);
我不知道应该使用RenderTargetBitmap
转换为BMP以及如何将其添加到MyCanvas?
在RenderTargetBitmap
文档示例中,我发现了这一点:
RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
myImage.Source = bmp;
但我不知道如何在我的代码中实现这一点。