dynamic thumbnails

C

Chip

I allow users to upload their own images, but I need to display them in a
small space. I can use the src tags to limit the size, but this doesn't keep
the aspect ratio. How can I display any image using a set WIDTH and have the
HEIGHT resize to fit the aspect ration?

Failing any java or cool html that will do this... Is there some .NET code
with which I can read the dimensions of the uploaded image to store them? I
could use this info to calculate my own SRC tags.
 
S

Steve C. Orr [MVP, MCSD]

You can resize the image using the GetThumbnailImage method.
Here's more info:
http://msdn.microsoft.com/library/d...emDrawingImageClassGetThumbnailImageTopic.asp

Or maybe you'll find these custom functions useful that I wrote:

public Image DisplaySize(Bitmap bmp)
{
Response.Write(bmp.Width.ToString());
Response.Write(bmp.Height.ToString());
}

//shrink the image proportionately so that neither height nor width is more
than [NewSize] pixels

public System.Drawing.Image ShrinkImage(System.Drawing.Bitmap bmp, int
NewSize)

{

double NewWidth;

double NewHeight;

double ShrinkPercent;

System.Drawing.Image.GetThumbnailImageAbort myCallback =

new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

if (bmp.Width>bmp.Height)

{

NewWidth=NewSize;

ShrinkPercent=(NewWidth/bmp.Width)*100;

NewHeight=(ShrinkPercent/100)*bmp.Height;

}

else

{

NewHeight=NewSize;

ShrinkPercent=(NewHeight/bmp.Height)*100;

NewWidth=(ShrinkPercent/100)*bmp.Width;

}

System.Drawing.Image myShrunkenImage =
bmp.GetThumbnailImage((int)NewWidth,(int)NewHeight,myCallback,IntPtr.Zero);

return myShrunkenImage;

}

public bool ThumbnailCallback(){return false;}
 
C

Chip

Thank-you for your help. I must have made this much harder than it really
was because I realized I had never actually experimented with the image
control. It automatically resizes the picture using only one parameter:
height or width, and keeps the aspect ratio. So, all I needed to do was to
set WIDITH in the image control to 150 and I'm done.
 
S

Steve C. Orr [MVP, MCSD]

You should realize your solution is not very efficient. The full (large)
image is being transferred and then the browser is resizing it. That's
somewhat wasteful, so a server side resizing routine (as I provided) would
be a bit more efficient.
But, as they say "If it isn't broken, then don't fix it". So if you aren't
concerned about the best possible efficiency then just call it done as is.
 
J

Julien Fabre

"so a server side resizing routine (as I provided) would
be a bit more efficient."


Hi!

I was wondering if you could show me how to do all that but in ASP, not
..NET please! :)

thanks in advance!

julien
 
S

Steve C. Orr [MVP, MCSD]

It's easy to do in .NET.
It's very hard to do in ASP.Old.
I suggest you upgrade.
 
S

someone

Here is a script that I use on my site to dynamically resize an image. It
resizes/resamples the image but doesn't apply any JPEG compression:

<%@ Page %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>



<script runat="server">

Function NewthumbSize(currentwidth, currentheight)


' Calculate the Size of the New image

dim tempMultiplier as Double
If currentwidth<Request.QueryString("width") Then
tempMultiplier=1
Else
tempMultiplier = Request.QueryString("width") / currentwidth
End If

dim NewSize as New Size(CInt(currentwidth * tempMultiplier),
CInt(currentheight * tempMultiplier))


return NewSize
End Function

Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


' set the mime type
Response.Clear()
Response.ContentType="image/jpeg"

dim g as System.Drawing.Image =
System.Drawing.Image.FromFile(server.mappath(request("src")))
dim thumbSize as New size
thumbSize=NewthumbSize(g.width,g.height)
Dim OutputBitmap as Bitmap = new
Bitmap(g,thumbSize.width,thumbSize.height)
Dim encoderParams as System.Drawing.Imaging.EncoderParameters = new
System.Drawing.Imaging.EncoderParameters()
Dim quality as long

Dim comp as integer = 100
quality = comp ' 0 to 100 - 100 highest quality
Dim encoderParam as System.Drawing.Imaging.EncoderParameter = new
System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,
quality)
encoderParams.Param(0) = encoderParam

Dim arrayICI as ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
Dim jpegICI as ImageCodecInfo
Dim x as integer
for x = 0 to arrayICI.Length-1

if (arrayICI(x).FormatDescription.Equals("JPEG")) then
jpegICI = arrayICI(x)
exit for
end if

next

if not jpegICI is nothing then
OutputBitmap.Save(Response.OutputStream, jpegICI, encoderParams)
end if


' clean up
OutputBitmap.Dispose()

end sub
</script>

To use it, I reference images as: <img src=image.aspx?src=[path to image,
relative to where image.aspx lives]&width=[width in pixels]>

You can modify it to be able to specify the compression:
Dim comp as integer = 0
if not (Request.QueryString("comp") = "") then comp =
Cint(Request.QueryString("comp"))
quality = comp ' 0 to 100 - 100 highest quality

All you need is a server that can process ASPX files. So, you're actually
processing it in .NET, but you can reference using .ASP.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top