gdi programming with c sharp phần 5 pdf

70 469 1
gdi programming with c sharp phần 5 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Rectangle rect1 = new Rectangle(20, 20, 60, 80); Rectangle rect2 = new Rectangle(50, 30, 60, 80); Region rgn1 = new Region(rect1); Region rgn2 = new Region(rect2); // If region is not empty, empty it if (! rgn1.IsEmpty(g)) rgn1.MakeEmpty(); // If region is not infinite, make it infinite if (! rgn2.IsInfinite(g)) rgn2.MakeInfinite(); // Get bounds of the infinite region RectangleF rect = rgn2.GetBounds(g); // Display MessageBox.Show(rect.ToString()); // Fill the region g.FillRegion(Brushes.Red, rgn2); // Dispose of object g.Dispose(); An infinite region's starting coordinates are negative numbers, and its height and width are large positive numbers, as Figure 6.10 shows. Using FillRegion on an infinite region fills the entire form. Figure 6.10. Bounds of an infinite region [ Team LiB ] This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [ Team LiB ] 6.3 Regions and Clipping As we discussed in Chapter 3, 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 regions 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. Figure 6.11. ExcludeClip output This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. 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 object g.Dispose(); This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. Table 6.2. CombineMode members MemberDescription 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. Figure 6.12. Using Clip methods This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. TranslateClip translates the clipping region as specified. Listing 6.13 uses the TranslateClip method to translate a region by 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 RectangleF 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 TranslateClip with h and w g.TranslateClip(h, w); // Fill rectangle g.FillRectangle(Brushes.Green, 20, 20, 300, 300); Figure 6.13 shows the output from Listing 6.13. Figure 6.13. Using TranslateClip [ Team LiB ] This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [ Team LiB ] 6.4 Clipping Regions Example Listing 6.14 uses Xor to clip regions. Listing 6.14 Using the Xor method 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. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. Figure 6.15. Result of the Union method Now let's replace Union with Exclude: region.Exclude(rect2); The output looks like Figure 6.16. Figure 6.16. Result of the Exclude method If we use the Intersect method: This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. region.Intersect(rect2); the output looks like Figure 6.17. Figure 6.17. Result of the Intersect method [ Team LiB ] This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [ Team LiB ] This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. 6.5 Regions, Nonrectangular Forms, and Controls When we're writing Windows applications with drawing functionality, it becomes important to understand the roles of regions, client areas, and nonclient areas. This section will describe an exciting and wonderful use of regions. Figure 6.18 shows a typical rectangular form. As you can see, the title bar area usually contains the title of the form, as well as minimize, maximize, and close buttons. This is the nonclient area; the rest of the form is the client area. Graphics objects can be drawn only in the client area. The combination of both client and nonclient areas is the default region of a form. Figure 6.18. Client and nonclient areas of a form What exactly is a region? A region is a collection of pixels that represents part of a control. GDI+ is responsible only for drawing the region associated with a window (a form or control). The default region of a window includes both client and nonclient areas, so GDI+ draws the entire window. However, you can force the operating system to display only part of a window. This is where regions are useful. 6.5.1 The Application This section will show you the importance of regions and how you can use them in real-world applications. Have you ever thought about writing nonrectangular forms or controls? How about writing circular, triangular, or polygonal forms, buttons, This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [...]... Listing 6.17 shows the code for the context menu click event handlers Listing 6.17 Menu item click event handlers private void CircleMenu_Click(object sender, System.EventArgs e) { // Create a rectangle Rectangle rect = new Rectangle (50 , 0, 300, 300); // Create a Shape object and call // the GetRectRegion method Shape shp = new Shape(); This document was created by an unregistered ChmMagic, please go to... and images with shading A vector image is a collection of one or more vectors Mathematically, a vector is a combination of a magnitude and a direction, which can be used to represent the relationships between points, lines, curves, and filled areas In vector images, a vector is the entity to be controlled Each vector can have a separate color or shade So a vector image with a line and a rectangle is... 100 Graphics g = e.Graphics; Image curImage = Image.FromFile(curFileName); Rectangle rect = new Rectangle(20, 20, 100, 100); g.DrawImage(curImage, rect); If you want to fill the entire form with an image, you can use the ClientRectangle property of the form as the default rectangle Graphics g = e.Graphics; This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register... that we create a region from the path and return it See Chapters 3 and 9 for more about the GraphicsPath class Similarly, we create a region from a rectangle in the GetRectRegion method Listing 6. 15 The Shape class // The Shape class contains the functionality // of shaped controls public class Shape { public Shape() { } public Region GetPolyRegion(Point[] pts) { // Create a graphics path GraphicsPath... class provides functionality to work with raster images, and the Metafile class provides functionality to work with vector images Both classes are inherited from the Image class In this chapter we will discuss theImage and Bitmap classes and their members TheMetafile class will be discussed in Chapter 8 We'll start this discussion with the Image class, which is defined in theSystem.Drawing namespace... 0) }; this.BackColor = Color.CornflowerBlue; // Create a Shape object and call // the GetPolyRegion method Shape shp = new Shape(); this.Region = shp.GetPolyRegion(pts); } The code in Listing 6.18 for the Close menu item simply closes the form Listing 6.18 The Close menu click event handler private void CloseMenu_Click(object sender, System.EventArgs e) { this.Close(); } This document was created by... Image.GetThumbnailImageAbort tnCallBack = new Image.GetThumbnailImageAbort(tnCallbackMethod); // Get the thumbnail image Image thumbNailImage = curImage.GetThumbnailImage (100, 100, tnCallBack, IntPtr.Zero); // Create a Graphics object Graphics tmpg = this.CreateGraphics(); tmpg.Clear(this.BackColor); // Draw thumbnail image tmpg.DrawImage(thumbNailImage, 40, 20); This document was created by an unregistered ChmMagic, please... a set of vectors in which each vector has different properties, such as color or shade Vector graphics are mathematically described and appear smooth at any size or resolution, and they are often used by mechanical and architectural engineers Vector images can be transformed from one state to another without any loss of data Transforming raster images, however, may cause data loss or reduce the quality... nonrectangular form and controls 6 .5. 2 Coding In Windows Forms, every control, including a form, is derived from the Control class The Region property of the control class represents the region of control If you set the Region property of a control, only the area covered by that region will be visible to the user Section 6 .5. 2.1 through 6 .5. 2.6 describe the steps involved in writing code for nonrectangular... cameras It was originally developed by the Japan Electronic Industry Development Association The EXIF file contains an image compressed according to the JPEG specification 7.1.2 .5 PNG This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks Portable Network Graphics (PNG) format provides the advantages of the GIF format but supports greater color . 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 =. 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); //. item click event handlers private void CircleMenu_Click(object sender, System.EventArgs e) { // Create a rectangle Rectangle rect = new Rectangle (50 , 0, 300, 300); // Create a Shape object

Ngày đăng: 12/08/2014, 19:20

Từ khóa liên quan

Mục lục

  • Chapter 6. Rectangles and Regions

    • 6.3 Regions and Clipping

    • 6.4 Clipping Regions Example

    • 6.5 Regions, Nonrectangular Forms, and Controls

    • SUMMARY

    • Chapter 7. Working with Images

      • 7.1 Raster and Vector Images

      • 7.2 Working with Images

      • 7.3 Manipulating Images

      • 7.4 Playing Animations in GDI+

      • 7.5 Working with Bitmaps

      • 7.6 Working with Icons

      • 7.7 Skewing Images

Tài liệu cùng người dùng

Tài liệu liên quan