如果是旋转90度的倍数可以,其他的角度图片会变小,怎么解决

雕雨的乞讨者 2020-01-15 03:17:56
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);
}
}
}
...全文
95 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
desperaso 2020-01-15
  • 打赏
  • 举报
回复
http://bbs.cskin.net/thread-15494-1-1.html
winform的图
hztltgg 2020-01-15
  • 打赏
  • 举报
回复
斜边比直角边要长,旋转了原来的地方肯定放不下啊,怎么办就看你需求了,显示的地方不能变那要么裁剪,要么缩小,显示的地方能变就变大

110,533

社区成员

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

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

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