[轉]C#圖像剪裁、縮放、旋轉、轉化為鼠標光標


        //=======================================================
        //圖像剪裁、縮放,轉化為鼠標光標
        //=======================================================
        /// <summary>
        /// 從圖像pic中截取區域Rect構建新的圖像
        /// </summary>
        public Bitmap GetRect(Image pic, Rectangle Rect)
        {
            //創建圖像
            Rectangle drawRect = new Rectangle(0, 0, Rect.Width, Rect.Height);  //繪制整塊區域
            Bitmap tmp = new Bitmap(drawRect.Width, drawRect.Height);           //按指定大小創建位圖

            //繪制
            Graphics g = Graphics.FromImage(tmp);                   //從位圖創建Graphics對象
            g.Clear(Color.FromArgb(0, 0, 0, 0));                    //清空
            g.DrawImage(pic, drawRect, Rect, GraphicsUnit.Pixel);   //從pic的給定區域進行繪制

            return tmp;     //返回構建的新圖像
        }

        /// <summary>
        /// 從圖像pic中截取區域Rect構建為drawRect大小的圖像
        /// </summary>
        public Bitmap GetRectTo(Image pic, Rectangle Rect, Rectangle drawRect)
        {
            //創建圖像
            Bitmap tmp = new Bitmap(drawRect.Width, drawRect.Height);           //按指定大小創建位圖

            //繪制
            Graphics g = Graphics.FromImage(tmp);                   //從位圖創建Graphics對象
            g.Clear(Color.FromArgb(0, 0, 0, 0));                    //清空
            g.DrawImage(pic, drawRect, Rect, GraphicsUnit.Pixel);   //從pic的給定區域進行繪制

            return tmp;     //返回構建的新圖像
        }
        /// <summary>
        /// 對圖像pic進行縮放,縮放比例reSize
        /// </summary>
        public Bitmap shrinkTo(Image pic, float reSize)
        {
            Size S = new Size((int)(pic.Width * reSize), (int)(pic.Height * reSize));
            Rectangle Rect = new Rectangle(new Point(0, 0), S);

            return shrinkTo(pic, Rect);
        }
        /// <summary>
        /// 對圖像pic進行縮放處理,縮放為Rect大小的新圖像
        /// </summary>
        public Bitmap shrinkTo(Image pic, Rectangle Rect)
        {
            //創建圖像
            Bitmap tmp = new Bitmap(Rect.Width, Rect.Height);                   //按指定大小創建位圖
            Rectangle drawRect = new Rectangle(0, 0, Rect.Width, Rect.Height);  //繪制整塊區域
            Rectangle srcRect = new Rectangle(0, 0, pic.Width, pic.Height);     //pic的整個區域

            //繪制
            Graphics g = Graphics.FromImage(tmp);                   //從位圖創建Graphics對象
            g.Clear(Color.FromArgb(0, 0, 0, 0));                    //清空
            g.DrawImage(pic, drawRect, srcRect, GraphicsUnit.Pixel);//從pic的給定區域進行繪制

            return tmp;     //返回構建的新圖像
        }
        /// <summary>
        /// 對圖像進行任意角度的旋轉
        /// </summary>
        public Bitmap Rotate(Bitmap bmp, float angle)
        {
            return Rotate(bmp, angle, Color.Transparent);
        }
        /// <summary>
        /// 任意角度旋轉,此函數非原創
        /// </summary>
        public Bitmap Rotate(Bitmap bmp, float angle, Color bkColor)
        {
            int w = bmp.Width + 2;
            int h = bmp.Height + 2;

            PixelFormat pf;

            if (bkColor == Color.Transparent)
            {
                pf = PixelFormat.Format32bppArgb;
            }
            else
            {
                pf = bmp.PixelFormat;
            }

            Bitmap tmp = new Bitmap(w, h, pf);
            Graphics g = Graphics.FromImage(tmp);
            g.Clear(bkColor);
            g.DrawImageUnscaled(bmp, 1, 1);
            g.Dispose();

            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(new RectangleF(0f, 0f, w, h));
            Matrix mtrx = new Matrix();
            mtrx.Rotate(angle);
            RectangleF rct = path.GetBounds(mtrx);

            Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
            g = Graphics.FromImage(dst);
            g.Clear(bkColor);
            g.TranslateTransform(-rct.X, -rct.Y);
            g.RotateTransform(angle);
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.DrawImageUnscaled(tmp, 0, 0);
            g.Dispose();

            tmp.Dispose();

            return dst;
        }

        //獲取圖像pic旋轉angle角度后的圖像
        public Bitmap Rotate2(Image pic, float angle)
        {
            //創建圖像
            int size = pic.Width > pic.Height ? pic.Width * 3 : pic.Height * 3;

            Bitmap tmp = new Bitmap(size, size);                           //按指定大小創建位圖
            Rectangle Rect = new Rectangle(0, 0, pic.Width, pic.Height);   //pic的整個區域

            //繪制
            Graphics g = Graphics.FromImage(tmp);                   //從位圖創建Graphics對象
            g.Clear(Color.FromArgb(0, 0, 0, 0));                    //清空

            g.TranslateTransform(Rect.Width / 2, Rect.Height / 2);  //設置為繞中心處旋轉
            g.RotateTransform(angle);                               //控制旋轉角度

            Point pos = new Point((int)((size - pic.Width) / 2), (int)((size - pic.Height) / 2));  //中心對齊
            g.DrawImage(pic, pos);                                  //繪制圖像

            g.TranslateTransform(-Rect.Width / 2, -Rect.Height / 2);//還原錨點為左上角

            return tmp;     //返回構建的新圖像
        }
        /// <summary>
        /// 圖像沿Y軸翻轉
        /// </summary>
        public Bitmap FlipY(Bitmap pic)
        {
            pic.RotateFlip(RotateFlipType.RotateNoneFlipY);
            return pic;
        }

        /// <summary>
        /// 圖像沿X軸翻轉
        /// </summary>
        public Bitmap FlipX(Bitmap pic)
        {
            pic.RotateFlip(RotateFlipType.RotateNoneFlipX);
            return pic;
        }
        /// <summary>
        /// 從給定的圖像創建鼠標光標
        /// </summary>
        public Cursor GetCursor(Bitmap pic)
        {
            try { return new Cursor(pic.GetHicon()); }         //從位圖創建鼠標圖標
            catch (Exception e) { return Cursors.Default; }
        }

        /// <summary>
        /// 獲取用圖像pic,按指定大小width創建鼠標光標
        /// </summary>
        public Cursor GetCursor(Image pic, int width)
        {
            Bitmap icon = new Bitmap(width, width);             //按指定大小創建位圖
            Graphics g = Graphics.FromImage(icon);              //從位圖創建Graphics對象
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(pic, 0, 0, icon.Width, icon.Height);    //繪制Image到位圖

            //Bitmap icon = new Bitmap(tiles[toolsPostion.Y - 1]);

            try { return new Cursor(icon.GetHicon()); }         //從位圖創建鼠標圖標
            catch (Exception e) { return Cursors.Default; }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM