Graphics in C#
We will learn some graphics processing using the C# Express. We will be working with Graphics Files. In this tutorial we will see, how to create a new png file of a given size and then write a line from one given coordinate to another coordinate. So here is the program.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { using (Bitmap b = new Bitmap(640, 480)) { using (Graphics g = Graphics.FromImage(b)) { g.Clear(Color.White); g.DrawLine(Pens.Red, 10, 10, 100, 100); } b.Save(@"C:\tutorials\blank_640_480.png"); } } } } |
Some Explanation
Bitmap b= Bitmap(640, 480)Initializes a new instance of the Bitmap class with the 640x480 and assigns it to variable b.
Graphics.FromImage Method Creates a new Graphics from the specified Image b.
g.Clear(Color.White);
Creates a white background.
g.DrawLine(Pens.Red, 10, 10, 100, 100);
Draws a line in Red color from Coordinate (10,10) to (100,100)
and Finally
b.Save(@"C:\tutorials\blank_640_480.png");
saves the file image as blank_640_480.png in the directory C:\tutorials
Here is the you tube video to show it ( Watch it in full screen to be able to see it more clearly)