C#绘图正常是不涉及到旋转的。有时候会有旋转画笔的情况。比如条码打印字竖着打印。旋转图片一定角度绘制。或者斜着画水印。这时候就涉及到旋转画笔了。
通过graphics.TranslateTransform(Pcenter.X, Pcenter.Y);设置旋转的中心坐标。通过graphics.RotateTransform(i%360);设置画笔的旋转角度。
参照示例:
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.Threading;
namespace 图片操作
{
public partial class Form1 : Form
{
bool a = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(旋转));
thread.Start();
}
private void 旋转()
{
#region MyRegion
Graphics graphics = this.CreateGraphics();
graphics.Clear(Color.White);
//装入图片
Bitmap image = new Bitmap("1.bmp");
//定义绘图区域
RectangleF picRect = new RectangleF(100, 100, image.Width, image.Height);
PointF Pcenter = new PointF(picRect.X + picRect.Width / 2,
picRect.Y + picRect.Height / 2);
#endregion
//让图片绕中心旋转一周
for (int i = 0;; i += 5)
{
//刷新界面
//this.Refresh();
// 绘图平面以图片的中心点旋转
graphics.TranslateTransform(Pcenter.X, Pcenter.Y);//通过使此 Graphics 的变换矩阵左乘指定的平移来更改坐标系统的原点
graphics.RotateTransform(i%360);//将指定旋转应用于此 Graphics 的变换矩阵
//恢复绘图平面在水平和垂直方向的平移
graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
//绘制图片并延时
graphics.DrawImage(image, picRect);
Thread.Sleep(50);
//重置绘图平面的所有变换
graphics.ResetTransform();
if (a)
{
break;
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
a = true;
Application.Exit();
}
}
}
代码的效果就是把.net图标不停的360度旋转
供需要绘图的同学参考