1

Using the below code I'm drawing on DrawingVisual then rendering it to an Image using RenderTargetBitmap. The final Image is later added to a Canvas and displayed on the screen.

My problem is with the pixelWidth and pixelHeight arguments the RenderTargetBitmap method wants. What valued should I give to it? I have noticed that if I give it lower numbers parts of the image is not rendered. On what basis should I choose these? I have given it 1000 in the code below.

public class ModelBeamSectionNamesInPlan : Image
{
    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;

        beamtextcolor.Freeze();
        const double scale = 0.6;

        var drawingVisual = new DrawingVisual();
        using (var dc = drawingVisual.RenderOpen())
        {
            foreach (var beam in Building.ModelBeamsInTheElevation)
            {
                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));
            }
        }

        var bmp = new RenderTargetBitmap(1000, 1000, 120, 96, PixelFormats.Pbgra32);
        bmp.Render(drawingVisual);
        Source = bmp;
    }
}
Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 1
    How about the [ContentBounds](http://msdn.microsoft.com/en-us/library/system.windows.media.containervisual.contentbounds.aspx) property of the DrawingVisual? – Clemens Aug 01 '14 at 15:13
  • @Clemens Thanks, `drawingVisual.ContentBounds.Width` gives me `-Infinity`. – Vahid Aug 02 '14 at 11:46
  • 1
    Did you perhaps try [DescendantBounds](http://msdn.microsoft.com/en-us/library/system.windows.media.containervisual.descendantbounds.aspx) instead? – Clemens Aug 03 '14 at 17:14

1 Answers1

3

You can query the DrawingVisual's ContentBounds property, which

gets the bounding box for the contents of the ContainerVisual

or the DescendantBounds property which

gets the union of all the content bounding boxes for all of the descendants of the ContainerVisual, but not including the contents of the ContainerVisual.

Something like this should work:

var bounds = drawingVisual.DescendantBounds;
var bmp = new RenderTargetBitmap(
    (int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height),
    96, 96, PixelFormats.Pbgra32);
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thanks Clemens, this was very helpful. Is this the only way to get the Bitmap? Is there any other way in WPF better or more efficient than this one? – Vahid Aug 04 '14 at 12:28
  • You mean another way to create a bitmap from a Visual? That's exactly the purpose of RenderTargetBitmap. If you just wanted to draw a copy of a Visual, you might as well use a VisualBrush, e.g. as Fill of a Rectangle. – Clemens Aug 04 '14 at 14:37
  • Yes I meant is there another way? Like you know there are a bunch of ways to do things in WPF. Regarding the VisualBrush things, something just occurred to me, can we store each English letter as different VisualBrush and create a more efficient text system like SHX fonts in autocad? I wanted to ask this as a separate question, but I'm afraid it is a bit general. – Vahid Aug 04 '14 at 20:28
  • More efficient? I guess not, as text rendering is already quite efficient in WPF. This article may be interesting: [Typography in WPF](http://msdn.microsoft.com/en-us/library/ms742190.aspx). – Clemens Aug 04 '14 at 20:32
  • Thanks Clemens. Great article. – Vahid Aug 04 '14 at 22:00