Here is some C# code that I use to do exactly that. I pull the image from an image field in a database and resize it memory, then put it out to the output stream.
public partial class ShowImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
SqlCommand photoCommand = new SqlCommand("SELECT Image FROM Photos WHERE PhotoID = @PhotoID", connection);
photoCommand.Parameters.AddWithValue("@PhotoID", Request.QueryString["PhotoID"]);
try {
connection.Open( );
// Get bytes return from DB Command
byte[] b = (byte[])photoCommand.ExecuteScalar();
if(b.Length > 0)
{
// Open a stream for the image and write the bytes into it
System.IO.MemoryStream stream = new System.IO.MemoryStream(b, true);
stream.Write(b, 0, b.Length);
// Create a bitmap from the stream
Bitmap bmp = new Bitmap(stream);
Size new_size = new Size( );
//resize based on the longer dimension
if (bmp.Width > bmp.Height) {
new_size.Width = outputSize;
new_size.Height = (int)(((double)outputSize / (double)bmp.Width) * (double)bmp.Height);
}
else {
new_size.Width = (int)(((double)outputSize / (double)bmp.Height) * (double)bmp.Width);
new_size.Height = outputSize;
}
Bitmap bitmap = new Bitmap(new_size.Width, new_size.Height, bmp.PixelFormat);
Graphics new_g = Graphics.FromImage(bitmap);
new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
new_g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
new_g.DrawImage(bmp, -1, -1, bitmap.Width + 1, bitmap.Height + 1);
bmp.Dispose( );
//Draw the bitmapt to the outputstream
bitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose( );
new_g.Dispose( );
// Close the stream
stream.Close( );
}
connection.Close( );
}
catch (Exception exc) {
connection.Close( );
}
}
}
GrayMatterSoftCheck out our new free rich date picker control for ASP.NET 2.0