C # wpf control in viewbox but cannot get real size

I am using viewbox in a wpf window and the control is being added programmatically. I hope the controls in the window can be flexible when resizing the window. However, the appearance of the controls seems to be changed, the width and height do not change, and although I use the sizeChanged event for the control, it does not fire when I saw that the control will be resized.

Here is my xaml example in the 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 dynamic add control code:

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

And the xaml video channel looks 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 has a button, so click one button, it will dynamically add something to the tabControl:

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

In the latter case, the final control I want to know 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 border1 image when I use the original size, all the same everything is fine. However, if I change the window, the size of frame1 does not change, it will still be the original size, although I saw more or less.

I am trying to use PointToWindow in the order:

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

It works fine, (The border point in the upper and left parts will change), but I'm not sure if the user will just change the height or width of the window (so I cannot use the point scale method).

I am adding the sizeChanged event to border1 and it does not fire (I don't know the reason).

Capture Code:

  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'm trying to get the actual size (when changing the window) of the border.

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

Sorry for the poor explanation, the gist of the problem:

Use the viewbox to control the stretch, but the size of the control does not change when I change its size when the window is resized (the viewport area is a whole window).

Any answer is appreciated.

+4
source share
2 answers

Use the stretch property of the View window to set the appropriate stretch mode.

0
source

OK, I know what you mean.

I have another solution, but this is a trick.

I use the sizeChanged event of my main window and keep the size when resizing. Then, on control border1, I use the scale to change the capture range.

Viewbox may stretch your control, but the actual size of your control has not changed.

0
source

Source: https://habr.com/ru/post/1484809/


All Articles