473,320 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Displaying images in a repeater from database

Hi,

I am trying to display images in a repeater from a SQL database and do
some transformations on the image prior to displaying them (such as
thumbnail with a shadow). The problem is I can't seem to get the data
passed to the handler.

The following is the code that I have so far.

In the ViewPics.aspx file I have...

<asp:repeater id="rThumbnails" runat="server">
<itemtemplate>
<img src="Thumbnail.ashx?imgdata=<%#
DataBinder.Eval(Container.DataItem, "ImageData")) %>&w=150&shadow=true"
border="0" width="150">
</itemtemplate>
</asp:repeater>

....where ImageData is an image type in the SQL database
In the Thumbnail.ashx handler I have...

public class Thumbnail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Parse parameters
byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(context.Reques t["imgdata"]);
MemoryStream ms = new MemoryStream(bytes);
Image srcImage = Image.FromStream(ms);
Size size = new Size();
size.Width = Convert.ToInt32(context.Request["w"]);
size.Height = Convert.ToInt32(context.Request["h"]);
// Calculate missing size param maintaining the aspect ration
if (size.Width == 0)
size.Width = srcImage.Width * size.Height / srcImage.Height;
if (size.Height == 0)
size.Height = srcImage.Height * size.Width / srcImage.Width;
bool shadow = (context.Request["shadow"] == "true");

// Create bitmap 10 pixels larger then srcImage (for shadow) and draw
srcImage to it
Bitmap bitmap;
if (shadow)
bitmap = new Bitmap(size.Width+10, size.Height+10,
PixelFormat.Format24bppRgb);
else
bitmap = new Bitmap(size.Width, size.Height,
PixelFormat.Format24bppRgb);
bitmap.SetResolution(srcImage.HorizontalResolution ,
srcImage.VerticalResolution);
Graphics g = Graphics.FromImage(bitmap);
if (shadow)
{
// Create shadow effect
g.FillRectangle(new SolidBrush(Color.FromArgb(235, 233, 218)), 0, 0,
size.Width+10, size.Height+10);
g.FillRectangle(new SolidBrush(Color.DarkGray), 8, 8, size.Width+2,
size.Height+2);
g.FillRectangle(new SolidBrush(Color.Black), 0, 0, size.Width+6,
size.Height+6);
g.DrawImage(srcImage, 2, 2, size.Width+2, size.Height+2);
} else
g.DrawImage(srcImage, 0, 0, size.Width, size.Height);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

srcImage.Dispose();
g.Dispose();

// Send the Bitmap to the browser
context.Response.ContentType = "image/jpeg";
context.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + imageFilename);
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
context.Response.End();

// Cleanup
bitmap.Dispose();
}

public bool IsReusable
{
get { return true; }
}
}

You can ignore the bulk of the above image manipulation code. It works
ok.
The problem is in the first line in getting the image data passed to it
as shown below...

byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(context.Reques t["imgdata"]);

Instead of passing the string of data from the image filed in the sql
record it returns the string "System.Byte[]"
ie. the data type instead of the data.

I suspect the problem is with the Databinder class but I am lost as to
how else to do this.

Any help would be greatly appreciated.
Thanks

Nov 17 '05 #1
2 5205
Instead of storing the ImageData directly in the ViewPics.aspx as a bunch of
bytes in the querystring of Thumbnail.ashx, you should store its ID only and
use this ID to retrieve the ImageData from the database in the Thumbnai.ashx
control.

By using your previous method, the ImageData will travel three times over
the wire instead of just one time: first, as a bunch of bytes written as a
parameter to Thumbnail.axhx?... , then in a GET trip back to the server to
the Thumbnail.ashx control and finally as the image sent back by
Thumbnail.axhx as the source of the HTML image control.

Sending an image over the wire take time; sending it three times will not do
any good.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
<ma****@thos.ca> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi,

I am trying to display images in a repeater from a SQL database and do
some transformations on the image prior to displaying them (such as
thumbnail with a shadow). The problem is I can't seem to get the data
passed to the handler.

The following is the code that I have so far.

In the ViewPics.aspx file I have...

<asp:repeater id="rThumbnails" runat="server">
<itemtemplate>
<img src="Thumbnail.ashx?imgdata=<%#
DataBinder.Eval(Container.DataItem, "ImageData")) %>&w=150&shadow=true"
border="0" width="150">
</itemtemplate>
</asp:repeater>

...where ImageData is an image type in the SQL database
In the Thumbnail.ashx handler I have...

public class Thumbnail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Parse parameters
byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(context.Reques t["imgdata"]);
MemoryStream ms = new MemoryStream(bytes);
Image srcImage = Image.FromStream(ms);
Size size = new Size();
size.Width = Convert.ToInt32(context.Request["w"]);
size.Height = Convert.ToInt32(context.Request["h"]);
// Calculate missing size param maintaining the aspect ration
if (size.Width == 0)
size.Width = srcImage.Width * size.Height / srcImage.Height;
if (size.Height == 0)
size.Height = srcImage.Height * size.Width / srcImage.Width;
bool shadow = (context.Request["shadow"] == "true");

// Create bitmap 10 pixels larger then srcImage (for shadow) and draw
srcImage to it
Bitmap bitmap;
if (shadow)
bitmap = new Bitmap(size.Width+10, size.Height+10,
PixelFormat.Format24bppRgb);
else
bitmap = new Bitmap(size.Width, size.Height,
PixelFormat.Format24bppRgb);
bitmap.SetResolution(srcImage.HorizontalResolution ,
srcImage.VerticalResolution);
Graphics g = Graphics.FromImage(bitmap);
if (shadow)
{
// Create shadow effect
g.FillRectangle(new SolidBrush(Color.FromArgb(235, 233, 218)), 0, 0,
size.Width+10, size.Height+10);
g.FillRectangle(new SolidBrush(Color.DarkGray), 8, 8, size.Width+2,
size.Height+2);
g.FillRectangle(new SolidBrush(Color.Black), 0, 0, size.Width+6,
size.Height+6);
g.DrawImage(srcImage, 2, 2, size.Width+2, size.Height+2);
} else
g.DrawImage(srcImage, 0, 0, size.Width, size.Height);
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

srcImage.Dispose();
g.Dispose();

// Send the Bitmap to the browser
context.Response.ContentType = "image/jpeg";
context.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + imageFilename);
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
context.Response.End();

// Cleanup
bitmap.Dispose();
}

public bool IsReusable
{
get { return true; }
}
}

You can ignore the bulk of the above image manipulation code. It works
ok.
The problem is in the first line in getting the image data passed to it
as shown below...

byte[] bytes =
System.Text.Encoding.ASCII.GetBytes(context.Reques t["imgdata"]);

Instead of passing the string of data from the image filed in the sql
record it returns the string "System.Byte[]"
ie. the data type instead of the data.

I suspect the problem is with the Databinder class but I am lost as to
how else to do this.

Any help would be greatly appreciated.
Thanks

Nov 17 '05 #2
Thank you Sylvain for your reply. That is good advice.

Marv

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jon | last post by:
Hi, suspect there might not be a good answer to this one. Using vb.net I have a page like this some text datagrid repeater Now the repeater is populated from a simple query that takes no time...
7
by: Jim | last post by:
I am trying to display images that are stored in a database, and I am using a repeater control. What I still use the Response.BinaryWrite method with a binding expression, if so, what with the...
7
by: gemel | last post by:
I am developing an application that uses SQL 2000 as my source of images. I have successfully created the code to load the images onto the SQL Server and also to retrieve the images into a dataset....
2
by: marvin | last post by:
Hi, I am trying to display images in a repeater from a SQL database and do some transformations on the image prior to displaying them (such as thumbnail with a shadow). The problem is I can't...
5
by: Imran Aziz | last post by:
Hello all, I am populating the contents of a repeater control using a database query. In the repeater control I have a link , which I want to display or not based on the current logged in user....
5
by: Brad Baker | last post by:
I'm completely new to ASP.NET programming, so please accept my apologies in advance for asking what is probably an obvious question. :-) I'm trying to write a page which will display the...
0
by: Nobody | last post by:
I'm working on an e-commerce type site where I have a bunch of categorized items... once you drill down to the items page, I show the list of items with a "preview picture". When you click on the...
4
by: redpears007 | last post by:
Hi Again, Throwing this one out to you again as i am not getting anywhere and can find little to no information out there. I am currently displaying images (Jpegs) in access via the routine...
1
by: Jeff | last post by:
hi asp.net 2.0 On my webpage I want to display a list of images. Most proparly display the list horizontal Sometimes I want only one image to be displayed and sometimes maybe 10 images....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.