Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Legend export to screenshot

Solved!
Go to solution

Hi,

 

I am trying to create a screenshot option for my Graph. Including the Legend. It looks quite good so far:

 

public void ScreenShot()
        {
            Rect bounds = LayoutInformation.GetLayoutSlot(SensorPanelGraph);
            var bitmap = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, 96, 96, PixelFormats.Pbgra32);

            Rect bounds2 = LayoutInformation.GetLayoutSlot(mainWindow.SPSMI8Legend);

            DrawingVisual dv = new DrawingVisual();

            using (DrawingContext dc = dv.RenderOpen())
            {

                VisualBrush vb = new VisualBrush(SensorPanelGraph);
         
                VisualBrush vb2 = new VisualBrush(mainWindow.SPSMI8Legend);

              
                            Point origo = new Point();
                Point LegendOrigo = new Point();
                Point LegendSecondPoint = new Point();

                LegendOrigo.X = origo.X + bounds.Width - bounds2.Width - 5;
                LegendOrigo.Y = origo.Y + 10;

                LegendSecondPoint.X = LegendOrigo.X + bounds2.Width ;
                LegendSecondPoint.Y = LegendOrigo.Y + bounds2.Height ;
               

                dc.DrawRectangle(vb, null, new Rect(origo, bounds.Size));


                vb2.Transform = new ScaleTransform(0.75, 0.75, LegendOrigo.X + bounds2.Width / 2, LegendOrigo.Y + bounds2.Height/2);
                dc.DrawRectangle(vb2, null, new Rect(LegendOrigo, LegendSecondPoint));

            }

            bitmap.Render(dv);

            string path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\Graph.png";
            using (FileStream outStream = new FileStream(path, FileMode.Create))
            {

                PngBitmapEncoder enc = new PngBitmapEncoder();

                enc.Frames.Add(BitmapFrame.Create(bitmap));

                enc.Save(outStream);

            }
        }

I know the code is not beautiful, I am still only experimentig around though. My main problem is the following.

 

The Legend doesn't have much space on the GUI, so it automaticly puts scrollbars horizontally and verticaly on the window if I fill it up too much. The VisualBrush object sadly takes a pixel by pixel copy of the UI object, and on the exported picture I only see part of the Legend which was visible at the time of the screenshot, and the scrollbars on each side.

 

Question: Is it possible to copy the whole of the Legend, as if it were expanded to its full size?

 

0 Kudos
Message 1 of 4
(4,413 Views)
Solution
Accepted by topic author jtamaska

All WPF drawing mechanisms use the layout size of the control. In order to display the full legend, you will either need to expand the legend in the UI, or you will need to use the technique from the Create WPF element offscreen and render to bitmap question (from Alex+ra) to do so off screen.


To expand the legend, it is possible to measure and arrange using the legend's desired size (though this will change it on screen as well):


    Rect desiredBounds = new Rect( new Point( ), legend.DesiredSize );
    legend.InvalidateMeasure( );
    legend.Measure( desiredBounds.Size );
    legend.Arrange( desiredBounds );
    legend.UpdateLayout( );


To update off screen, you will either need to remove the legend from its parent and then measure as above, or create a new legend with the same settings and render it.


Another alternative would be to declare two legends in your UI, keeping one of them in a hidden canvas (allowing it to render to full size without effecting the on screen display). This would avoid the need to manually perform measure and arrange calls to update the layout (though the legend visual would be silently updating even when you do not use the screenshot feature).

~ Paul H
0 Kudos
Message 2 of 4
(4,385 Views)

Hi jtamaska!

I think the previous reply can solve your problem, if not, than here is a link about a similar problem.
http://stackoverflow.com/questions/9429048/outlook-how-to-make-a-screenshot-of-the-entire-e-mail-bod...

 

You can find here some examples about the topic of screenshots (not exactly about graphs).

 

I hope I could help.

 

majora

 

0 Kudos
Message 3 of 4
(4,318 Views)

I used the laziest last solution, works fine!

 

Thanks!

0 Kudos
Message 4 of 4
(4,298 Views)