void MatrixTests(Graphics g) { // test matrix RotateAt DrawRotatedLines(g, Colors.SkyBlue, rotatedLineCenter, (center, angle) => { var m = Matrix.Create(); m.RotateAt(angle, center); g.MultiplyTransform(m); }); // test matrix scale/translate/rotate DrawRotatedLines(g, Colors.Salmon, rotatedLineCenter, (center, angle) => { var m = Matrix.Create(); m.Translate(center); m.Rotate(angle); m.Translate(-center); m.Scale(.4f); g.MultiplyTransform(m); }); // test rotating arcs DrawRotatedArcs(g, Colors.LightBlue, rotatedLineCenter, (center, angle) => { var m = Matrix.Create(); m.RotateAt(angle, center); g.MultiplyTransform(m); }); // test transformed text DrawRotatedText(g, Colors.Lime, rotatedTextCenter, (center, angle) => { var m = Matrix.Create(); m.RotateAt(angle, center - 40); g.MultiplyTransform(m); }); // test image drawing { g.SaveTransform(); g.MultiplyTransform(Matrix.FromScaleAt(new SizeF(1, -1), imageScaleLocation + image.Size / 2)); g.DrawImage(image, imageScaleLocation); var m = Matrix.Create(); m.Translate(0, -50); m.ScaleAt(0.3f, imageScaleLocation + image.Size / 2); g.MultiplyTransform(m); g.DrawImage(image, imageScaleLocation); g.RestoreTransform(); } // test skewing { g.SaveTransform(); var m = Matrix.Create(); m.Skew(20, 20); g.MultiplyTransform(m); var textSize = g.MeasureString(font, "Skewed Text"); g.DrawText(font, Colors.White, new PointF(110, 0), "Skewed Text"); g.DrawLine(Pens.White, 110, textSize.Height + 2, 110 + textSize.Width, textSize.Height + 2); g.RestoreTransform(); } // test more drawing operations { g.SaveTransform(); var m = Matrix.Create(); m.Translate(480, 20); m.Scale(0.4f); m.Rotate(90); g.MultiplyTransform(m); PixelOffsetSection.Draw(g); g.RestoreTransform(); } }
Control DrawImageToRect() { var image64 = TestIcons.Textures; var bitmap = new Bitmap(new Size(105, 105), PixelFormat.Format32bppRgba); using (var g = new Graphics(bitmap)) { // Draw the "5" portion of the texture at a smaller size at the origin. g.DrawImage(image64, new RectangleF(80, 80, 80, 80), new RectangleF(0, 0, 32, 32)); // draw two rulers to indicate how big the green image should be g.SaveTransform(); g.MultiplyTransform(Matrix.Create(1, 0, 0, 1, 0, 70)); DrawRuler(32, g, Colors.Blue); g.RestoreTransform(); g.SaveTransform(); g.MultiplyTransform(Matrix.Create(0, 1, 1, 0, 70, 0)); DrawRuler(32, g, Colors.Blue); g.RestoreTransform(); } return new DrawableImageView { Image = bitmap }; }
void DirectTests(Graphics g) { // test translate/rotate DrawRotatedLines(g, Colors.SkyBlue, rotatedLineCenter, (center, angle) => { g.TranslateTransform(center); g.RotateTransform(angle); g.TranslateTransform(-center); }); // test translate/rotate/scale DrawRotatedLines(g, Colors.Salmon, rotatedLineCenter, (center, angle) => { g.TranslateTransform(center); g.RotateTransform(angle); g.TranslateTransform(-center); g.ScaleTransform(.4f); }); // test rotating arcs DrawRotatedArcs(g, Colors.LightBlue, rotatedLineCenter, (center, angle) => { g.TranslateTransform(center); g.RotateTransform(angle); g.TranslateTransform(-center); }); // test transformed text DrawRotatedText(g, Colors.Lime, rotatedTextCenter, (center, angle) => { g.TranslateTransform(center - 40); g.RotateTransform(angle); g.TranslateTransform(-center + 40); }); // Test image drawing g.SaveTransform(); g.TranslateTransform(imageScaleLocation + image.Size / 2); g.ScaleTransform(1, -1); g.TranslateTransform(-imageScaleLocation - image.Size / 2); g.DrawImage(image, imageScaleLocation); g.TranslateTransform(0, -50); g.TranslateTransform(imageScaleLocation + image.Size / 2); g.ScaleTransform(0.3f); g.TranslateTransform(-imageScaleLocation - image.Size / 2); g.DrawImage(image, imageScaleLocation); g.RestoreTransform(); // test skewing g.SaveTransform(); g.MultiplyTransform(Matrix.FromSkew(20, 20)); var textSize = g.MeasureString(font, "Skewed Text"); g.DrawText(font, Colors.White, new PointF(110, 0), "Skewed Text"); g.DrawLine(Pens.White, 110, textSize.Height + 2, 110 + textSize.Width, textSize.Height + 2); g.RestoreTransform(); // test more drawing operations g.SaveTransform(); g.TranslateTransform(480, 20); g.ScaleTransform(0.4f); g.RotateTransform(90); PixelOffsetSection.Draw(g); g.RestoreTransform(); }
public void Draw(Graphics graphics) { graphics.SaveTransform(); graphics.MultiplyTransform(transform); draw(graphics); graphics.RestoreTransform(); }
public void Erase(Graphics graphics) { if (transform != null) { graphics.SaveTransform(); graphics.MultiplyTransform(transform); erase(graphics); graphics.RestoreTransform(); } }
Control DrawImageToRect() { var image64 = TestIcons.Textures; var size = new Size(105, 105) * (int)Screen.PrimaryScreen.LogicalPixelSize; var bitmap = new Bitmap(size, PixelFormat.Format32bppRgba); using (var g = new Graphics(bitmap)) { g.PixelOffsetMode = PixelOffsetMode.Half; g.ScaleTransform(Screen.PrimaryScreen.LogicalPixelSize, Screen.PrimaryScreen.LogicalPixelSize); // Draw the "5" portion of the texture at a smaller size at the origin. g.DrawImage(image64, new RectangleF(80, 80, 80, 80), new RectangleF(0, 0, 32, 32)); // draw two rulers to indicate how big the green image should be g.SaveTransform(); g.MultiplyTransform(Matrix.Create(1, 0, 0, 1, 0.5f, 70.5f)); DrawRuler(32, g, Colors.Blue); g.RestoreTransform(); g.SaveTransform(); g.MultiplyTransform(Matrix.Create(0, 1, 1, 0, 70.5f, 0.5f)); DrawRuler(32, g, Colors.Blue); g.RestoreTransform(); } return new DrawableImageView { Image = new Icon(Screen.PrimaryScreen.LogicalPixelSize, bitmap) }; }
static void EllipseAndCurveTest(Graphics graphics) { var path = new GraphicsPath(); path.FillMode = FillMode.Winding; path.AddBezier(new PointF(10f, 6f), new PointF(10f, 4f), new PointF(8f, 2f), new PointF(6f, 2f)); path.CloseFigure(); var m = Matrix.Create(); m.Scale(60f, 60f); graphics.SaveTransform(); graphics.MultiplyTransform(m); var brush = new SolidBrush(Colors.Black); var pen = new Pen(Colors.Red, 0.1f); graphics.FillPath(brush, path); graphics.DrawPath(pen, path); brush.Dispose(); pen.Dispose(); var brushY = new SolidBrush(Color.FromArgb(0xFF, 0xFF, 0x00, 0x9F)); var penB = new Pen(Color.FromArgb(0x00, 0x00, 0xFF, 0x9F), 0.1f); graphics.DrawEllipse(penB, 10f, 6f, 0.5f, 0.5f); graphics.FillEllipse(brushY, 10f, 6f, 0.5f, 0.5f); graphics.DrawEllipse(penB, 6f, 2f, 0.5f, 0.5f); graphics.FillEllipse(brushY, 6f, 2f, 0.5f, 0.5f); graphics.RestoreTransform(); }
static void ScissorsTest(Graphics graphics) { var t = new { CenterX = 0f, CenterY = 0f, OffsetX = 200f, OffsetY = 50f, RotateAngle = 0f, SkewAngleY = 10f, SkewAngleX = 10f, ScaleX = 16f, ScaleY = 16f }; var path = new GraphicsPath(); path.FillMode = FillMode.Winding; // draw some scissors path.AddBezier(new PointF(9.64f, 7.64f), new PointF(9.87f, 7.14f), new PointF(10f, 6.59f), new PointF(10f, 6f)); path.AddBezier(new PointF(10f, 6f), new PointF(10f, 3.79f), new PointF(8.21f, 2f), new PointF(6f, 2f)); path.AddBezier(new PointF(6f, 2f), new PointF(3.79f, 2f), new PointF(2f, 3.79f), new PointF(2f, 6f)); path.AddBezier(new PointF(2f, 6f), new PointF(2f, 8.21f), new PointF(3.79f, 10f), new PointF(6f, 10f)); path.AddBezier(new PointF(6f, 10f), new PointF(6.59f, 10f), new PointF(7.14f, 9.87f), new PointF(7.64f, 9.64f)); path.AddLine(7.64f, 9.64f, 10f, 12f); path.AddLine(10f, 12f, 7.64f, 14.36f); path.AddBezier(new PointF(7.64f, 14.36f), new PointF(7.14f, 14.13f), new PointF(6.59f, 14f), new PointF(6f, 14f)); path.AddBezier(new PointF(6f, 14f), new PointF(3.79f, 14f), new PointF(2f, 15.79f), new PointF(2f, 18f)); path.AddBezier(new PointF(2f, 18f), new PointF(2f, 20.21f), new PointF(3.79f, 22f), new PointF(6f, 22f)); path.AddBezier(new PointF(6f, 22f), new PointF(8.21f, 22f), new PointF(10f, 20.21f), new PointF(10f, 18f)); path.AddBezier(new PointF(10f, 18f), new PointF(10f, 17.41f), new PointF(9.87f, 16.86f), new PointF(9.64f, 16.36f)); path.AddLine(9.64f, 16.36f, 12f, 14f); path.AddLine(12f, 14f, 19f, 21f); path.AddLine(19f, 21f, 22f, 21f); path.AddLine(22f, 21f, 22f, 20f); path.AddLine(22f, 20f, 9.64f, 7.64f); path.CloseFigure(); path.AddBezier(new PointF(6f, 8f), new PointF(4.9f, 8f), new PointF(4f, 7.11f), new PointF(4f, 6f)); path.AddBezier(new PointF(4f, 6f), new PointF(4f, 4.89f), new PointF(4.9f, 4f), new PointF(6f, 4f)); path.AddBezier(new PointF(6f, 4f), new PointF(7.1f, 4f), new PointF(8f, 4.89f), new PointF(8f, 6f)); path.AddBezier(new PointF(8f, 6f), new PointF(8f, 7.11f), new PointF(7.1f, 8f), new PointF(6f, 8f)); path.CloseFigure(); path.AddBezier(new PointF(6f, 20f), new PointF(4.9f, 20f), new PointF(4f, 19.11f), new PointF(4f, 18f)); path.AddBezier(new PointF(4f, 18f), new PointF(4f, 16.89f), new PointF(4.9f, 16f), new PointF(6f, 16f)); path.AddBezier(new PointF(6f, 16f), new PointF(7.1f, 16f), new PointF(8f, 16.89f), new PointF(8f, 18f)); path.AddBezier(new PointF(8f, 18f), new PointF(8f, 19.11f), new PointF(7.1f, 20f), new PointF(6f, 20f)); path.CloseFigure(); path.AddBezier(new PointF(12f, 12.5f), new PointF(11.72f, 12.5f), new PointF(11.5f, 12.28f), new PointF(11.5f, 12f)); path.AddBezier(new PointF(11.5f, 12f), new PointF(11.5f, 11.72f), new PointF(11.72f, 11.5f), new PointF(12f, 11.5f)); path.AddBezier(new PointF(12f, 11.5f), new PointF(12.28f, 11.5f), new PointF(12.5f, 11.72f), new PointF(12.5f, 12f)); path.AddBezier(new PointF(12.5f, 12f), new PointF(12.5f, 12.28f), new PointF(12.28f, 12.5f), new PointF(12f, 12.5f)); path.CloseFigure(); path.AddLine(19f, 3f, 13f, 9f); path.AddLine(13f, 9f, 15f, 11f); path.AddLine(15f, 11f, 22f, 4f); path.AddLine(22f, 4f, 22f, 3f); path.CloseFigure(); path.AddBezier(new PointF(9.64f, 7.64f), new PointF(9.87f, 7.14f), new PointF(10f, 6.59f), new PointF(10f, 6f)); path.AddBezier(new PointF(10f, 6f), new PointF(10f, 3.79f), new PointF(8.21f, 2f), new PointF(6f, 2f)); path.AddBezier(new PointF(6f, 2f), new PointF(3.79f, 2f), new PointF(2f, 3.79f), new PointF(2f, 6f)); path.AddBezier(new PointF(2f, 6f), new PointF(2f, 8.21f), new PointF(3.79f, 10f), new PointF(6f, 10f)); path.AddBezier(new PointF(6f, 10f), new PointF(6.59f, 10f), new PointF(7.14f, 9.87f), new PointF(7.64f, 9.64f)); path.AddLine(7.64f, 9.64f, 10f, 12f); path.AddLine(10f, 12f, 7.64f, 14.36f); path.AddBezier(new PointF(7.64f, 14.36f), new PointF(7.14f, 14.13f), new PointF(6.59f, 14f), new PointF(6f, 14f)); path.AddBezier(new PointF(6f, 14f), new PointF(3.79f, 14f), new PointF(2f, 15.79f), new PointF(2f, 18f)); path.AddBezier(new PointF(2f, 18f), new PointF(2f, 20.21f), new PointF(3.79f, 22f), new PointF(6f, 22f)); path.AddBezier(new PointF(6f, 22f), new PointF(8.21f, 22f), new PointF(10f, 20.21f), new PointF(10f, 18f)); path.AddBezier(new PointF(10f, 18f), new PointF(10f, 17.41f), new PointF(9.87f, 16.86f), new PointF(9.64f, 16.36f)); path.AddLine(9.64f, 16.36f, 12f, 14f); path.AddLine(12f, 14f, 19f, 21f); path.AddLine(19f, 21f, 22f, 21f); path.AddLine(22f, 21f, 22f, 20f); path.AddLine(22f, 20f, 9.64f, 7.64f); path.CloseFigure(); path.AddBezier(new PointF(6f, 8f), new PointF(4.9f, 8f), new PointF(4f, 7.11f), new PointF(4f, 6f)); path.AddBezier(new PointF(4f, 6f), new PointF(4f, 4.89f), new PointF(4.9f, 4f), new PointF(6f, 4f)); path.AddBezier(new PointF(6f, 4f), new PointF(7.1f, 4f), new PointF(8f, 4.89f), new PointF(8f, 6f)); path.AddBezier(new PointF(8f, 6f), new PointF(8f, 7.11f), new PointF(7.1f, 8f), new PointF(6f, 8f)); path.CloseFigure(); path.AddBezier(new PointF(6f, 20f), new PointF(4.9f, 20f), new PointF(4f, 19.11f), new PointF(4f, 18f)); path.AddBezier(new PointF(4f, 18f), new PointF(4f, 16.89f), new PointF(4.9f, 16f), new PointF(6f, 16f)); path.AddBezier(new PointF(6f, 16f), new PointF(7.1f, 16f), new PointF(8f, 16.89f), new PointF(8f, 18f)); path.AddBezier(new PointF(8f, 18f), new PointF(8f, 19.11f), new PointF(7.1f, 20f), new PointF(6f, 20f)); path.CloseFigure(); path.AddBezier(new PointF(12f, 12.5f), new PointF(11.72f, 12.5f), new PointF(11.5f, 12.28f), new PointF(11.5f, 12f)); path.AddBezier(new PointF(11.5f, 12f), new PointF(11.5f, 11.72f), new PointF(11.72f, 11.5f), new PointF(12f, 11.5f)); path.AddBezier(new PointF(12f, 11.5f), new PointF(12.28f, 11.5f), new PointF(12.5f, 11.72f), new PointF(12.5f, 12f)); path.AddBezier(new PointF(12.5f, 12f), new PointF(12.5f, 12.28f), new PointF(12.28f, 12.5f), new PointF(12f, 12.5f)); path.CloseFigure(); path.AddLine(19f, 3f, 13f, 9f); path.AddLine(13f, 9f, 15f, 11f); path.AddLine(15f, 11f, 22f, 4f); path.AddLine(22f, 4f, 22f, 3f); path.CloseFigure(); var m = Matrix.Create(); var c = new PointF((float)t.CenterX, (float)t.CenterY); // translate m.Translate((float)t.OffsetX, (float)t.OffsetY); // rotate m.RotateAt((float)t.RotateAngle, c); // skew m.Translate(-c.X, -c.Y); m.Prepend(Matrix.Create(1, (float)Math.Tan(Math.PI * t.SkewAngleY / 180.0), (float)Math.Tan(Math.PI * t.SkewAngleX / 180.0), 1, 0, 0)); m.Translate(c.X, c.Y); // scale m.ScaleAt((float)t.ScaleX, (float)t.ScaleY, (float)t.CenterX, (float)t.CenterY); graphics.SaveTransform(); graphics.MultiplyTransform(m); var brush = new SolidBrush(Colors.Black); var pen = new Pen(Colors.Red, 0.5f); graphics.FillPath(brush, path); graphics.DrawPath(pen, path); brush.Dispose(); pen.Dispose(); graphics.RestoreTransform(); }