如何设置图片旋转任意角度但是尺寸不变。用这个程序体调试会导致图片旋转一次就变小一次
谢哔哔 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);
}
}
}