0

I'm using the following code to draw text on DrawingVisual. I've heard that you can use RenderTargetBitmap 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;
        }
    }
}

And finally I'm using the below code to add the texts to Canvas:

var beamtexts = new ModelBeamSectionNamesInPlan(this);
MyCanvas.Children.Add(beamtexts);

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.

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • Did you look at the online documentation? RenderTargetBitmap *is a* bitmap (a BitmapSource to be precise), and it has a `Render` method that takes a `Visual` argument. And of course you would use that bitmap as `Source` of an Image control or as `ImageSource` of an `ImageBrush`. – Clemens Jun 04 '14 at 17:25
  • @Clemens I did but honestly I couldn't figure out how to use it for the above situation :( – Vahid Jun 04 '14 at 17:28
  • 1
    There is an example of all that on the RenderTargetBitmap MSDN page. – Clemens Jun 04 '14 at 17:29
  • @Clemens I saw that but I have two problems here, the first one is I don't know to calculate the Width, Height of the final image? the second and most important one is how to implement this bitmap conversion in my code. – Vahid Jun 04 '14 at 17:36

0 Answers0