九宫切图实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zuoyefeng1990/article/details/62058700
public static void RenderBackground(Graphics g, Image img, Rectangle rect) {
    //填充四个角
    g.DrawImage(img, new Rectangle(rect.X, rect.Y, 5, 5), 
        new Rectangle(0, 0, 5, 5), GraphicsUnit.Pixel);
    g.DrawImage(img, new Rectangle(rect.Right - 5, rect.Y, 5, 5), 
        new Rectangle(img.Width - 5, 0, 5, 5), GraphicsUnit.Pixel);
    g.DrawImage(img, new Rectangle(rect.X, rect.Bottom - 5, 5, 5), 
        new Rectangle(0, img.Height - 5, 5, 5), GraphicsUnit.Pixel);
    g.DrawImage(img, new Rectangle(rect.Right - 5, rect.Bottom - 5, 5, 5), 
        new Rectangle(img.Width - 5, img.Height - 5, 5, 5), GraphicsUnit.Pixel);
    //四边
    g.DrawImage(img, new Rectangle(rect.X, rect.Y + 5, 5, rect.Height - 10), 
        new Rectangle(0, 5, 5, img.Height - 10), GraphicsUnit.Pixel);
    g.DrawImage(img, new Rectangle(rect.X + 5, rect.Y, rect.Width - 10, 5), 
        new Rectangle(5, 0, img.Width - 10, 5), GraphicsUnit.Pixel);
    g.DrawImage(img, new Rectangle(rect.Right - 5, rect.Y + 5, 5, rect.Height - 10), 
        new Rectangle(img.Width - 5, 5, 5, img.Height - 10), GraphicsUnit.Pixel);
    g.DrawImage(img, new Rectangle(rect.X + 5, rect.Bottom - 5, rect.Width - 10, 5), 
        new Rectangle(5, img.Height - 5, img.Width - 10, 5), GraphicsUnit.Pixel);
    //中间
    g.DrawImage(img, 
        new Rectangle(rect.X + 5, rect.Y + 5, rect.Width - 10, rect.Height - 10), 
        new Rectangle(5, 5, img.Width - 10, img.Height - 10), GraphicsUnit.Pixel);
 
}


public class TestA : Control
{
    protected override void OnPaint(PaintEventArgs e) {
        RenderHelper.RenderBackground(
            e.Graphics,
            Properties.Resources.Qbtn_Gray,//资源图
            this.ClientRectangle);
        base.OnPaint(e);
    }
}

展开阅读全文

没有更多推荐了,返回首页