You need to enable JavaScript to run this app. WPF C#中Image控件位于窗口540像素高度以下时发生裁切或消失异常
优惠活动
大模型
产品
解决方案
定价
更多

WPF C#中Image控件位于窗口540像素高度以下时发生裁切或消失异常

WPF Image控件固定540px纵向位置渲染裁切问题

问题现象

  • 触发规则:只要Image控件的顶部边距对应的纵向位置落在窗口距顶部540像素刻度线以下区域,控件就会完全从界面中消失
  • 裁切表现:如果控件位置跨在540像素刻度线上,处于刻度线以下的部分会被强制裁切,始终无法正常渲染
  • 排查状态:检索各类公开技术资料,未找到匹配该问题的解决方案
  • 视觉表现:异常效果为自定义绘制的时钟Image控件在对应位置出现下半部分裁切、完全超界消失的情况

相关实现代码

后台时钟绘制逻辑(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" />

内容的提问来源于stack exchange,提问作者Asfa Oryn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.12 04:27:00