C# Winfrom编程 图片旋转问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 07:27:39
public Bitmap Rotate(Bitmap bmp, float angle, Color bkColor)
{
try
{
angle = angle % 360;

//弧度转换

double radian = angle * Math.PI / 180.0;

double cos = Math.Cos(radian);

double sin = Math.Sin(radian);

//原图的宽和高

int w = bmp.Width;

int h = bmp.Height;

int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));

int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));

//目标位图

Bitmap dsImage = new Bitmap(W, H);

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;

//* 重画一次图片 *//
//新建一个bmp图片
Bitmap bitmap = new Bitmap(w, h);
//新建一个画板
Graphics gg = Graphics.FromImage(bitmap);
//设置高质量插值法
gg.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
gg.SmoothingMode = SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
gg.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
gg.DrawImage(dst, new Rectangle(0, 0, w, h), new Rectangle(0, 0, dst.Width, dst.Height), GraphicsUnit.Pixel);

return bitmap;

按原来图片的大小 重画一次 就行了。。。再返回重画的图片 以上代码是我调试可用的代码。
你试试吧 不懂 hi 我啊

旋转后当然会变大,矩形图片旋转一个小角度后,在外面在画一个水平的矩形,当然变大了。
这是肯定的,如果不要他变大,只能在缩放了。