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

推荐订阅源

T
Tailwind CSS Blog
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - Franky
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
B
Blog
有赞技术团队
有赞技术团队
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
Jina AI
Jina AI
Vercel News
Vercel News
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
The Cloudflare Blog
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
腾讯CDC

博客园 - 周黔

delphi firemonkey 12.3 动态绑定TFDMemTable到TStringGrid delphian firemonkey. android:exported 错误 RAD Studio 12 Delphi 12 实现手机无线wifi调试 (巧用Android Studio 2024 连WIFI) delphi 11 HMACSha512 Android Studio 2024 不需要三方插件,直接wifi 开发调试,真方便 CEF4Delphi(DELPHI Google Chrome 浏览器封装) 折腾 (2)--打开一个网页 CEF4Delphi(DELPHI Google Chrome 浏览器封装) 折腾 (1)--安装 Delphi WINAPI 任意拖动移动控件 DELPHI 调用标准C接口DLL char *value Apache NetBeans IDE 12.3 双击无反应怪事 delphi 10.4 JSON 建立数组示例 delphi 10.3实现TNetHTTPClient 异步 POST 另类实现返回附加标记及Unicode 编码与解码 \u - 周黔 小程序开发笔记1 Delphi DLL 消息循环 Synchronize SendMessage PostMessage 用户中心 - 博客园 delphi 跨版本DLL调用嵌入窗体实现 DELPHI GDI + TGPFont UnitPixel 问题解决 delphi DLL image 动态绘图 句柄处理 delphi 每英寸相素点取值偏差
[转]Regions and Clipping in GDI+
周黔 · 2020-09-29 · via 博客园 - 周黔

This article has been excerpted from book "Graphics Programming with GDI+".

The Graphics class provides methods to clip regions. Using these methods, an application can restrict where graphics objects are drawn. One major use of clipping region is to repaint only part of a control. In some cases painting an entire form is costly in terms of time and memory resources. Clipping plays a vital role by painting only the desired area. The Graphics class provides the SetClip, ResetClip, IntersectClip, ExcludeClip and TranslateClip methods to use in clipping operations.

ExcludeClip excludes the area specified by an argument of type Rectangle or a Region and updates the clipping region. Listing 6.11 fills a rectangle, excluding one small rectangle and a region.

LISTING 6.11: Using ExcludeClip to clip regions


            //Create a Graphics object
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            //Create rectangles
            Rectangle rect1 = new Rectangle(20, 20, 60, 80);
            Rectangle rect2 = new Rectangle(100, 100, 30, 40);

            //Create a region
            Region rgn1 = new Region(rect2);

            //Exclude clip
            g.ExcludeClip(rect1);
            g.ExcludeClip(rgn1);

            //Fill rectangle
            g.FillRectangle(Brushes.Red, 0, 0, 200, 200);

            //Dispose of object
            g.Dispose();

Figure 6.11 shows output from Listing 6.11. The small rectangle and small region are not updated.

SetClip sets the clipping region of a Graphics object. This method has many overloaded forms and takes parameters of type Rectangle, RectangleF, Region, GraphicsPath, and Graphics with or without the CombineMode enumeration. The CombineMode enumeration defines how different clipping regions can be combined (see Table 6.2).

The ResetClip method resets the clipping region to infinity. Listing 6.12 uses the SetClip, ResetClip, and IntersectClip methods.

Listing 6.12: Using the SetClip, ResetClip, and IntersectClip methods


            //Create a Graphics object
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            //Create rectangles and regions
            Rectangle rect1 = new Rectangle(20, 20, 200, 200);
            Rectangle rect2 = new Rectangle(100, 100, 200, 200);
            Region rgn1 = new Region(rect1);
            Region rgn2 = new Region(rect2);

            //Call SetClip
            g.SetClip(rgn1, CombineMode.Exclude);

            //Call IntersectClip
            g.IntersectClip(rgn2);

            //Fill rectangle
            g.FillRectangle(Brushes.Red, 0, 0, 300, 300);

            //Call ResetClip
            g.ResetClip();

            //Draw rectangles
            g.DrawRectangle(Pens.Green, rect1);
            g.DrawRectangle(Pens.Yellow, rect2);
            //Dispose of objects

            g.Dispose();

FIGURE 6.11: ExcludeClip Output

TABLE 6.2: CombineMode members

Member

Description

Complement

The existing region is replaced by the result of the existing region being removed from the new region.

Exclude

The existing region is replaced by the result of the new region being removed from the existing region.

Intersect

Two clipping regions are combined and the result is their intersection.

Replace

One clipping region replaces the other.

Union

Two clipping regions are combined, and the result is their union.

Xor

Two clipping regions are combined and the result is their union minus their intersection.

Note: The CombineMode enumeration is defined in the System.Drawing.Drawing2D namespace.

Figure 6.12 shows the output from Listing 6.12.

TranslateClip translates the clipping region as specified. Listing 6.13 uses the TranslateClip method to translate a region to 20 and 30 points.

LISTING 6.13: Using TranslateClip to translate a region

            //Create a Graphics object
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);

            //Create a Rectangle
            RectangleF rect1 = new RectangleF(20.0f, 20.0f, 200.0f, 200.0f);

            //Create a region
            Region rgn1 = new Region(rect1);

            //Call SetClip
            g.SetClip(rgn1, CombineMode.Exclude);
            float h = 20.0f;
            float w = 30.0f;

            //Call Translate Clip with h and w
            g.TranslateClip(h, w);

            //Fill rectangle
            g.FillRectangle(Brushes.Green, 20, 20, 300, 300);

FIGURE 6.12: Using clip mode

FIGURE 6.13: Using TranslateClip

Figure 6.13 shows the output from Listing 6.13

Clipping Regions Example

Listing 6.14 uses Xor to clip regions

LISTING 6.14: Using the Xor method

            Graphics g = e.Graphics;
            Pen pen = new Pen(Color.Red, 5);
            SolidBrush brush = new SolidBrush(Color.Red);
            Rectangle rect1 = new Rectangle(50, 0, 50, 150);
            Rectangle rect2 = new Rectangle(0, 50, 150, 50);
            Region region = new Region(rect1);
            region.Xor(rect2);
            g.FillRegion(brush, region);

Figure 6.14 shows the output from Listing 6.14.

FIGURE 6.14: Result of the Xor method

Now if we replace Xor with Union:

region.Union(rect2);

the new output looks like Figure 6.15.

Now let's replace Union with Exclude:

region.Exclude(rect2);

FIGURE 6.15: Result of the Union method

FIGURE 6.16: Result of the Exclude method

FIGURE 6.17: Result of the Intersect method

The output looks like Figure 6.16.

If we use the Intersect method:

region.Intersect (rect2);

the output looks like Figure 6.17.

Conclusion


Hope the article would have helped you in understanding 
Regions and Clipping in GDI+. Read other articles on GDI+ on the website.

https://www.c-sharpcorner.com/uploadfile/mahesh/regions-and-clipping-in-gdi/