惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
小众软件
小众软件
F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
有赞技术团队
有赞技术团队
J
Java Code Geeks
WordPress大学
WordPress大学
The Cloudflare Blog

博客园 - Jonny Yu

[GCC学习]get the optimized function call graph 界面开发中的那些疯狂的小事 Re:架构设计之性能设计经验 技术资产管理 .Net下进程外COM服务器的实现 控件的鼠标拖动和改变大小实现的思考 Learn from mistake, i.e. 和 e.g. 是不同的 How can I hide a user from the Welcome Screen in Windows XP? About HDC window class, OO about wParam and lParam 就差一点点-微妙的强制类型转换 Disable the CrossThreadChecking in .Net Framework v2.0 several useful Store Procedures in MSSQL http://cnblogs.com/rickie/archive/2005/07/02/184927.aspx VS2005使用体验 Inventor 的一些快捷键 First day in Hanna. XPath crash course note
[GDI+]DrawRectangle和FillRectangle,细节决定成败
Jonny Yu · 2005-07-20 · via 博客园 - Jonny Yu

首先, GDI+里坐标网格是通过每个象素的中心的
对于DrawRectangle
其中矩形的长度和宽度指的是象素之间的间隔数,因此如果要绘制
DrawRectangle ( Pens.Black, 0,0, 5,4);
最终会得到长为6个象素宽为5个象素的矩形框。

而在FillRectangle 的时候,指定的长度和宽度是实际矩形的长宽的象素数。
如果仍然按照GDI+的坐标网格来看实际填充的的矩形区域比指定填充区域向左,上各偏移了0.5个象素。

因此在很多时候我们需要为一个填充的矩形区域画边框我们需要小心的给出边界参数。
下面这段代码演示了,如何为矩形区域绘制内边框和外边框。

        private void Form1_Paint(object sender, PaintEventArgs e)
        
{
            Graphics g 
= e.Graphics;

            Rectangle rect 
= new Rectangle(10,10,40,30);

            
g.FillRectangle(Brushes.LightBlue, rect);

            Rectangle innerBounds 
= new Rectangle(rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);
            Rectangle outerBounds 
= new Rectangle(rect.Left - 1, rect.Top - 1, rect.Width + 1, rect.Height + 1);

            g.DrawRectangle(Pens.Brown, innerBounds);
            g.DrawRectangle(Pens.Blue, outerBounds);
        }

这只是我的理解的解决方法,如果大家觉得不对或有更好的实现方法,请尽管拍砖过来啊!

BTW:进一步研究发现Rectangle.Contains的行为也和FillRectangle相同,Rectangle的右上,右下,左下点不算在矩形区域内。所以ClipRectangle的计算又得小心了。