Documents for Imaging .NET Edition
Features / Work with Graphics / Clip Region
In This Topic
    Clip Region
    In This Topic

    A clip region refers to a specific part of an image and is defined to limit the drawing operations for an image to a specific part of the image.

    In GcImaging, the clip region can be created using the CreateClipRegion method of the GcBitmapGraphics class which takes a Rectangle or a GraphicsPath as a parameter. The defined clip region is applied to the image using the PushClip method which limits the drawing operations to the clip region. After the required drawing operations are done, you can use the PopClip method to remove the clip region and make the complete image surface available for any further drawing operations.

    A beautiful home with a clipped region on top right

    To clip an image:

    1. Create an instance of GcBitmap.
    2. Load an image into the GcBitmap instance.
    3. Define a clip rectangle for adding text.
    4. Add text to the rectangle.
    5. Pass the rectangle as a parameter in PushClip method of the GcBitmapGraphics class.
      C#
      Copy Code
      var backColor = Color.FromArgb(unchecked((int)0xff0066cc));
      var foreColor = Color.FromArgb(unchecked((int)0xffffcc00));
      float cw = 450, ch = 300, pad = 10, bord = 4;
      int pixelWidth = 1024, pixelHeight = 1024;
      
      GcBitmap origBmp = new GcBitmap(pixelWidth, pixelHeight, true);
      var path = Path.Combine("Resources", "Images", "tudor.jpg");
      origBmp.Load(path);
      
      GcBitmapGraphics g = origBmp.CreateGraphics();
      
      RectangleF clipRc = new RectangleF(pixelWidth - cw - pad,
          
          pad, cw, ch);
      
      using (g.PushClip(clipRc))
      {
          g.FillRectangle(clipRc, Color.Blue);
          g.DrawString("This is a beautiful home",
              new TextFormat()
              {
                  Font = Font.FromFile(Path.Combine("Resources", 
                  "Fonts", "times.ttf")),
                  FontSize = 16,
                  ForeColor = foreColor
              },
              clipRc
              );
      }
      
      //Save the image with clipped region
      origBmp.SaveAsJpeg("ClipImageTest.jpeg");
      
    Back to Top

    For more information about implementation of clipping using GcImaging, see GcImaging sample browser.