画九宫图

    /// <summary>
        /// 绘制窗体边框
        /// </summary>
        /// <param name="destForm">需要绘制边框的窗体</param>
        /// <param name="g">绘制边框所用的绘图对象</param>
        /// <param name="fringeImg">边框图片</param>
        /// <param name="radius">边框的圆角矩形</param>
        public static void DrawFormFringe(Form destForm, Graphics g, Image fringeImg, int radius)
        {
            DrawNineRect(
                g,
                fringeImg,
                new Rectangle(-radius, -radius, destForm.ClientSize.Width + 2 * radius, destForm.ClientSize.Height + 2 * radius),
                new Rectangle(0, 0, fringeImg.Width, fringeImg.Height));
        }



        /// <summary>
        /// 画九宫图
        /// </summary>
        /// <param name="g">绘图对象</param>
        /// <param name="img">所需绘制的图片</param>
        /// <param name="DestRect">目标矩形</param>
        /// <param name="SrcRect">来源矩形</param>
        public static void DrawNineRect(Graphics g, Image img, Rectangle DestRect, Rectangle SrcRect)
        {
            int offset = 5;
            Rectangle NineRect = new Rectangle(img.Width / 2 - offset, img.Height / 2 - offset, 2 * offset, 2 * offset);
            int x = 0, y = 0, nWidth, nHeight;
            int xSrc = 0, ySrc = 0, nSrcWidth, nSrcHeight;
            int nDestWidth, nDestHeight;
            nDestWidth = DestRect.Width;
            nDestHeight = DestRect.Height;
            // 左上-------------------------------------;
            x = DestRect.Left;
            y = DestRect.Top;
            nWidth = NineRect.Left - SrcRect.Left;
            nHeight = NineRect.Top - SrcRect.Top;
            xSrc = SrcRect.Left;
            ySrc = SrcRect.Top;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
            // 上-------------------------------------;
            x = DestRect.Left + NineRect.Left - SrcRect.Left;
            nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
            xSrc = NineRect.Left;
            nSrcWidth = NineRect.Right - NineRect.Left;
            nSrcHeight = NineRect.Top - SrcRect.Top;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
            // 右上-------------------------------------;
            x = DestRect.Right - (SrcRect.Right - NineRect.Right);
            nWidth = SrcRect.Right - NineRect.Right;
            xSrc = NineRect.Right;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
            // 左-------------------------------------;
            x = DestRect.Left;
            y = DestRect.Top + NineRect.Top - SrcRect.Top;
            nWidth = NineRect.Left - SrcRect.Left;
            nHeight = DestRect.Bottom - y - (SrcRect.Bottom - NineRect.Bottom);
            xSrc = SrcRect.Left;
            ySrc = NineRect.Top;
            nSrcWidth = NineRect.Left - SrcRect.Left;
            nSrcHeight = NineRect.Bottom - NineRect.Top;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
            // 中-------------------------------------;
            x = DestRect.Left + NineRect.Left - SrcRect.Left;
            nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
            xSrc = NineRect.Left;
            nSrcWidth = NineRect.Right - NineRect.Left;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);

            // 右-------------------------------------;
            x = DestRect.Right - (SrcRect.Right - NineRect.Right);
            nWidth = SrcRect.Right - NineRect.Right;
            xSrc = NineRect.Right;
            nSrcWidth = SrcRect.Right - NineRect.Right;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);

            // 左下-------------------------------------;
            x = DestRect.Left;
            y = DestRect.Bottom - (SrcRect.Bottom - NineRect.Bottom);
            nWidth = NineRect.Left - SrcRect.Left;
            nHeight = SrcRect.Bottom - NineRect.Bottom;
            xSrc = SrcRect.Left;
            ySrc = NineRect.Bottom;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
            // 下-------------------------------------;
            x = DestRect.Left + NineRect.Left - SrcRect.Left;
            nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
            xSrc = NineRect.Left;
            nSrcWidth = NineRect.Right - NineRect.Left;
            nSrcHeight = SrcRect.Bottom - NineRect.Bottom;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
            // 右下-------------------------------------;
            x = DestRect.Right - (SrcRect.Right - NineRect.Right);
            nWidth = SrcRect.Right - NineRect.Right;
            xSrc = NineRect.Right;
            g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
        }

转载于:https://www.cnblogs.com/yixiong/p/3199877.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值