Writing code: experimentation is the best part

random_by_jmummeyI recently wrote a desktop application, a small piece required that I draw a simple rectangle to the screen. When I drew the rectangle I made an error, I put the x-coordinate in as the width and the width as the x-coordinate. The result was pretty but not what I wanted so I fixed it and moved on. But I came back to it later that night and started fiddling with the code to generate more unexpected graphics. In the end I had written a simple method in the C# language where I feed it random x and y coordinates as well as random height and width sizes and random shapes to draw on the screen. I liked the images so much (only because some remind me of Star Trek) that I picked out what I consider to be the coolest 60 images and published them in my first artistic coloring book called “Random”. The book size is 8 ½ x 11 and each image is on the right with a blank page on the back so the images can easily be colored, removed and framed without disrupting the other images. Check it out and color all of your boring processes away: https://www.amazon.com/Random-Coloring-computer-generated-coordinates/dp/1539987582

If you are interested in learning how to write code, the instructions below will help you get started, these are the steps I went through to create some of the artwork in the book Random that I mention above, enjoy and code on:

Language used:  C#
Type of application written:  console application running on my Windows desktop PC (no server needed)
Developer tools used:  Visual Studio, running on my Windows desktop PC (this is available to you for free): https://www.visualstudio.com/free-developer-offers/

Steps performed for drawing ellipses:

  1. Install Visual Studio from the link provided above
  2. Reboot
  3. Open Visual Studio
  4. Select File > New > Project > Templates > Visual C# > Windows > Classic Desktop > Console Application
  5. Name the application and choose a location to store it > select OK
  6. The “Program.cs” file should have opened by default for you. It will look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)//main method begins
{

}//main method ends

}

}

  1. Look in the Solution Explorer (View > Solution Explorer) and add a reference to a library for drawing:
  2. Click once on “References”, right click and select “Add Reference” and find “System.Drawing”, place a checkmark beside “System.Drawing” and select OK
  3. Towards the top of “Program.cs” add this: usingDrawing; and also add using System.Drawing.Imaging;
  4. Inside the Main method, add the following for loop (the purpose of this for loop is make many images. If you set this for loop to run 40 times then that means 40 images will be created and stored in the Temp directory on your c drive.  If you set this for loop to run 10 times then only 10 images will be created and stored in the temp directory on your c drive:

for (int t = 0; t < 40; t++)
{

makeSweetImages3();   //call method repeatedly to create multiple images

}

 

  1. Under the Main method, add the following method, this method does all of the work (declares random coordinates, draws the image and stores the image):

static private void makeSweetImages3()
{

DateTime localDate = DateTime.Now; //we use the milliseconds to uniquely name each image
Random rnd = new Random();
Bitmap bitmap = new Bitmap(2552, 3302);//width, height
bitmap.SetResolution(300, 300);

// Draw graphic
int z = rnd.Next(1, 2550);//to represent width of item being drawn keep btw these values
int b = rnd.Next(1, 3300);//to represent height of item being drawn keep btw these values
int k = rnd.Next(0, 2550 – z);//to represent item on the x axis keep between these values
int m = rnd.Next(0, 3300 – b);//to represent item on the y axis

using (Graphics gr = Graphics.FromImage(bitmap))
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//type of line

for (int p = 0; p < 10; p++)//number of ellipses being drawn for this one image
{
System.Drawing.Rectangle rect5 = new System.Drawing.Rectangle(k, m, z, b);
// x, y, width, height, stage is set for graphic, when you draw an ellipse you
//have to define its outer rectangle first then draw the ellipse within or on
//said rectangle
using (System.Drawing.Pen thick_pen = new System.Drawing.Pen(System.Drawing.Color.Black, 2))
{
gr.DrawEllipse(thick_pen, rect5);//draw graphic
}
z = rnd.Next(1, 2550);//width
b = rnd.Next(1, 3300);//height
k = rnd.Next(0, 2550 – z);//x
m = rnd.Next(0, 3300 – b);//y

}
}

bitmap.Save(@”C:\Temp\” + localDate.ToString(“yyyyMMdd-HHmmssfff”) + “.png”, ImageFormat.Png);
//save image to Temp directory on c drive
}

  1. Run the console application by selecting Debug > Start without Debugging (you will notice a black console window will appear, once the console window displays “Press any key to continue…” you will know all drawings are complete, press any key to close the console window)
  2. Navigate to C:\Temp on your computer and view the beautiful images you just created

Blog at WordPress.com.

Up ↑