GetThumbnailImage

A

Andrew Banks

Is it possible to use GetThumbnailImage with images being pulled from an SQL
DB and if so how? All examples I've seen refer to working with an actual
image stored on the server.

My code for pulling the image from the DB is below if it makes a difference.

Thanks in advance

private void Page_Load(object sender, System.EventArgs e)
{
string imageID = Request.QueryString["ID"];
SqlDataReader imageContent = GetImages(imageID);
imageContent.Read();
Response.ContentType = imageContent["ContentType"].ToString();

Response.OutputStream.Write((byte[])imageContent["CoverShot"],0,System.Conve
rt.ToInt32(imageContent["ContentLength"]));
Response.End();
}
private SqlDataReader GetImages(string imageID)
{
SqlConnection con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("Co
nRead"));
SqlCommand cmd = new SqlCommand("SELECT CoverShot, ContentType,
ContentLength FROM Products WHERE ProductID = '" + imageID + "'",con);
con.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
 
L

Luke Venediger

Hi there,

Your image will be stored in the database as type Image. You can do the
following to pull it out, assuming you want to get the thumbnail for a
particular image:

string sql = "SELECT thumbnail FROM tb_Products WHERE ProductID = " +
ProductID;
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.Text;
command.CommandText = sql;
command.Connection = yourConnectionObject;

// Get the raw image data
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable resultDt = new DataTable();
adapter.Fill(resultDt);
command.Close();
byte[] imageData = (byte[])resultDt.Rows[0]["thumbnail"];

// Send it to the caller
MemoryStream memStream = new MemoryStream(imageData);
memStream.WriteTo(Response.OutputStream);

Cheers,

Luke Venediger
http://blogdotnet.blogspot.com
 
A

Andrew Banks

Thanks TJS.

Only problem is I already have the image in the DB and want to dynamically
resize it onthe way out and not on the way in.

Any ideas how I would do this please?

TJS said:
http://www.dotnetjunkies.com/HowTo/C4A2ACC3-C6B4-4E4C-802A-85B8DBEC7F25.dcik


Andrew Banks said:
Is it possible to use GetThumbnailImage with images being pulled from an SQL
DB and if so how? All examples I've seen refer to working with an actual
image stored on the server.

My code for pulling the image from the DB is below if it makes a difference.

Thanks in advance

private void Page_Load(object sender, System.EventArgs e)
{
string imageID = Request.QueryString["ID"];
SqlDataReader imageContent = GetImages(imageID);
imageContent.Read();
Response.ContentType = imageContent["ContentType"].ToString();
Response.OutputStream.Write((byte[])imageContent["CoverShot"],0,System.Conve
rt.ToInt32(imageContent["ContentLength"]));
Response.End();
}
private SqlDataReader GetImages(string imageID)
{
SqlConnection con = new
SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("Co
nRead"));
SqlCommand cmd = new SqlCommand("SELECT CoverShot, ContentType,
ContentLength FROM Products WHERE ProductID = '" + imageID + "'",con);
con.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
 
D

Daniel Pratt

Hi Andrew,

Andrew Banks said:
Is it possible to use GetThumbnailImage with images being pulled from an SQL
DB and if so how? All examples I've seen refer to working with an actual
image stored on the server.

My code for pulling the image from the DB is below if it makes a difference.

Thanks in advance

This is just "air" code, but it should give you the idea:

...
private void Page_Load(object sender, System.EventArgs e)
{
string imageID = Request.QueryString["ID"];
SqlDataReader imageContent = GetImages(imageID);
imageContent.Read();
Response.ContentType = imageContent["ContentType"].ToString();

MemoryStream imageData = new
MemoryStream((byte[])imageContent["CoverShot"]);

using(Image fullImage = new Bitmap(imageData))
{
using(Image thumbnailImage =
fullImage.GetThumbnailImage(...))
{
thumbnailImage.Save(Response.OutputStream, ...);
}
}

Response.End();
}
...

Regards,
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top