本文介绍了如何在WPF C#中将DrawingVisual转换为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码在
DrawingVisual
上绘制文字。我听说你可以使用`
. I've heard that you can use `
RenderTargetBitmap
`将它转换为Bitmap以获得更好的性能。
` to convert this to Bitmap to have better performance.
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的代码:
And finally I'm using the below code to add the texts to `Canvas`:
var beamtexts = new ModelBeamSectionNamesInPlan(this);
MyCanvas.Children.Add(beamtexts);
我没有丝毫的线索我应该使用`RenderTargetBitmap`转换为BMP以及如何使用将它添加到MyCanvas?
在
I don't have the slightest clue where I should use the `RenderTargetBitmap` to convert to BMP and how to add it to MyCanvas?
In the
RenderTargetBitmap
文档示例中我发现了这个:
documentation example I have found this:
RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 120, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
myImage.Source = bmp;
但我不知道如何在我的代码中实现这一点。
But I don't know how to implement this in my code.
推荐答案
这篇关于如何在WPF C#中将DrawingVisual转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!