Displaying Dynamically Generated Image

Here is a simplified way to have a dynamically generated image be the ImageUrl property of an ImageControl.

Here is a simplified way to have a dynamically generated image be the ImageUrl property of an ImageControl.


Here is a simplified way to have a dynamically generated image be
the ImageUrl property of an ImageControl.

By simply setting the ImageUrl property to the actual page that generates the image, we can even pass in querystring parameters that control how the image is generated. The image of course, could also be coming from a database as a byte array:


// ImageGen.aspx (codebehind)
public class ImageGen : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
try
{
decimal[] myVals={121.2M,122.3M,200.1M,125.5M};
Bitmap newBitmap = Draw(System.Drawing.Color.Azure,500,500,myVals);
newBitmap.Save(Server.MapPath("test.jpg"),System.Drawing.Imaging.ImageFormat.Jpeg);
Response.WriteFile(Server.MapPath("test.jpg"));
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message + " " + ex.StackTrace);
}
}


public Bitmap Draw(Color bgColor, int width, int height,
decimal[] vals)
{
// Create a new image and erase the background
Bitmap bitmap = new Bitmap(width,height,
PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(bitmap);
SolidBrush brush = new SolidBrush(bgColor);
graphics.FillRectangle(brush, 0, 0, width, height);
brush.Dispose();
// Create brushes for coloring the pie chart
SolidBrush[] brushes = new SolidBrush[10];
brushes[0] = new SolidBrush(Color.Yellow);
brushes[1] = new SolidBrush(Color.Green);
brushes[2] = new SolidBrush(Color.Blue);
brushes[3] = new SolidBrush(Color.Cyan);
brushes[4] = new SolidBrush(Color.Magenta);
brushes[5] = new SolidBrush(Color.Red);
brushes[6] = new SolidBrush(Color.Black);
brushes[7] = new SolidBrush(Color.Gray);
brushes[8] = new SolidBrush(Color.Maroon);
brushes[9] = new SolidBrush(Color.LightBlue);
// Sum the inputs to get the total
decimal total = 0.0m;
foreach( decimal val in vals )
total += val;
// Draw the pie chart
float start = 0.0f;
float end = 0.0f;
decimal current = 0.0m;
for( int i = 0; i < vals.Length; i++ )
{
current += vals[i];
start = end;
end = (float) (current / total) * 360.0f;
graphics.FillPie(brushes[i % 10], 0.0f, 0.0f, width,
height, start, end - start);
}
// Clean up the brush resources
foreach( SolidBrush cleanBrush in brushes )
cleanBrush.Dispose();
return bitmap;
}


The following snippet from the aspx portion of a WebForm illustrates how the ImageUrl property is set the the PAGE that generates the image:
//WebForm1.aspx imageControl ImageUrl="ImageGen.aspx"

<form id="Form1" method="post" runat="server">
<asp:Image id=Image1 style="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 112px" runat="server" ImageUrl="ImageGen.aspx"></asp:Image>

</form>

Finally, here is how we would write the image directly to the output stream, FROM MEMORY, without saving it first:


System.IO.MemoryStream m = new System.IO.MemoryStream();
newBitmap.Save(m,System.Drawing.Imaging.ImageFormat.Jpeg );
byte[] blob =new byte[m.Length];
m.Read (blob,0,(int)m.Length);
Response.OutputStream.Write( m.ToArray() , 0,(int) m.Length );

By Peter Bromberg   Popularity  (603 Views)