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; }}

接受自己的失败面,是一种成熟,更是一种睿智;

C#图像剪裁、缩放、旋转、转化为鼠标光标

相关文章:

你感兴趣的文章:

标签云: