Graphics DrawLine 坐标值很大时出现的异常

本文探讨了在使用Graphics进行绘图时,遇到高坐标值导致的线条绘制方向错误问题。通过分析gfx.ClipBounds的边界限制,揭示了非常高的y坐标值如何被截断,从而引发绘制方向的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



            Graphics gfx = e.Graphics;
            int x = 150;
            int y = 8380000;
            gfx.DrawLine(new Pen(Color.Red, 1), x, x, x + 10, y);
            x = 150;
            y = 8388608 -1;

            gfx.DrawLine(new Pen(Color.Blue, 1), x + 10, x, x + 20, y);



 Graphics gfx = e.Graphics;
            int x = 150;
            int y = 8380000;
            gfx.DrawLine(new Pen(Color.Red, 1), x, x, x + 10, y);
            x = 150;
            y = 8388608 +1;
            gfx.DrawLine(new Pen(Color.Blue, 1), x + 10, x, x + 20, y);


            

发现坐标值太大时,发生反方向,什么原因??

我们发现 8388608是Graphic的ClipBound的宽度和高度

  • My first thought is that as y is really way off the actual bitmap bounds you are pushing the limits too far here. Actually I gave a look at gfx.ClipBounds.Top and found this is 8388608/2. So it's likely your very high y value is truncated in a way that gives either a negative value sligthly above gfx.ClipBounds.Bottom or a positive value slightly below gfx.ClipBounds.Top.

    You have (or had ?) something similar in DirectX that is you could use coordinates way off the actual size of a render target but you still had a high value limit (I believe that if you goes past this value, it fails).

    So just try to avoid using way off values when it's easy to do otherwise (or check against ClipBounds if you can't do otherwise)...




private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (currentTransform != null) { e.Graphics.Transform = currentTransform; // 直接应用预计算矩阵 } e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // 计算实际绘制区域 var clientRect = pictureBox1.ClientRectangle; // 自动计算数据边界(需要预先获取points的实际范围) float dataMinX = points.Min(p => p.fiveX); float dataMaxX = points.Max(p => p.fiveX); float dataMinY = points.Min(p => p.sexY); float dataMaxY = points.Max(p => p.sexY); // 计算缩放比例 float scaleX = clientRect.Width / (dataMaxX - dataMinX); float scaleY = clientRect.Height / (dataMaxY - dataMinY); float zoomLevel = Math.Min(scaleX, scaleY) * 0.9f; // 保留10%边距 // 计算偏移量 float offsetX = -dataMinX * zoomLevel + (clientRect.Width - (dataMaxX - dataMinX) * zoomLevel) / 2; float offsetY = -dataMinY * zoomLevel + (clientRect.Height - (dataMaxY - dataMinY) * zoomLevel) / 2; e.Graphics.TranslateTransform(offsetX, offsetY); e.Graphics.ScaleTransform(zoomLevel, zoomLevel); currentTransform = e.Graphics.Transform; foreach (var point in points) { // 限制缩放级别最小值 float safeZoom = Math.Max(zoomLevel, 0.001f); float pointSize = Math.Min(4f / safeZoom, 100f); // 限制点尺寸最大值 // 计算坐标(确保用 float 运算) float blueX = point.fiveX - pointSize / 2; float blueY = point.sexY - pointSize / 2; // 检查坐标有效性 if (float.IsInfinity(blueX) || float.IsInfinity(blueY) || float.IsNaN(blueX) || float.IsNaN(blueY)) { continue; } // 绘制蓝色点 e.Graphics.FillEllipse(Brushes.Blue, blueX, blueY, pointSize, pointSize); // 同理处理红色点 float redX = point.eightX - pointSize / 2; float redY = point.nineY - pointSize / 2; if (float.IsInfinity(redX) || float.IsInfinity(redY) || float.IsNaN(redX) || float.IsNaN(redY)) { continue; } e.Graphics.FillEllipse(Brushes.Red, redX, redY, pointSize, pointSize); } if (currentTransform != null) { // 应用相同的变换矩阵 e.Graphics.Transform = currentTransform; // 在此绘制内容,坐标会自动适配 e.Graphics.DrawLine(Pens.Black, dataMinX, dataMinY, dataMaxX, dataMaxY); } }// 绘制蓝色点 e.Graphics.FillEllipse(Brushes.Blue, blueX, blueY, pointSize, pointSize); e.Graphics.FillEllipse(Brushes.Red, redX, redY, pointSize, pointSize);OverflowException 怎么解决
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值