Just Code‎ > ‎

ASP.Net - Simple - Generate image dynamically in codebehind

posted Dec 7, 2009, 5:13 AM by Peter Henell   [ updated May 5, 2010, 4:31 AM ]
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using Henell.Web.UI.Objects;
using System.Collections.Generic;

namespace Henell.Web.UI
{ public partial class _Default : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e) { Bitmap bitmap = new Bitmap(1024, 768); Graphics surface = Graphics.FromImage(bitmap); surface.FillRectangle(Brushes.White, 0, 0, 1024, 768); surface.TextRenderingHint = TextRenderingHint.AntiAlias; //Rectangle rec = new Rectangle(20, 20, 20, 20); List<BaseSystem> systems = new List<BaseSystem>(); systems.Add(new Machine()); systems.Add(new Machine()); systems.Add(new Machine()); systems.Add(new Machine()); systems.Add(new Machine()); systems.Add(new Machine()); systems.Add(new Machine()); systems.Add(new Machine()); foreach (BaseSystem item in systems) { item.Draw(surface); } Response.ContentType = "image/jpeg"; //oBitmap.Save(Server.MapPath("gen_img.jpg"), ImageFormat.Jpeg); bitmap.Save(Response.OutputStream, ImageFormat.Jpeg); } } public abstract class BaseSystem
{ protected Rectangle rect; protected Pen pen; public BaseSystem(Rectangle rectangle) { rect = rectangle; pen = new Pen(Brushes.Black, 1); } public void Draw(Graphics surface) { surface.DrawRectangle(pen, rect); } } public class Machine : BaseSystem
{ public Machine() : base(RectangleFactory.Create()) { } } }
Comments