protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2);
g.DrawLine(p, 100, 100, 200, 200);
}
这条线的中点是(150,150);怎样实现按45度的旋转?可以给出代码吗? 谢谢
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2);
g.DrawLine(p, 100, 100, 200, 200);
}
这条线的中点是(150,150);怎样实现按45度的旋转?可以给出代码吗? 谢谢
2.位图旋转参考:/// <summary>
/// 任意角度旋转
/// </summary>
/// <param name="bmp">原始图Bitmap</param>
/// <param name="angle">旋转角度</param>
/// <param name="bkColor">背景色</param>
/// <returns>输出Bitmap</returns>
public static Bitmap KiRotate(Bitmap bmp, float angle, Color bkColor)
...{
int w = bmp.Width + 2;
int h = bmp.Height + 2; PixelFormat pf; if (bkColor == Color.Transparent)
...{
pf = PixelFormat.Format32bppArgb;
}
else
...{
pf = bmp.PixelFormat;
} Bitmap tmp = new Bitmap(w, h, pf);
Graphics g = Graphics.FromImage(tmp);
g.Clear(bkColor);
g.DrawImageUnscaled(bmp, 1, 1);
g.Dispose(); GraphicsPath path = new GraphicsPath();
path.AddRectangle(new RectangleF(0f, 0f, w, h));
Matrix mtrx = new Matrix();
mtrx.Rotate(angle);
RectangleF rct = path.GetBounds(mtrx); Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
g = Graphics.FromImage(dst);
g.Clear(bkColor);
g.TranslateTransform(-rct.X, -rct.Y);
g.RotateTransform(angle);
g.InterpolationMode = InterpolationMode.HighQualityBilinear;
g.DrawImageUnscaled(tmp, 0, 0);
g.Dispose(); tmp.Dispose();
return dst;
}
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2);
g.DrawLine(p, 100, 100, 200, 200);
// 用背景色重画上面这条线
double PI = 3.1415926;
g.DrawLine(Pens.Red, 100, 100, float.Parse((200 * Math.Cos(PI / 6)).ToString()), float.Parse((200 * Math.Sin(PI / 6)).ToString()));
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication26
{
public partial class Form1 : Form
{
int T = 0; public Form1()
{
InitializeComponent(); timer1.Enabled = true;
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2);
g.DrawLine(p, 100, 100, 100 + (float)Math.Sin(T * 45 * Math.PI / 180) * 100, 100 + (float)Math.Cos(T * 45 * Math.PI / 180) * 100);
} private void timer1_Tick(object sender, EventArgs e)
{
T++;
this.Invalidate();
}
}
}
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2);
g.DrawLine(p, 100, 100, 200, 200);
} 对这条直线进行旋转,我用的重载ONPAINT方法。
上面两位朋友给的代码好象都有问题,结果不对啊。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ }
void DrawLineWithAngle(Graphics g,Pen p,Point Mid,int Length,float angle)
{
Point A,B;
A=new Point();
B = new Point();
A.X =(int)(Mid.X + (float)Math.Sin(angle * Math.PI / 180) * Length/2);
A.Y = (int)(Mid.Y + (float)Math.Cos(angle * Math.PI / 180) * Length / 2);
B.X =(int)( Mid.X - (float)Math.Sin(angle * Math.PI / 180) * Length / 2);
B.Y = (int)(Mid.Y - (float)Math.Cos(angle * Math.PI / 180) * Length / 2);
g.DrawLine(p,A,B);
} private void Form1_Paint(object sender, PaintEventArgs e)
{
//base.OnPaint(sender,e);
Graphics g = e.Graphics;
Pen p = new Pen(Color.Black, 2);
Point mid=new Point(150,150);
DrawLineWithAngle(g,p,mid,200,45);
DrawLineWithAngle(g, p, mid, 200, 60);
}
}
}
OrgImage就是原始图片,你把要画的都先画到这个图里去,
#region void RotateCLImage() // 旋转齿轮图片
/// <summary>
/// 旋转齿轮图片
/// </summary>
void RotateCLImage()
{
Image OrgImage = DM.GetImageSafely(PbCL); // 我这里是从picturebox里获得bitmap,而你的要事前先new一个bitmap给OrgImage然后draw好
Bitmap ResultImage = new Bitmap(OrgImage.Width, OrgImage.Height);
Graphics ResultGraphics = Graphics.FromImage(ResultImage);
ResultGraphics.Clear(Color.White);
Bitmap TempImage = new Bitmap(OrgImage);
ResultGraphics.TranslateTransform(TempImage.Width / 2, TempImage.Height / 2);
ResultGraphics.RotateTransform(RotateAngle);
RotateAngle = RotateAngle + 6 % 360; // RotateAngle 是旋转角度,开始是0
ResultGraphics.TranslateTransform(-TempImage.Width / 2, -TempImage.Height / 2);
ResultGraphics.DrawImage(TempImage, 0, 0);
DM.SetImageSafely(PbRunCl, ResultImage);
}
#endregion