dynamic thumbnails

Discussion in 'ASP .Net' started by Chip, Mar 1, 2005.

  1. Chip

    Chip Guest

    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.
     
    Chip, Mar 1, 2005
    #1
    1. Advertising

  2. 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;}


    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net



    "Chip" <> wrote in message
    news:...
    >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.
    >
     
    Steve C. Orr [MVP, MCSD], Mar 1, 2005
    #2
    1. Advertising

  3. Chip

    Chip Guest

    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.


    "Chip" <> wrote in message
    news:...
    >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.
    >
     
    Chip, Mar 1, 2005
    #3
  4. 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.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net



    "Chip" <> wrote in message
    news:...
    > 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.
    >
    >
    > "Chip" <> wrote in message
    > news:...
    >>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.
    >>

    >
    >
     
    Steve C. Orr [MVP, MCSD], Mar 1, 2005
    #4
  5. Chip

    Julien Fabre Guest

    "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



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Julien Fabre, Mar 20, 2005
    #5
  6. It's easy to do in .NET.
    It's very hard to do in ASP.Old.
    I suggest you upgrade.

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://SteveOrr.net


    "Julien Fabre" <> wrote in message
    news:%23lb%...
    > "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
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Steve C. Orr [MVP, MCSD], Mar 20, 2005
    #6
  7. Chip

    Guest

    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.
     
    , Mar 20, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franco Gustavo

    OpenDialog and thumbnails

    Franco Gustavo, Aug 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,095
    Franco Gustavo
    Aug 27, 2003
  2. Kenneth Keeley

    Displaying PDF Thumbnails

    Kenneth Keeley, Jul 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    438
    Kenneth Keeley
    Jul 3, 2003
  3. Espen Evje
    Replies:
    1
    Views:
    393
    Espen Evje
    Jul 11, 2003
  4. Rick Spiewak

    Re: Create thumbnails on the fly

    Rick Spiewak, Jul 24, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    492
    David W. Simmonds
    Jul 25, 2003
  5. Chris D

    ASP.Net Thumbnails

    Chris D, Nov 17, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    3,218
Loading...

Share This Page