//=======================================================
//图像剪裁、缩放,转化为鼠标光标
//=======================================================
/// <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; }
}