Question

I use viewbox in the window of wpf, and the control is added programmatically. I hope the controls in the window can be flexible when the window size changed. However, the appearance of the controls seems be changed, the width and height not changed, and even though I use sizeChanged event to the control, it not fired when I saw the size of the control be changed.

Here is my sample xaml in main window:

    <Grid>
        <Viewbox Name="viewbox1">
            <Grid Height="667" HorizontalAlignment="Left" Margin="241,31,0,0" Name="grid1" VerticalAlignment="Top" Width="745" Background="White" /> 
            </Grid>
        </Viewbox>
    </Grid>

And the dynamic add control code is:

 VideoChannel vc = new VideoChannel();
 this.grid1.Children.Add(vc);

And the VideoChannel xaml like this:

<TabControl Height="613" HorizontalAlignment="Left" Margin="0,12,0,0" Name="tabControl1" VerticalAlignment="Top" Width="638" Background="{x:Null}" SelectionChanged="tabControl1_SelectionChanged">
            <TabItem Header="-P1-" Name="tabItem1" Height="30" FontFamily="Meiryo" FontWeight="Bold" BorderBrush="#FFA0A0A0" Background="#FFB7DEE8">
                <Grid Height="566">
                    <Button BorderBrush="{x:Null}" ClickMode="Press" Height="36" Margin="48,11,0,0" Name="btnVideo1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="36" Click="btnVideo1_Click">
                    </Button>
                </Grid>
            </TabItem>
        </TabControl>

Just a tabControl that have some button, so click one button, it will dynamic add something in the tabControl:

VideoType1 vt1 = new VideoType1();
this.grid1.Children.Add(vt1);

In the last, the final control that I want to know it's size is:

<Border Background="DimGray" MinHeight="144" MinWidth="176" Name="border1" Grid.Column="1" Margin="0,63,0,0" Height="385" VerticalAlignment="Top" HorizontalAlignment="Left" Width="527" />

I want to capture the picture of the border1, when I use original size, it was okay after all.However, if I change the window, the size of border1 doesn't changed, it still the original size even though I saw if bigger or smaller.

I try to use PointToWindow is okay:

Point video = border1.PointToScreen(new Point(0, 0));

It works fine,( The point of border in the top and left will change) but I not sure user will just change height or width of window( So I cannot use method of scale of the point).

I add event of sizeChanged in border1, and it does not fired( I don't know the reason).

The capture code is:

            Point video = border1.PointToScreen(new Point(0, 0));

            int w = (int)video.X + (int)border1.Width;

            int h = (int)video.Y + (int)border1.Height;

            int hsi = (int)border1.Height;

            if (canCap == true)
            {
                int wid = (int)border1.Margin.Right - (int)video.X;
                int hei = (int)border1.Margin.Bottom - (int)video.Y;
                System.Drawing.Bitmap b = new System.Drawing.Bitmap((int)border1.Width,(int)border1.Height);
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b))
                {
                    g.CopyFromScreen((int)video.X, (int)video.Y, (int)border1.Margin.Right, (int)border1.Margin.Bottom, b.Size, System.Drawing.CopyPixelOperation.SourceCopy);
                }

                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.DefaultExt = ".png";
                dlg.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png";

                Nullable<bool> result = dlg.ShowDialog();

                if (result == true)
                {
                    string filename = dlg.FileName;
                    b.Save(filename);
                }
            }
            else
                return;

And I try to get real size(when window changed) of the border1.

In the capture code, the size(border1.Width and border1.Height)is always the original size(527x385).

Sorry for poor explanation, the core of problem is:

Use viewbox to stretch control, but the control size isn't change when I change it's size while resize window( the area of the viewbox is whole window).

Any answer appreciated.

Was it helpful?

Solution

Use the stretch property of Viewbox to set the appropriate stretch mode.

OTHER TIPS

OK, I know what you mean.

I have another solution about it, it is a trick though.

I use sizeChanged event of my main Window, and store the size when the size changed. Then, in the control border1, I use scale to change the capture range.

Viewbox can stretch your control, but the real size of your control doesn't changed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top