This is a migrated thread and some comments may be shown as answers.

Issues when making a screenshot

1 Answer 27 Views
Map
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 12 Jan 2016, 03:49 PM

I have the following extension method in my application. It captures previews of my usercontrols and allows the user to navigate them via a menu.

 

/// <summary>
/// Renders a framework element as a bitmap image.
/// </summary>
/// <param name="frameworkElement">The framework element to be rendered.</param>
/// <param name="background">The background to be applied to the element, in case the element has trasnparent background.</param>
/// <param name="scale">The scaling of the image, default should be 1.</param>
/// <returns>The image as a scaled bitmap.</returns>
public static BitmapImage RenderToBitmap(this FrameworkElement frameworkElement, Brush background, double scale = 1)
{
    Check.NotNull(frameworkElement, "frameworkElement");
 
 
    var renderWidth = (int)(frameworkElement.RenderSize.Width * scale);
    var renderHeight = (int)(frameworkElement.RenderSize.Height * scale);
 
    if (renderWidth > 0 && renderHeight > 0)
    {
        var renderTarget = new RenderTargetBitmap(renderWidth, renderHeight, 96, 96, PixelFormats.Default);
 
        var sourceBrush = new VisualBrush(frameworkElement);
 
        var drawingVisual = new DrawingVisual();
 
        var rect = new Rect(new Point(0, 0), new Point(frameworkElement.RenderSize.Width, frameworkElement.RenderSize.Height));
 
        using (var drawingContext = drawingVisual.RenderOpen())
        {
            drawingContext.PushTransform(new ScaleTransform(scale, scale));
            drawingContext.DrawRectangle(background, null, rect);
            drawingContext.DrawRectangle(sourceBrush, null, rect);
        }
 
        renderTarget.Render(drawingVisual);
 
        var image = new BitmapImage();
 
        using (MemoryStream memoryStream = new MemoryStream())
        {
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
 
            encoder.Frames.Add(BitmapFrame.Create(renderTarget));
 
            encoder.Save(memoryStream);
 
            image.BeginInit();
            image.StreamSource = new MemoryStream(memoryStream.ToArray());
            image.EndInit();
        }
 
        return image;
    }
 
    return null;
}

 

Lately we've started a new development with the radmap control and we started to have some issues. The app hanged whenever we entered the view containing the map. I profiled the app and the results tell me that there's an issue with the radmap control.

 

I've started trying stuff and I realized that the problem starts when I try to generate the visual before the image creation. If I instead do renderTarget.Render(frameworkElement) it seems to work, but then I can't apply the background transformation to the visual. The problem starts when I use the map to create a VisualBrush. Could this be a bug? 

 

Thanks for your time.

 

 

1 Answer, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 15 Jan 2016, 10:14 AM
Hello Carlos,

Please send us some more code showing the way you use this method with RadMap and especially what you use in RadMap:
- - which map provider - Bing /OopenStreet ?
- - which map layer - ItemInformationLayer / VisualizationLayer ?
- - do you add shapes - map shape data , framework elements, how many ?
We will then try to reproduce such error on our side.


Regards,
Petar Mladenov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Map
Asked by
Carlos
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or