I have a SQL 2000 table in which pictures are stored as an Image column. I want to display then onto a c# aspx webpage without storing them to disk.
What is the best way to read and display the pictures?
In classic ASP I used to do this:
Response.ContentType = "image/jpeg"
Response.BinaryWrite rs.fields("ThisImage")
but I can't get this to work in c#.Kinda, you should use a <img src="http://pics.10026.com/?src=viewImage.aspx?id=RecordID"> and then use some code like this below to read the database:
using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace DBImages
{
/// <summary>
/// Summary description for ViewImage.
/// </summary>
public class ViewImage : System.Web.UI.Page
{
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);}
#endregionprivate void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}public ViewImage()
{
Page.Init += new System.EventHandler(Page_Init);
}private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
//get the image id from the url
string ImageId =Request.QueryString["img"];
double adjustedSize=Convert.ToDouble(Request.QueryString["size"]);//build our query statement
string sqlText = "SELECT img_data, img_contenttype FROM Image WHERE img_pk = " + ImageId;SqlConnection connection = new SqlConnection(ConfigurationSettings.Appsettings["DSN"]);
SqlCommand command = new SqlCommand( sqlText, connection);//open the database and get a datareader
connection.Open();
SqlDataReader dr;
dr = command.ExecuteReader();if ( dr.HasRows) //yup we found our image
{
dr.Read();Response.ContentType = dr["img_contenttype"].ToString();
System.IO.Stream fs=new System.IO.MemoryStream((byte[])dr.GetSqlBinary(0) );
System.Drawing.Image _image=System.Drawing.Image.FromStream( new System.IO.MemoryStream((byte[])dr.GetSqlBinary(0)));int fileLength, fileWidth, fileHeight;
using(System.Drawing.Image image = System.Drawing.Image.FromStream(fs))
{
fileLength = Convert.ToInt32(fs.Length);
fileWidth = image.Width;
fileHeight = image.Height;
}
double multiplier;if (fileHeight>fileWidth) // picture must be portrait
{
multiplier=Convert.ToDouble(adjustedSize / fileHeight);
}
else
{
multiplier=Convert.ToDouble(adjustedSize / fileWidth);
}
int newWidth, newHeight;newWidth=(int)(fileWidth * multiplier);
newHeight=(int)(fileHeight * multiplier);System.Drawing.Image _newimage=_image.GetThumbnailImage(newWidth,newHeight,null,new System.IntPtr());
_newimage.Save(System.Web.HttpContext.Current.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.BinaryWrite( (byte[]) dr["img_data"] );}
connection.Close();
}
}
}
}
I do not comment code. If it was hard to write, it should be hard to read. :) I answer some questions tho.|||Thanks for that
Response.BinaryWrite( (byte[]) dr["img_data"] );does what I want, and I really appreciate the master class on image manipulation.
No comments:
Post a Comment