How to draw a line on a image in C#?

How to draw a line on a image in C#?

To draw a line on an image in C# using the .NET Framework, you can use the System.Drawing namespace. Here's an example:

using System.Drawing; // Load the image from a file Image image = Image.FromFile("path/to/image.jpg"); // Create a Graphics object from the image Graphics graphics = Graphics.FromImage(image); // Set the line color and width Pen pen = new Pen(Color.Red, 5); // Draw the line on the image graphics.DrawLine(pen, new Point(0, 0), new Point(100, 100)); // Save the modified image to a file image.Save("path/to/new/image.jpg"); 

In this example, we load an image from a file using the Image.FromFile method. We then create a Graphics object from the image using the Graphics.FromImage method. We create a Pen object with a red color and a width of 5 pixels. We then use the DrawLine method of the Graphics object to draw a line from the point (0, 0) to the point (100, 100) using the Pen object.

Finally, we save the modified image to a file using the Image.Save method.

Note that this is just one example of how to draw a line on an image in C#. There are many other techniques you can use, such as using a Bitmap object or applying other types of drawing operations. Depending on your specific use case, you may need to use additional techniques or options to achieve the desired result.

Examples

  1. "Draw a line on an image in C#"

    • Code:
      using System.Drawing; class Program { static void Main() { Bitmap image = new Bitmap("input.jpg"); Graphics graphics = Graphics.FromImage(image); Pen pen = new Pen(Color.Red, 2); // Draw a line on the image graphics.DrawLine(pen, new Point(50, 50), new Point(200, 200)); image.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } } 
    • Description: This code loads an image, creates a Graphics object, and uses a Pen to draw a red line from point (50, 50) to point (200, 200) on the image.
  2. "C# draw line with different colors on image"

    • Code:
      using System.Drawing; class Program { static void Main() { Bitmap image = new Bitmap("input.jpg"); Graphics graphics = Graphics.FromImage(image); // Draw lines with different colors on the image DrawColoredLine(graphics, Color.Red, new Point(50, 50), new Point(200, 200)); DrawColoredLine(graphics, Color.Blue, new Point(100, 100), new Point(250, 250)); image.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } static void DrawColoredLine(Graphics graphics, Color color, Point start, Point end) { using (Pen pen = new Pen(color, 2)) { graphics.DrawLine(pen, start, end); } } } 
    • Description: This code introduces a method (DrawColoredLine) to draw lines with different colors on the image.
  3. "C# draw dashed line on image"

    • Code:
      using System.Drawing; using System.Drawing.Drawing2D; class Program { static void Main() { Bitmap image = new Bitmap("input.jpg"); Graphics graphics = Graphics.FromImage(image); // Draw a dashed line on the image DrawDashedLine(graphics, Color.Green, new Point(50, 50), new Point(200, 200)); image.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } static void DrawDashedLine(Graphics graphics, Color color, Point start, Point end) { using (Pen pen = new Pen(color, 2)) { pen.DashStyle = DashStyle.Dash; graphics.DrawLine(pen, start, end); } } } 
    • Description: This code uses the DashStyle property of the Pen to draw a dashed line on the image.
  4. "C# draw line with variable thickness on image"

    • Code:
      using System.Drawing; class Program { static void Main() { Bitmap image = new Bitmap("input.jpg"); Graphics graphics = Graphics.FromImage(image); // Draw lines with variable thickness on the image DrawVariableThicknessLine(graphics, Color.Orange, new Point(50, 50), new Point(200, 200), 2); DrawVariableThicknessLine(graphics, Color.Purple, new Point(100, 100), new Point(250, 250), 4); image.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } static void DrawVariableThicknessLine(Graphics graphics, Color color, Point start, Point end, int thickness) { using (Pen pen = new Pen(color, thickness)) { graphics.DrawLine(pen, start, end); } } } 
    • Description: This code introduces a method (DrawVariableThicknessLine) to draw lines with variable thickness on the image.
  5. "Draw arrow on image in C#"

    • Code:
      using System.Drawing; using System.Drawing.Drawing2D; class Program { static void Main() { Bitmap image = new Bitmap("input.jpg"); Graphics graphics = Graphics.FromImage(image); // Draw an arrow on the image DrawArrow(graphics, Color.Brown, new Point(50, 50), new Point(200, 200)); image.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } static void DrawArrow(Graphics graphics, Color color, Point start, Point end) { using (Pen pen = new Pen(color, 2)) { pen.EndCap = LineCap.ArrowAnchor; graphics.DrawLine(pen, start, end); } } } 
    • Description: This code uses the LineCap property of the Pen to draw an arrow on the image.
  6. "C# draw line with transparency on image"

    • Code:
      using System.Drawing; class Program { static void Main() { Bitmap image = new Bitmap("input.jpg"); Graphics graphics = Graphics.FromImage(image); // Draw a transparent line on the image DrawTransparentLine(graphics, Color.FromArgb(128, 255, 0, 0), new Point(50, 50), new Point(200, 200)); image.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } static void DrawTransparentLine(Graphics graphics, Color color, Point start, Point end) { using (Pen pen = new Pen(color, 2)) { graphics.DrawLine(pen, start, end); } } } 
    • Description: This code uses Color.FromArgb to create a transparent color and draws a line with transparency on the image.
  7. "C# draw line with arrowheads on image"

    • Code:
      using System.Drawing; using System.Drawing.Drawing2D; class Program { static void Main() { Bitmap image = new Bitmap("input.jpg"); Graphics graphics = Graphics.FromImage(image); // Draw a line with arrowheads on the image DrawLineWithArrowheads(graphics, Color.DarkBlue, new Point(50, 50), new Point(200, 200), 10); image.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } static void DrawLineWithArrowheads(Graphics graphics, Color color, Point start, Point end, int arrowSize) { using (Pen pen = new Pen(color, 2)) { pen.CustomEndCap = new AdjustableArrowCap(arrowSize, arrowSize); graphics.DrawLine(pen, start, end); } } } 

More Tags

hp-uft currency qmake hortonworks-data-platform laravel-validation iconbutton gmt kubectl manifest spring-boot-2

More C# Questions

More Chemical reactions Calculators

More Chemical thermodynamics Calculators

More Mixtures and solutions Calculators

More Mortgage and Real Estate Calculators