c#图片在PictureBox内任意角度旋转问题

cjwlz 2012-08-26 11:44:14
问题:不能以我的窗体中心旋转 旋转之后图片出了窗体,必须拉滚动条才能显示(PS:我图片较大,必须要)、
窗体上控件:panel1里有个PictureBox1
附上我得关键代码:
        //任意角度旋转
private void RotateTransformButton_Click(object sender, EventArgs e)
{
try
{
Bitmap a = new Bitmap(pictureBox1.Image);//得到图片框中的图片
pictureBox1.Image = Rotate(a, Convert.ToInt32(textBox1.Text));
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Location = panel1.Location;
pictureBox1.Refresh();//最后刷新图片框
}
catch { }
}

#region 图片旋转函数
/// <summary>
/// 以逆时针为方向对图像进行旋转
/// </summary>
/// <param name="b">位图流</param>
/// <param name="angle">旋转角度[0,360](前台给的)</param>
/// <returns></returns>
public Bitmap Rotate(Bitmap 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;
}
#endregion 图片旋转函数
...全文
1761 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
_小黑_ 2013-06-18
  • 打赏
  • 举报
回复
...........哈哈 ,我认识楼主,
cjwlz 2012-08-28
  • 打赏
  • 举报
回复
求解啊 高手指点迷津吧
cjwlz 2012-08-27
  • 打赏
  • 举报
回复
这样还是不行吧,第一个问题:pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; 当图片过大时,旋转之后,其他部分就没了,这个可以设置成AutoSize;第二个问题:当旋转完之后,你打开的图片还在下面。是吧 等回复哈[Quote=引用 5 楼 的回复:]

C# code

Point[] ps;
public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
int x1 = (pictureBox1.……
[/Quote]
cjwlz 2012-08-26
  • 打赏
  • 举报
回复
我是新手,还是没看懂,呵呵
你把你的代码结合到我上面写的模式上呗,这样写我看起来实在太抽象,见谅呀 呵呵[Quote=引用 3 楼 的回复:]
无所谓,那没什么区别,只需要看那个矩阵怎么写就行了。
[/Quote]
lizhibin11 2012-08-26
  • 打赏
  • 举报
回复
无所谓,那没什么区别,只需要看那个矩阵怎么写就行了。
cjwlz 2012-08-26
  • 打赏
  • 举报
回复
没看懂,可能你没明白我的意思吧
图片在PictureBox内任意角度旋转,[Quote=引用 1 楼 的回复:]
C# code


Image image = Image.FromFile("e:\\oldpc\\dadi.jpg");
Matrix m;
float cos = (float)Math.Cos(Math.PI / 90);
float sin = (float)Math.Sin(Math.PI / 90);
……
[/Quote]
lizhibin11 2012-08-26
  • 打赏
  • 举报
回复

Image image = Image.FromFile("e:\\oldpc\\dadi.jpg");
Matrix m;
float cos = (float)Math.Cos(Math.PI / 90);
float sin = (float)Math.Sin(Math.PI / 90);
float centerx = 150;
float centery = 150;
Point[] ps = { new Point(100, 100), new Point(200, 100), new Point(100, 200) };
Graphics g;

private void button1_Click(object sender, EventArgs e)
{
g.Clear(this.BackColor);
m.TransformPoints(ps);
g.DrawImage(image, ps);
}

private void Form1_Shown(object sender, EventArgs e)
{
m = new Matrix(cos, sin, -sin, cos, centerx + centery * sin - centerx * cos, centery - centery * cos - centerx * sin);
g = Graphics.FromHwnd(this.Handle);
g.DrawImage(image, ps);
}
lizhibin11 2012-08-26
  • 打赏
  • 举报
回复

Point[] ps;
public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
int x1 = (pictureBox1.Width - pictureBox1.Image.Width) / 2;
int x2 = (pictureBox1.Width + pictureBox1.Image.Width) / 2;
int y1 = (pictureBox1.Height - pictureBox1.Image.Height) / 2;
int y2 = (pictureBox1.Height + pictureBox1.Image.Height) / 2;
ps = new Point[] { new Point(x1, y1), new Point(x2, y1), new Point(x1, y2) };
}

private void button1_Click(object sender, EventArgs e)
{
rotate(Math.PI / 90, ps);
}
void rotate(double angle , Point[] ps)
{
Graphics g = pictureBox1.CreateGraphics();
float cos = (float)Math.Cos(angle);
float sin = (float)Math.Sin(angle);
float centerx = pictureBox1.Width / 2;
float centery = pictureBox1.Height / 2;
Matrix m = new Matrix(cos, sin, -sin, cos, centerx + centery * sin - centerx * cos, centery - centery * cos - centerx * sin);
m.TransformPoints(ps);
g.Clear(pictureBox1.BackColor);
g.DrawImage(pictureBox1.Image, ps);
g.Dispose();
}

111,098

社区成员

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

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

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