如何设置图片旋转任意角度但是尺寸不变。用这个程序体调试会导致图片旋转一次就变小一次

谢哔哔 2020-01-15 02:57:47
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(pictureBox1.ClientRectangle);
Region region = new Region(gp);
pictureBox1.Region = region;
}

private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Image=Rotate(pictureBox1.Image, 45);
}
public Image Rotate(Image b, int angle)
{
angle = angle % 360;
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
int w = b.Width;
int h = b.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);
Graphics g = Graphics.FromImage(dsImage);
g.InterpolationMode = InterpolationMode.Bilinear;
g.SmoothingMode = SmoothingMode.HighQuality;
Point Offset = new Point((W - w) / 2, (H - h) / 2);
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(360 - angle);
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(b, rect);
g.ResetTransform();
g.Save();
g.Dispose();
return dsImage;
}

private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = Rotate(pictureBox1.Image, 90);
}
}
}
...全文
310 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
desperaso 2020-01-15
  • 打赏
  • 举报
回复
引用 3 楼 weixin_43451707 的回复:

楼上6幅图360度转还不够耍的?
谢哔哔 2020-01-15
  • 打赏
  • 举报
回复
desperaso 2020-01-15
  • 打赏
  • 举报
回复
https://bbs.csdn.net/topics/395656234
的2楼
datafansbj 2020-01-15
  • 打赏
  • 举报
回复
你这张图有多大(像素数)?图像旋转时使用整数运算会带来舍入误差,如果图像本来就不大,那么多次运算带来的总误差就大了。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧