Scaling JPG files

  • Thread starter Thread starter daveh551
  • Start date Start date
D

daveh551

I have done a Google search on this, and the hits seem to indicate
that there's probably not a computationally easy way to do it, but
I'll ask anyway before I go off and re-invent the wheel.

I have user control do display a specified image file (.jpg) from a
database. Files may have arbitrary height, width, and aspect ratios.
I have height and width properties on the control to fit the image
display into the appropriate space on the page. But I would like to
do that while maintaining the aspect ratio of the original image,
rather than just leaving it up to the browser to scale to the
specified height and width. This would require me to adjust the
requested height and width property to maintain the aspect ratio of
the original file, which I have no problem with... except that I don't
have an easy way get the original height and width.

Is there an easy way to do this in ASP.NET? I don't mind writing code
in the code-behind file to do it, but is there a way to do it without
reading in the whole JPG file (and then reading it again when it's
loaded by the HTML)? It seems like this would be a fairly common
problem, so I'm sure there are some classes out there to do it, maybe
even within .NET itself, though the impression I got from the Google
hits was that the JPEG file format definition is such that you're
going to have read the whole file (or most of it) to extract the
information.

Is this computationally intensive enough to make it worth storing in a
database table and looking it up first, only reading the file to find
it if it's not in the DB table (and then adding it)?
 
daveh551 said:
I have done a Google search on this, and the hits seem to indicate
that there's probably not a computationally easy way to do it, but
I'll ask anyway before I go off and re-invent the wheel.

I have user control do display a specified image file (.jpg) from a
database. Files may have arbitrary height, width, and aspect ratios.
I have height and width properties on the control to fit the image
display into the appropriate space on the page. But I would like to
do that while maintaining the aspect ratio of the original image,
rather than just leaving it up to the browser to scale to the
specified height and width. This would require me to adjust the
requested height and width property to maintain the aspect ratio of
the original file, which I have no problem with... except that I don't
have an easy way get the original height and width.

Is there an easy way to do this in ASP.NET? I don't mind writing code
in the code-behind file to do it, but is there a way to do it without
reading in the whole JPG file (and then reading it again when it's
loaded by the HTML)? It seems like this would be a fairly common
problem, so I'm sure there are some classes out there to do it, maybe
even within .NET itself, though the impression I got from the Google
hits was that the JPEG file format definition is such that you're
going to have read the whole file (or most of it) to extract the
information.

Is this computationally intensive enough to make it worth storing in a
database table and looking it up first, only reading the file to find
it if it's not in the DB table (and then adding it)?

What you want to do is create a new aspx page. The whole purpose of
this page is to serve the images.

The following link will give you a start. In the link there is a
querystring used. That would be the key to your image in the database:

http://www.sitepoint.com/article/generating-asp-net-images-fly


Hope this helps
LS
 
I have done a Google search on this, and the hits seem to indicate
that there's probably not a computationally easy way to do it, but
I'll ask anyway before I go off and re-invent the wheel.

I have user control do display a specified image file (.jpg)  from a
database. Files may have arbitrary height, width, and aspect ratios.
I have height and width properties on the control to fit the image
display into the appropriate space on the page.  But I would like to
do that while maintaining the aspect ratio of the original image,
rather than just leaving it up to the browser to scale to the
specified height and width.  This would require me to adjust the
requested height and width property to maintain the aspect ratio of
the original file, which I have no problem with... except that I don't
have an easy way get the original height and width.

Is there an easy way to do this in ASP.NET?  I don't mind writing code
in the code-behind file to do it, but is there a way to do it without
reading in the whole JPG file (and then reading it again when it's
loaded by the HTML)?  It seems like this would be a fairly common
problem, so I'm sure there are some classes out there to do it, maybe
even within .NET itself, though the impression I got from the Google
hits was that the JPEG file format definition is such that  you're
going to have read the whole file (or most of it) to extract the
information.

Is this computationally intensive enough to make it worth storing in a
database table and looking it up first, only reading the file to find
it if it's not in the DB table (and then adding it)?

Try to read just some fist bytes and get the dimensions from header.
There's plenty of documentation about JPG format online. Here are some
examples on how to get the information from header:

http://quilt.ic.cz/tmp/devfus/ImageInfo.rar
http://sipostamas.spaces.live.com/blog/cns!F648CB161AEB2D04!157.entry
 
daveh551 said:
I have done a Google search on this, and the hits seem to indicate
that there's probably not a computationally easy way to do it, but
I'll ask anyway before I go off and re-invent the wheel.

I have user control do display a specified image file (.jpg) from a
database. Files may have arbitrary height, width, and aspect ratios.
I have height and width properties on the control to fit the image
display into the appropriate space on the page. But I would like to
do that while maintaining the aspect ratio of the original image,
rather than just leaving it up to the browser to scale to the
specified height and width. This would require me to adjust the
requested height and width property to maintain the aspect ratio of
the original file, which I have no problem with... except that I don't
have an easy way get the original height and width.

Is there an easy way to do this in ASP.NET? I don't mind writing code
in the code-behind file to do it, but is there a way to do it without
reading in the whole JPG file (and then reading it again when it's
loaded by the HTML)? It seems like this would be a fairly common
problem, so I'm sure there are some classes out there to do it, maybe
even within .NET itself,

There is nothing in the framework for this. You can easily get the
information, but not without also decompressing the image data.
though the impression I got from the Google
hits was that the JPEG file format definition is such that you're
going to have read the whole file (or most of it) to extract the
information.

You only need a small part of the file to get the image dimensions, but
as the JPEG format is flexible you don't know beforehand where in the
file that information is located. You may have to skip through several
sections of the file to get to the right one.
Is this computationally intensive enough to make it worth storing in a
database table and looking it up first, only reading the file to find
it if it's not in the DB table (and then adding it)?

Yes, I would definitely store the image dimensions in the database.

You should also consider creating the thumbnail images on the server
instead of letting the browser scale the image. This reduces the
bandwidth usage, and the image quality of the thumbnails gets much better.
 
There is nothing in the framework for this. You can easily get the
information, but not without also decompressing the image data.


You only need a small part of the file to get the image dimensions, but
as the JPEG format is flexible you don't know beforehand where in the
file that information is located. You may have to skip through several
sections of the file to get to the right one.


Yes, I would definitely store the image dimensions in the database.

You should also consider creating the thumbnail images on the server
instead of letting the browser scale the image. This reduces the
bandwidth usage, and the image quality of the thumbnails gets much better..

Thank you all for your responses. This will give me some valuable
guidance as I implement this.
 
Back
Top