System.Drawing.Rectangleを回転させる方法 (Rotate Rectangle) (C#) (WinForms)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Rectangle r1 = new Rectangle(100, 60, 32, 32);
private Rectangle r2 = new Rectangle(160, 100, 32, 32);
Graphics g;
public Form1()
{
InitializeComponent();
Paint += Form1_Paint;
FormClosing += (s, e) =>
{
g.Dispose();
};
}
public void RotateRectangle(Graphics g, Rectangle r, float angle, Pen pen)
{
using (Matrix m = new Matrix())
{
m.RotateAt(angle, new PointF(r.Left + (r.Width / 2), r.Top + (r.Height / 2)));
g.Transform = m;
g.DrawRectangle(pen, r);
g.ResetTransform();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
g= e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
RotateRectangle(g, r1, 0, Pens.Black);
RotateRectangle(g, r1, 65, Pens.Red);
}
}
}