question

UCC-3182 avatar image
0 Votes"
UCC-3182 asked UCC-3182 answered

VisualBrush of an FrameworkElement after expanding/collapsing

Hello All:

I have a user control with a grid. Some of the grid columns might be collapsed as needed.

I'm working on saving an image of the control with the VisualBrush. My code saves the display image fine if all the panels are visible, otherwise, the image is compressed like the code squeezes all the panels into the control panel size when some grids are collapsed. In this case, the collapsed part is shown as a white background.

Thank you in advance.

I found a forum where they discussed a similar problem but the solution blog is not live any longer. Here is the forum page: https://social.msdn.microsoft.com/Forums/vstudio/en-US/997fb616-4cd4-49f0-8b85-f05cde328104/visualbrush-of-an-expander-after-expandingcollapsing?forum=wpf

Here is my code.
public static BitmapSource DrawToBitmap(this FrameworkElement element)
{
double width = element.ActualWidth;
double height = element.ActualHeight;
int pixelWidth = (int)Math.Round(width);
int pixelHeight = (int)Math.Round(height);
var bmp = new RenderTargetBitmap(pixelWidth, pixelHeight, 96, 96, PixelFormats.Default);
var drawingVisual = new DrawingVisual(); using (DrawingContext dc = drawingVisual.RenderOpen())
{
var brush = new VisualBrush(element);
dc.DrawRectangle(brush, null, new Rect(0, 0, width, height));
}
bmp.Render(drawingVisual);
return bmp;
}

dotnet-csharpdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@UCC-3182
Do you want to save the image of the UserControl with collapsed controls the same as all the UserControl with the controls visible? Do you use Visibility.Collapsed to make the Grid Collapsed? Could you describe to me what type of image you want to save for the pages with Collapsed Control?

0 Votes 0 ·

Dear DaisyTian:

I have prepared an image of the problem here. You can see the image here: https://hosting.photobucket.com/images/l225/eccoskun/MSHelp_Image.png. In the image, you can see my code and end results for both cases.

Answers to your questions;
I would like to save the image as I see it on the display without any distortion. I use both "Image.visibility.Collapsed" and "Grid.width='0'" to hide my second image.

Thank you for looking into this.

0 Votes 0 ·
UCC-3182 avatar image
0 Votes"
UCC-3182 answered

Kent Boogaart (https://kent-boogaart.com/) provided a workaround.

Here is the final code with his input.
Image of a FrameworkElement element can be saved by:

         double width = element.ActualWidth;
         double height = element.ActualHeight;
         int pixelWidth = (int)Math.Round(width);
         int pixelHeight = (int)Math.Round(height);
         var bmp = new RenderTargetBitmap(pixelWidth, pixelHeight, 96, 96, PixelFormats.Default);
         var drawingVisual = new DrawingVisual();
         using (DrawingContext dc = drawingVisual.RenderOpen())
         {
             var brush = new VisualBrush(element);
             brush.ViewboxUnits = BrushMappingMode.Absolute;
             brush.Viewbox = new Rect(element.TranslatePoint(new Point(0, 0), element), new Size(width, height));
             dc.DrawRectangle(brush, null, new Rect(0, 0, width, height));
         }
         bmp.Render(drawingVisual);
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

UCC-3182 avatar image
0 Votes"
UCC-3182 answered

This code also works:

         double width = element.ActualWidth;
         double height = element.ActualHeight;
         int pixelWidth = (int)Math.Round(width);
         int pixelHeight = (int)Math.Round(height);
         var bmp = new RenderTargetBitmap(pixelWidth, pixelHeight, 96, 96, PixelFormats.Default);
         var drawingVisual = new DrawingVisual();
         using (DrawingContext dc = drawingVisual.RenderOpen())
         {
             var brush = new VisualBrush(element);
             brush.AlignmentX = AlignmentX.Left;
             brush.Stretch = Stretch.None;
             dc.DrawRectangle(brush, null, new Rect(0, 0, width, height));
         }
         bmp.Render(drawingVisual);
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.