如果是旋转90度的倍数可以,其他的角度图片会变小,怎么解决
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace WindowsFormsApp9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GraphicsPath path = new GraphicsPath();
path.AddEllipse(this.button1.ClientRectangle);
this.button1.Region = new Region(path);
//设置边框
this.button1.FlatStyle = FlatStyle.Flat;
//this.button1.FlatAppearance.BorderColor = this.button1.BackColor;
this.button1.FlatAppearance.BorderSize = 0;
GraphicsPath path1 = new GraphicsPath();
path1.AddEllipse(this.button2.ClientRectangle);
this.button2.Region = new Region(path1);
//设置边框
this.button2.FlatStyle = FlatStyle.Flat;
//this.button2.FlatAppearance.BorderColor = this.button2.BackColor;
this.button2.FlatAppearance.BorderSize = 0;
GraphicsPath path2 = new GraphicsPath();
path2.AddEllipse(this.button3.ClientRectangle);
this.button3.Region = new Region(path2);
//设置边框
this.button3.FlatStyle = FlatStyle.Flat;
this.button3.FlatAppearance.BorderSize = 0;
button3.BackColor = Color.Transparent;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("","你好");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("", "欢迎");
}
private void button4_Click(object sender, EventArgs e)
{
this.button1.BackgroundImage = RotateImg2(this.button1.BackgroundImage, 90, this.button1);
}
public Image RotateImg2(Image b, int angle, Button b1)
{
angle = angle % 360; //弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
int btw = b1.Width;
int bth = b1.Height;
//原图的宽和高
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)));
//目标位图
Image dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.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();
//dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
return dsImage;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.button1.BackgroundImage = RotateImg2(this.button1.BackgroundImage, 90, this.button1);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace WindowsFormsApp9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
GraphicsPath path = new GraphicsPath();
path.AddEllipse(this.button1.ClientRectangle);
this.button1.Region = new Region(path);
//设置边框
this.button1.FlatStyle = FlatStyle.Flat;
//this.button1.FlatAppearance.BorderColor = this.button1.BackColor;
this.button1.FlatAppearance.BorderSize = 0;
GraphicsPath path1 = new GraphicsPath();
path1.AddEllipse(this.button2.ClientRectangle);
this.button2.Region = new Region(path1);
//设置边框
this.button2.FlatStyle = FlatStyle.Flat;
//this.button2.FlatAppearance.BorderColor = this.button2.BackColor;
this.button2.FlatAppearance.BorderSize = 0;
GraphicsPath path2 = new GraphicsPath();
path2.AddEllipse(this.button3.ClientRectangle);
this.button3.Region = new Region(path2);
//设置边框
this.button3.FlatStyle = FlatStyle.Flat;
this.button3.FlatAppearance.BorderSize = 0;
button3.BackColor = Color.Transparent;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("","你好");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("", "欢迎");
}
private void button4_Click(object sender, EventArgs e)
{
this.button1.BackgroundImage = RotateImg2(this.button1.BackgroundImage, 90, this.button1);
}
public Image RotateImg2(Image b, int angle, Button b1)
{
angle = angle % 360; //弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
int btw = b1.Width;
int bth = b1.Height;
//原图的宽和高
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)));
//目标位图
Image dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.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();
//dsImage.Save("yuancd.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
return dsImage;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.button1.BackgroundImage = RotateImg2(this.button1.BackgroundImage, 90, this.button1);
}
}
}
...全文
相关推荐
vc实现BMP图片旋转90度
然后,创建一个新的位图信息头,调整宽度和高度值,因为旋转90度后,图像的宽高会互换。接着,根据旋转角度,计算新的像素位置,并将读取的像素写入新位置。
bmplib.rar_bmp_bmp 旋转_bmp旋转_图片旋转_图片旋转90度
本资源“bmplib.rar”包含一个名为“bmplib”的库,专注于BMP图片的旋转功能,特别是以90度倍数进行旋转。
C#图像任意角度旋转
在实际应用中,你可能还需要处理一些边界条件,比如负角度的旋转,以及处理非90度倍数的旋转时可能出现的图像溢出问题。
图片缩放和旋转
其次,图片旋转则涉及到改变图像的方向。旋转可以是90度的倍数,也可以是任意角度。对于90度、180度、270度的旋转,大多数库都提供了便捷的方法。
DELPHI 图片任意角旋转DELPHI 图片任意角旋转
最后,如果旋转角度不是90度的倍数,新图像的尺寸可能与原始图像不同,需要调整新图像的大小以适应旋转后的形状。


