【问题标题】:When changing the program size, the image box should automatically expand更改程序大小时,图像框应自动展开
【发布时间】:2017-03-13 10:34:11
【问题描述】:

我有一个程序大小,如果我在角落增加它,就是自动增加 ImageBox。不幸的是,这还不起作用,因为发生错误。如果我改变表单大小,有没有人解决这个问题,这也会自动增加 ImageBox。如果我更改表单的大小,我希望 ImageBox 的大小也更大。

private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
            {
                imgPreview.Source = zoom(imgPreview, new System.Drawing.Size(Convert.ToInt32(this.Width), Convert.ToInt32(this.Height))); // Mistake is here
            }

        System.Drawing.Image zoom(System.Drawing.Image img, System.Drawing.Size size)
        {
            Bitmap bmp = new Bitmap(img, img.Width + (img.Width * size.Width / 100), img.Height + (img.Height * size.Height / 100));
            Graphics g = Graphics.FromImage(bmp);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            return bmp;
        }

<UserControl x:Class="Vorschau.UCOxyplotPreview"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Vorschau"
             mc:Ignorable="d" Height="379.573" Width="539.634" SizeChanged="UserControl_SizeChanged">
    <Image x:Name="imgPreview" HorizontalAlignment="Left" Height="350" Margin="109,20,0,0" VerticalAlignment="Top" Width="350"/>

</UserControl>

【问题讨论】:

  • 你也可以显示你的 xaml 吗?
  • 谢谢。完成。
  • 以下属性是否有原因:Horizo​​ntalAlignment、Height、Margin、VerticalAlignment 和 Width?
  • 没有。这是自动生成的。
  • 好的,看看我的回答。

标签: c# wpf image


【解决方案1】:

尝试删除属性HorizontalAlignmentHeightMarginVerticalAlignmentWidthImage 应该会在完成后自动调整大小。

//编辑 只是向您展示一个简约的示例。使用此代码时,您的Image 大小将改变您的Window 大小正在改变。这就是为什么我认为问题出在您的Window xaml 中,您将UserControlImage 一起使用。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        xmlns:viewModel="clr-namespace:WpfApplication2">
    <Image Source="IMG_20160831_105257778.jpg" />
</Window>

【讨论】:

  • 谢谢。不幸的是,我模糊地表达了自己。如果我更改表单的大小,我希望 ImageBox 的大小也更大。
  • 所以一切正常,您的图像正在调整大小?
  • 总是只在中间,大小一样。
  • 您的 UserControl 是否位于窗口内?如果是:请告诉我我们的 Window xaml 以及重现您的问题。
  • @GabelUndMesser 那是因为 UserControl 大小本身永远不会改变。确保您也从主视图的 UserControl 中删除这些属性。
猜你喜欢
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多