WPF C# 图像控件在 540 像素以下被剪切/消失

Posted

技术标签:

【中文标题】WPF C# 图像控件在 540 像素以下被剪切/消失【英文标题】:WPF C# Image control cutted/disapeared below 540 pixels 【发布时间】:2021-04-01 12:22:56 【问题描述】:

由于某种原因,图像控件不显示低于 540 像素

如果图像控件上边距低于窗口的 540 像素,它就会消失 如果低于则低于 540 像素

С找不到任何解决方案

2 images of clock cut

C# 代码

        TimeZoneInfo newyorkZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        double time44 = Convert.ToDouble(TimeZoneInfo.ConvertTime(DateTime.Now, newyorkZone).ToString("HH"));

        if (time44 >= 12) time44 = time44 - 12;
     
        double time444 = Convert.ToDouble(TimeZoneInfo.ConvertTime(DateTime.Now, newyorkZone).ToString("mm"));
        
        double time44rad = (time44 * 360 / 12) * Math.PI / 180 - Math.PI / 2;
        double time444rad = (time444 * 360 / 60) * Math.PI / 180 - Math.PI / 2;
     

        double xtime444 = 125 + 125 * Math.Cos(time444rad);
        double ytime444 = 125 + 125 * Math.Sin(time444rad);
       
        xtime44 = Math.Round(xtime44, 0);
        int xitime44 = Convert.ToInt32(xtime44);
        
        ytime44 = Math.Round(ytime44, 0);
        int yitime44 = Convert.ToInt32(ytime44);
       
        xtime444 = Math.Round(xtime444, 0);
        int xitime444 = Convert.ToInt32(xtime444);
       
        ytime444 = Math.Round(ytime444, 0);
        int yitime444 = Convert.ToInt32(ytime444);

        int xi = 125;
        int yi = 125;

        GeometryGroup clock4 = new GeometryGroup();
  
        clock4.Children.Add(new EllipseGeometry(new Point(xi, yi), 125, 125));
       
        clock4.Children.Add(new LineGeometry(new Point(xi, yi), new Point(xitime44, yitime44)));
        clock4.Children.Add(new LineGeometry(new Point(xi, yi), new Point(xitime444, yitime444)));
        Pen pen = new Pen(Brushes.Red, 3);
  
        GeometryDrawing aGeometryDrawing4 = new GeometryDrawing();
        aGeometryDrawing4.Geometry = clock4;
        aGeometryDrawing4.Pen = pen;
     
        DrawingGroup aDrawingGroup4 = new DrawingGroup();
      
        aDrawingGroup4.Children.Add(aGeometryDrawing4);
      
        DrawingImage geometryImage4 = new DrawingImage(aDrawingGroup4);

        geometryImage4.Freeze();
  
        cap4time.Source = geometryImage4;

Xaml

        <Image x:Name="cap1time" Margin="100,50" Width="250" Height="250" HorizontalAlignment="Left" 
    VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />
        <Image x:Name="cap3time" Margin="100,500" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />
        <Image x:Name="cap4time" Margin="600,500" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />
        <Image x:Name="cap2time" Margin="600,50" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.ColumnSpan="1" Stretch="UniformToFill" />

【问题讨论】:

使用其边距定位 WPF 控件绝不是一个好主意 - 将每个图像放置在 Grid 控件的不同行/列中。 这意味着我需要添加网格并将其拆分,然后在单独的行/列中添加图像控件而不使用 Marging? 是的,例如wpf-tutorial.com/panels/grid-rows-and-columns 谢谢你,它有帮助!!! 【参考方案1】:

感谢 Peregrine,这对我有帮助

<Grid Grid.ColumnSpan="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="500"/>
                <ColumnDefinition Width="1320"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="500"/>
                <RowDefinition Height="530"/>
            </Grid.RowDefinitions>
            <Image x:Name="cap1time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="1" Grid.Row="1" />
            <Image x:Name="cap2time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="2" Grid.Row="1" />
            <Image x:Name="cap3time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="1" Grid.Row="2"/>
            <Image x:Name="cap4time" Width="250" Height="250" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="UniformToFill" Grid.Column="2" Grid.Row="2" />
            
        </Grid>

【讨论】:

以上是关于WPF C# 图像控件在 540 像素以下被剪切/消失的主要内容,如果未能解决你的问题,请参考以下文章

C# WPF - 选定项文本被剪切

如何防止 WPF 剪切我的图像?

转换后的WPF像素化黑白图像

WPF 裁剪问题

在 wpf C# 中禁用自定义父项时启用子控件

在特定阈值下调整 wpf 控件大小时出现不希望的剪辑