The following code sample shows how to Draw SerpinskiTriangle in Asp.net
import namespaces
using System.Drawing; using System.Drawing.Imaging;
Add This Code In PageLoad
protected void Page_Load(object sender, EventArgs e) { int width = 200; int height = 200; int iterations = 10000; //create instance for bitmap class Bitmap bitmap = new Bitmap(width, height); // Create our triangle's three Points Point top = new Point(width / 2, 0),bottomLeft = new Point(0, height), bottomRight = new Point(width, height); // Now, choose our starting point Point current = new Point(width / 2, height / 2); // Iterate iterations times Random rnd = new Random(); for (int iLoop = 0; iLoop < iterations; iLoop++) { // draw the pixel bitmap.SetPixel(current.X, current.Y, Color.Red);
// Choose our next pixel switch (rnd.Next(3)) { case 0: current.X -= (current.X - top.X) / 2; current.Y -= (current.Y - top.Y) / 2; break;
case 1: current.X -= (current.X - bottomLeft.X) / 2; current.Y -= (current.Y - bottomLeft.Y) / 2; break;
case 2: current.X -= (current.X - bottomRight.X) / 2; current.Y -= (current.Y - bottomRight.Y) / 2; break; } } bitmap.Save(@"D:\img", ImageFormat.Gif);
// Save the image to the OutputStream Response.ContentType = "image/jpeg"; bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
// clean up... bitmap.Dispose();
}
|
No responses found. Be the first to respond and make money from revenue sharing program.
|