Home > Uncategorized > Drawing a line on a png image in Visual C#

Drawing a line on a png image in Visual C#

December 13th, 2012

We are trying to write a small code that will draw a line on a 640 x 480 blank image. It will open an existing png file and and then draw a line on it. The program will be compiled and run using Microsoft's Visual C# Express edition.

Some practical application - If you have a bunch of Files ( jpg or png), you can use this program to draw a border around it. You can use this idea to write a text ( as signature ) on the right side of the images.

Here is the code to do it.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;

namespace ConsoleApplication1
class Program
static void Main(string[] args)

var img = Bitmap.FromFile(@"C:\tutorials\img.png");
using(var g = Graphics.FromImage(img))
g.DrawLine(Pens.Red, new Point(10, 10), new Point(100, 100));

If you see the error that

type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?)

You will need to dd a "reference" to the System.Drawing.dll assembly. Use the Project, Add Reference... menu command to add a reference to this DLL.

When you make a Console Application, by default Visual Studio does not expect you to be doing graphics so it does not add a "reference" to the System.Drawing.dll assembly.

Once the code runs you should see a file called img2.png in the directory C:/tutorial. You can see other C# tutorials at here .


  1. No comments yet.
  1. No trackbacks yet.