Create another aspx page called TimeImage.aspx , and then enter this code in the Page_Load code for the page:
void Page_Load(object sender, EventArgs args) { string mime = "image/jpeg"; Response.ContentType = mime; Bitmap b = GetImageForTime(DateTime.Now); var codec = ImageCodecInfo.GetImageEncoders().Where(i => i.MimeType == mime).SingleOrDefault(); if(codec != null) b.Save(Response.OutputStream, codec, null); }
Then create your html generation as follows:
protected void myLiteral_Load(object sender, EventArgs e) { string html = "<table><tr><td>"; html += "<img src='TimeImage.aspx'>" html += "</td><td>"; html += GetWeatherHtmlText(); html += "</td></tr></table>"; myLiteral.Text = html; }
This will create an img tag that calls the TimeImage.aspx page, this page changes the mime response type to image/jpeg , converts the bitmap image to JPG and then saves it to the response output stream so that the image tag can display it as JPG.
If you prefer a different format, just change the mime type at the top (for example, "image / gif" for GIF or "image / png" for PNG).
source share