Image Size

  • Thread starter Thread starter Jim via DotNetMonster.com
  • Start date Start date
J

Jim via DotNetMonster.com

Hi,

I have the location of an image in the database and I'm displaying it with
a repeater control. I'd like to add the width and height and I'm not sure
what the best way to do it is. I could add the fields to the database and
lookup the size and enter it. I know with .Net there's a way to find out
the size but I'm not sure how to go about doing it. This is the repeater
control:

<ASP:Repeater id="RepeaterMediaName" runat="server" DataSource="GetImage()">
<ItemTemplate>
<img src='images/<%# DataBinder.Eval (Container.DataItem, "MediaName") %>'
border='0' />
</ItemTemplate>
</ASP:Repeater>

Thanks
 
Jim:
You can dynamically get the size of an image via code like:

Bitmap b = new Bitmap(@"c:\karl\test.jpg");
Response.Write(b.Width);
Response.Write(b.Height);
b.Dispose()

Where I to do this, I'd create a custom server control that looked something
like this:

public class MediaImage : HtmlImage
{
private string _mediaImage;

public string mediaImage
{
get { return _mediaImage; }
set { _mediaImage = value; }
}

protected override void Render(HtmlTextWriter writer)
{
base.Src = "images/" + mediaImage;
string filePath = Page.Server.MapPath(base.Src);
Bitmap b = null;
try
{
b = new Bitmap(filePath);
base.Width = b.Width;
base.Height = b.Height;
}
finally
{
if (b != null)
{
b.Dispose();
}
}
base.Render (writer);
}

}


But to be honest, this is a lot of work...you are having to load the image
into memory and get the size. As far as I'm concerned it's (a) better to
store them in the database or (b) not render a width/height and let the
browser deal with it. I don't think I'd ever load an entire bitmap into
memory just to get the width/height...and if I had to specify it, I'd
certainly consider using the database first..

Karl
 
Karl,

Thank so much, that's what I was wondering whether it was better and faster
to store the sizes in the database or whether it was more efficient to
retrieve the size dynamically. I've always specified the image size as I
believe the page loads faster if you do. Thanks again.
 
If you can store it in the database, do it...the page does load faster if
you specify a width/height for an <img, but you have to ask yourself at what
cost? if you have 50 requests and all are having to do 5-15 width lookups,
you're page might render fast on the browser 'cuz you specified a
width/height, but the server might slow down...

Karl
 

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

Back
Top