public static void Draw(Graphics graphics) { var generator = graphics.Generator; var image = TestIcons.TestImage(generator); // lines var whitePen = Pens.White(generator); graphics.DrawLine(whitePen, 1, 1, 99, 99); graphics.DrawLine(whitePen, 50, 1, 50, 99); graphics.DrawLine(whitePen, 1, 51, 99, 51); graphics.DrawRectangle(Pens.White(generator), 101, 1, 100, 100); graphics.DrawRectangle(Pens.White(generator), 101, 1, 10, 10); graphics.DrawEllipse(Pens.Green(generator), 101, 1, 100, 100); graphics.DrawPolygon(Pens.White(generator), new PointF(203, 1), new PointF(253, 51), new Point(203, 101), new PointF(203, 1), new PointF(253, 1), new PointF(253, 101), new PointF(203, 101)); var rect = new RectangleF(255, 1, 100, 100); graphics.DrawArc(Pens.LightGreen(generator), rect, 180, 90); graphics.DrawArc(Pens.SkyBlue(generator), rect, 0, 90); rect.Inflate(-15, 0); graphics.DrawArc(Pens.FloralWhite(generator), rect, -45, 90); rect.Inflate(-5, -20); graphics.DrawArc(Pens.SlateGray(generator), rect, -45, 270); rect.Inflate(-10, -10); graphics.DrawArc(Pens.SteelBlue(generator), rect, 180 + 45, 270); graphics.DrawImage(image, 100, 1, 100, 100); graphics.DrawText(Fonts.Sans(12 * graphics.PointsPerPixel, generator: generator), Colors.White, 0, 104, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); // filled graphics.FillRectangle(Brushes.White(generator), 101, 120, 100, 100); graphics.FillRectangle(Brushes.Gray(generator), 101, 120, 10, 10); graphics.FillEllipse(Brushes.Green(generator), 101, 120, 100, 100); graphics.FillPolygon(Brushes.White(generator), new PointF(202, 120), new PointF(252, 170), new Point(202, 220), new PointF(202, 120)); rect = new RectangleF(255, 120, 100, 100); graphics.FillPie(Brushes.LightGreen(generator), rect, 180, 90); graphics.FillPie(Brushes.SkyBlue(generator), rect, 0, 90); rect.Inflate(-15, 0); graphics.FillPie(Brushes.FloralWhite(generator), rect, -45, 90); rect.Inflate(-5, -20); graphics.FillPie(Brushes.SlateGray(generator), rect, -45, 270); rect.Inflate(-10, -10); graphics.FillPie(Brushes.SteelBlue(generator), rect, 180 + 45, 270); graphics.DrawImage(image, 101, 120, 100, 100); }
Control CreateCustom32Alpha () { var image = new Bitmap (100, 100, PixelFormat.Format32bppRgba); // should always ensure .Dispose() is called when you are done with a Graphics object using (var graphics = new Graphics (image)) { graphics.DrawLine (Color.Blue, Point.Empty, new Point (image.Size)); graphics.DrawRectangle (Color.Black, new Rectangle (image.Size)); } return new ImageView { Image = image }; }
/// <summary> /// /// </summary> /// <param name="gfx"></param> /// <param name="pen"></param> /// <param name="isStroked"></param> /// <param name="p0"></param> /// <param name="p1"></param> private static void DrawLineInternal( Graphics gfx, Pen pen, bool isStroked, ref PointF p0, ref PointF p1) { if (isStroked) { gfx.DrawLine(pen, p0, p1); } }
void Draw(Graphics g, Action<Pen> action) { var path = new GraphicsPath(); path.AddLines(new PointF(0, 0), new PointF(100, 40), new PointF(0, 30), new PointF(50, 70)); for (int i = 0; i < 4; i++) { float thickness = 1f + i * PenThickness; var pen = new Pen(Colors.Black, thickness); pen.LineCap = this.LineCap; pen.LineJoin = this.LineJoin; pen.DashStyle = this.DashStyle; if (action != null) action(pen); var y = i * 20; g.DrawLine(pen, 10, y, 110, y); y = 80 + i * 50; g.DrawRectangle(pen, 10, y, 100, 30); y = i * 70; g.DrawArc(pen, 140, y, 100, 80, 160, 160); y = i * 70; g.DrawEllipse(pen, 260, y, 100, 50); g.SaveTransform(); y = i * 70; g.TranslateTransform(400, y); g.DrawPath(pen, path); g.RestoreTransform(); } }
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(); } }
void DrawRotatedLines(Graphics g, Color color, PointF center, Action<PointF, float> action) { var pen = new Pen(color); for (float i = 0; i < 360f; i += 10) { g.SaveTransform(); action(center, i); g.DrawLine(pen, center, center + new Size(40, 0)); g.RestoreTransform(); } }
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(); }
/// <summary> /// Draws a unit-length horizontal ruler from (0, 0) to (1, 0) into the graphics context. /// Scale and rotate as needed before calling. /// </summary> /// <param name="g"></param> static void DrawRuler(float length, Graphics g, Color color) { g.DrawLine(color, 0, 0, length, 0); var capHeight = 2; g.DrawLine(color, 0, -capHeight, 0, capHeight); g.DrawLine(color, length, -capHeight, length, capHeight); }
void DrawTest(Bitmap image) { // should always ensure .Dispose() is called when you are done with a Graphics or BitmapData object. // Test setting pixels directly using (var bd = image.Lock()) { var sz = image.Size / 5; for (int x = sz.Width; x < sz.Width * 2; x++) for (int y = sz.Height; y < sz.Height * 2; y++) bd.SetPixel(x, y, Colors.Green); } // Test using Graphics object using (var graphics = new Graphics(image)) { graphics.DrawLine(Pens.Blue, Point.Empty, new Point(image.Size)); graphics.DrawRectangle(Pens.Blue, new Rectangle(image.Size - 1)); } // should be able to set pixels after using graphics object using (var bd = image.Lock()) { var sz = image.Size / 5; for (int x = sz.Width * 3; x < sz.Width * 4; x++) for (int y = sz.Height * 3; y < sz.Height * 4; y++) bd.SetPixel(x, y, Colors.Red); } }
public static void Draw(Graphics graphics) { var image = TestIcons.TestImage; // lines var whitePen = new Pen(Colors.White, 1); graphics.DrawLine(whitePen, 1, 1, 99, 99); graphics.DrawLine(whitePen, 50, 1, 50, 99); graphics.DrawLine(whitePen, 1, 51, 99, 51); graphics.DrawRectangle(whitePen, 101, 1, 100, 100); graphics.DrawRectangle(whitePen, 101, 1, 10, 10); graphics.DrawEllipse(Pens.Green, 101, 1, 100, 100); graphics.DrawPolygon(whitePen, new PointF(203, 1), new PointF(253, 51), new Point(203, 101), new PointF(203, 1), new PointF(253, 1), new PointF(253, 101), new PointF(203, 101)); var rect = new RectangleF(255, 1, 100, 100); graphics.DrawArc(Pens.LightGreen, rect, 180, 90); graphics.DrawArc(Pens.SkyBlue, rect, 0, 90); rect.Inflate(-15, 0); graphics.DrawArc(Pens.FloralWhite, rect, -45, 90); rect.Inflate(-5, -20); graphics.DrawArc(Pens.SlateGray, rect, -45, 270); rect.Inflate(-10, -10); graphics.DrawArc(Pens.SteelBlue, rect, 180 + 45, 270); graphics.DrawImage(image, 100, 1, 100, 100); graphics.DrawText(Fonts.Sans(12 * graphics.PointsPerPixel), Colors.White, 0, 104, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); // filled graphics.FillRectangle(Brushes.White, 101, 120, 100, 100); graphics.FillRectangle(Brushes.Gray, 101, 120, 10, 10); graphics.FillEllipse(Brushes.Green, 101, 120, 100, 100); graphics.FillPolygon(Brushes.White, new PointF(202, 120), new PointF(252, 170), new Point(202, 220), new PointF(202, 120)); rect = new RectangleF(255, 120, 100, 100); graphics.FillPie(Brushes.LightGreen, rect, 180, 90); graphics.FillPie(Brushes.SkyBlue, rect, 0, 90); rect.Inflate(-15, 0); graphics.FillPie(Brushes.FloralWhite, rect, -45, 90); rect.Inflate(-5, -20); graphics.FillPie(Brushes.SlateGray, rect, -45, 270); rect.Inflate(-10, -10); graphics.FillPie(Brushes.SteelBlue, rect, 180 + 45, 270); graphics.DrawImage(image, 101, 120, 100, 100); const int offset = 440; var xoffset = offset; var yoffset = 112; for (int i = 1; i < 14; i++) { var pen = new Pen(Colors.White, i); pen.LineCap = PenLineCap.Butt; graphics.DrawLine(pen, xoffset, 1, xoffset, 110); graphics.DrawLine(pen, offset, yoffset, offset + 100, yoffset); xoffset += i + 2; yoffset += i + 2; } }
private static void drawPaths(Graphics g, List<Path> paths, Func<PointF, PointF> pointTransform, float zoomFactor = 1.0f) { g.AntiAlias = false; for (int i = 0; i < paths.Count; i++) { var path = paths[i]; for (int pointIdx = 0; pointIdx < (path.Points.Count - 1); pointIdx++) { var p1 = pointTransform(path.Points[pointIdx]); var p2 = pointTransform(path.Points[pointIdx + 1]); var pen = path.GetScaledPen(zoomFactor); g.DrawLine(pen, p1, p2); } } }