Click here to Skip to main content
15,881,700 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using the following code to draw text on
C#
DrawingVisual
. I've heard that you can use `
C#
RenderTargetBitmap
` to convert this to Bitmap to have better performance.

C#
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`:

C#
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
C#
RenderTargetBitmap 
documentation example I have found this:

C#
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.
Posted
Comments
Kenneth Haugland 9-Jun-14 16:35pm    
IF you just want to draw a bitmap why use Drawingvisual then? You are making a control that has HitTesting etc, and it doesn't seem like you need it?

This:
http://stackoverflow.com/questions/5851168/save-canvas-to-bitmap
Vahid Yousefzadeh 9-Jun-14 17:02pm    
You mean I already can draw to Bitmap without drawing on DrawingVisual in the first place?

1 solution

It's not clear what do you miss in this documentation. Pay attention: the method System.Windows.Media.Imaging.RenderTargetBitmap.Render takes the instance of System.Windows.Media.Visual as a parameter. And your class ModelBeamSectionNamesInPlan is UIElement and, consequently, Visual:
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.render%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx[^].

Get an instance of ModelBeamSectionNamesInPlan, pass it as a parameter of RenderTargetBitmap.Render, and so on.

—SA
 
Share this answer
 
v2
Comments
Vahid Yousefzadeh 9-Jun-14 17:34pm    
Thanks Sergey, my problem is 1) how should I know the pixelWidth and pixelHeight parameters? 2. How am I going to add the final result to Canvas. Let's say I passed ModelBeamSectionNamesInPlan to RenderTargetBitmap.Render, when I try to add the bitmap to Canvas it gives me an error indicating that I cannot add RenderTargetBitmap to UIElement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900