Graphics: TranslateClip

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;

using System.Windows.Forms;

class KeyholeClipCentered : Form {
    GraphicsPath path = new GraphicsPath();
    Image image = Image.FromFile("Color.jpg");
    public static void Main() {
        Application.Run(new KeyholeClipCentered());
    }
    public KeyholeClipCentered() {
        ResizeRedraw = true;
        path.AddArc(80, 0, 80, 80, 45, -270);
        path.AddLine(70, 180, 170, 180);
    }
    protected override void OnPaint(PaintEventArgs pea) {
        DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);
    }
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
        grfx.SetClip(path);

        RectangleF rectf = path.GetBounds();
        int xOffset = (int)((cx - rectf.Width) / 2 - rectf.X);
        int yOffset = (int)((cy - rectf.Height) / 2 - rectf.Y);

        grfx.TranslateClip(xOffset, yOffset);
        grfx.DrawImage(image, xOffset, yOffset, image.Width, image.Height);
    }
}

    


This entry was posted in 2D Graphics. Bookmark the permalink.