none
Resizing a visual object RRS feed

  • Question

  • 1) I want to render number (dynamic) of rectangles within a visual @C (Canvas/Panel/etc)

    Create a Canvas @C (dynamically/runtime)

    Generate Rectangles then put them into @C

    2) then I want to scale down the visual @C size, and expecting its contents scaled down accordingly

    My google research suggest to use Viewbox.

    trying this below (test and trial with few other whatever i can think of)

                double ImgScaling = 0.5d;
    
                Canvas img = new Canvas();
                ...
    
                var sz = new Size(xxWidth, xxHeight);
                img.Measure(sz);
                img.Arrange(new Rect(sz));
    
                var sz2 = new Size(xxWidth * ImgScaling, xxHeight * ImgScaling);
                var vb = new Viewbox();
                vb.Stretch = Stretch.Uniform;
                vb.Child = img;
                //vb.Height = img.ActualHeight * ImgScaling;
                //vb.Width = img.ActualWidth * ImgScaling;
                vb.Measure(sz2);
                vb.Arrange(new Rect(sz2));
    
                RenderTargetBitmap rtb = new RenderTargetBitmap((int)vb.Width, (int)vb.Height, 96, 96, PixelFormats.Pbgra32);
                rtb.Render(vb);
    
                ((Image)imgCtrl).Source = rtb;

    Main issue is Viewbox width and height always yield NaN

    I can run this codes fine without Viewbox, but missing the scaling down process.



    • Edited by Kelmen Thursday, March 15, 2018 10:02 AM
    Thursday, March 15, 2018 10:01 AM

Answers

  • this code worked:

            void DrawGG(Viewbox vb ...)
            {
                Canvas img = new Canvas();
    
                // draw stuffs on img
                img.Width = 123;
                img.Height = 456;
    
                vb.Stretch = Stretch.Uniform;
                vb.Child = img;
                vb.Width = img.Width * ImgScaling;
    

    need to assign size values to canvas width and height.

    then change viewbox size based on the canvas.width/height, as the actucalWidth/Height of both objects are zero.

    Don't need RenderTargetBitmap or Image.

    • Marked as answer by Kelmen Friday, March 16, 2018 9:39 AM
    Friday, March 16, 2018 9:38 AM

All replies

  • Hi,

    >>Main issue is Viewbox width and height always yield NaN

    You can not get Viewbox's ActualWidth and ActualHeight before the ViewBox rendered(after measure and arrange process finished ).

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 16, 2018 7:59 AM
    Moderator
  • i can't get the viewbox show up

    i changed the code for simple testing:

            <Viewbox Name="uiVB" Visibility="Hidden" />
    
            <Image Name="uiImgTest" Visibility="Hidden" />
    

                Canvas img = new Canvas();
    
                // drawing stuffs on Canvas
    
                var sz = new Size(xxWidth, xxHeight);
                img.Measure(sz);
                img.Arrange(new Rect(sz));
    
                uiVB.Stretch = Stretch.Uniform;
                uiVB.Child = img;
                uiVB.Visibility = Visibility.Visible;
    
                bool showImgTest = true;
                if (showImgTest)
                {
                    RenderTargetBitmap rtb = new RenderTargetBitmap((int)img.ActualWidth, (int)img.ActualHeight, 96, 96, PixelFormats.Pbgra32);
                    rtb.Render(img);
                    uiImgTest.Source = rtb;
                    uiImgTest.Visibility = Visibility.Visible;
                }

    I expect the viewbox and the image to show the canvas (with 2 simple rectangles draw on it).

    but only image show. the viewbox for unknown reason shows nothing, i chked its runtime property it is visible

    Friday, March 16, 2018 9:02 AM
  • this code worked:

            void DrawGG(Viewbox vb ...)
            {
                Canvas img = new Canvas();
    
                // draw stuffs on img
                img.Width = 123;
                img.Height = 456;
    
                vb.Stretch = Stretch.Uniform;
                vb.Child = img;
                vb.Width = img.Width * ImgScaling;
    

    need to assign size values to canvas width and height.

    then change viewbox size based on the canvas.width/height, as the actucalWidth/Height of both objects are zero.

    Don't need RenderTargetBitmap or Image.

    • Marked as answer by Kelmen Friday, March 16, 2018 9:39 AM
    Friday, March 16, 2018 9:38 AM