Hi all, and Kevin in particular,
here's a source for the HTTP handler that picks
the image files from a database (this case *.png) and
handles it over to an image on a Webform with
Image.ImageURL="...ashx" (although you could rightaway
also set the src-attribute of an <img/>-element.
Figure around: the pics wont show up on the WebForm.
What's the cause? - The ViewState?
Thanks for response
Detlef
--
<%@ WebHandler Language="C#" Class="PicViewer" %>
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.ComponentModel;
public class PicViewer : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
System.Data.SqlClient.SqlConnection
sqlConnInvoice;
Graphics g;
Bitmap bmp;
MemoryStream bmpStream;
Image _img;
MemoryStream memStream;
byte[] _picBytes;
//Get PicBytes, in this case Png Data,
where PngPic is a
//SQL-DataField of SQL format Image
conn = new
System.Data.SqlClient.SqlConnection();
conn.ConnectionString = "...yerkyerk...";
sqlConnInvoice.Open();
System.Data.SqlClient.SqlCommand
_cmdLoadPicture =
new
System.Data.SqlClient.SqlCommand("select PngPic from
PngPictures where PicID=52531;",conn);
SqlDataReader drReader =
_cmdLoadPicture.ExecuteReader();
drReader.Read();
_picBytes=(byte[])drReader.GetSqlBinary(0);
//Push data to Memory Stream
memStream = new MemoryStream();
memStream.Write
(_picBytes,0,_picBytes.Length);
//Commented to follow a more general route:
//Make a Bitmap, print image into it and
save
//it into another standard format, here
*.gif,
//but could be any other
/*
//Construct image to push into response
stream
_img=Image.FromStream(memStream,true);
*/
//Read image size
float _height = Image.FromStream
(memStream).PhysicalDimension.Height;
float _width = Image.FromStream
(memStream).PhysicalDimension.Width;
//new Bitmap - adapt size if necessary:
bmp = new Bitmap(Image.FromStream
(memStream), new Size((int)_width, (int)_height));
g = Graphics.FromImage (bmp);
//Draw on Bitmap
g.DrawImage(_img,0,0);
memStream.Close();
conn.Close();
/*
context.Response.ContentType = "image/gif";
//Save _img into Reponse stream - try
other format
_img.Save
(context.Response.OutputStream,ImageFormat.Gif);
*/
//Save bmp into Reponse stream
context.Response.ContentType="image/gif";
bmp.Save(context.Response.OutputStream,
System.Drawing.Imaging.ImageFormat.Gif);
bmp.Dispose();
g.Dispose();
}
public bool IsReusable
{
get { return true; }
}
}
//----------