Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 3rd, 2003, 08:13 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default How Can I Crop An Image in ASP

Hiya

I'm using some code that allows me to browse for an image on my hard drive, click upload and this then uploads my image to a specified folder on my website. This image name then gets placed into a field on an insert page which is then entered into a database.

The front end of my site calls this data from the database and displays the corresponding image for a particular record. However, I need to make sure that the image that's uploaded is the correct width.

Is it possible, before my image is uploaded to the specified folder, that this image can be cropped so that it's exactly the right size. I need to be able to specify the width, but the height can vary (as some images may be square, whilst others may be rectangular portrait).

Can this be done with a bit of ASP code?

Thanks in advance.

Lucy Robinson

  #2 (permalink)  
Old November 3rd, 2003, 11:33 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Do you mean that you want to resize to your specified size? I don't imagine you really mean to crop it. I have some code that does this very thing, let me dig it up and I'll post it.

Peter
----------------------------------------
Work smarter, not harder.
  #3 (permalink)  
Old November 3rd, 2003, 11:52 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Using the System.Drawing classes you can do all sorts of image processing. Here's a class file I wrote for an image browser application. I figured the whole thing would be useful for you.
Public Class ImageTools

    Private Enum ResizeType
        PixelFixed
        PixelMax
        Percent
    End Enum

    Private Enum ResizeOp
        Thumbnail
        Bitmap
    End Enum

    Public Class Thumbnail

        Public Shared Function ToMaxDimensions(ByVal image As System.Drawing.Image, ByVal maxHeight As Integer, ByVal maxWidth As Integer) As System.Drawing.Image
            Return DoResize(image, ResizeType.PixelMax, maxHeight, maxWidth, ResizeOp.Thumbnail)
        End Function

        Public Shared Function ToSize(ByVal image As System.Drawing.Image, ByVal height As Integer, ByVal width As Integer) As System.Drawing.Image
            Return DoResize(image, ResizeType.PixelFixed, height, width, ResizeOp.Thumbnail)
        End Function

        Public Shared Function ToPercent(ByVal image As System.Drawing.Image, ByVal percent As Byte) As System.Drawing.Image
            Return DoResize(image, ResizeType.Percent, percent, percent, ResizeOp.Thumbnail)
        End Function

    End Class

    Public Class Resize

        Public Shared Function ToMaxDimensions(ByVal image As System.Drawing.Image, ByVal maxHeight As Integer, ByVal maxWidth As Integer) As System.Drawing.Image
            Return DoResize(image, ResizeType.PixelMax, maxHeight, maxWidth, ResizeOp.Bitmap)
        End Function

        Public Shared Function ToSize(ByVal image As System.Drawing.Image, ByVal height As Integer, ByVal width As Integer) As System.Drawing.Image
            Return DoResize(image, ResizeType.PixelFixed, height, width, ResizeOp.Bitmap)
        End Function

        Public Shared Function ToPercent(ByVal image As System.Drawing.Image, ByVal percent As Byte) As System.Drawing.Image
            Return DoResize(image, ResizeType.Percent, percent, percent, ResizeOp.Bitmap)
        End Function

    End Class

    Private Shared Function DoResize(ByVal objImage As System.Drawing.Image, ByVal eType As ResizeType, ByVal nTargetHeight As Integer, ByVal nTargetWidth As Integer, ByVal eResizeOp As ResizeOp) As System.Drawing.Image
        Dim nCurrentHeight As Integer
        Dim nCurrentWidth As Integer
        Dim nNewHeight As Integer
        Dim nNewWidth As Integer
        Dim objAdjustedImage As System.Drawing.Image

        nCurrentHeight = objImage.Height
        nCurrentWidth = objImage.Width

        Select Case eType
            Case ResizeType.Percent
                nNewHeight = CType(nCurrentHeight * (nTargetHeight / 100), Integer)
                nNewWidth = CType(nCurrentWidth * (nTargetWidth / 100), Integer)

            Case ResizeType.PixelFixed
                nNewHeight = nTargetHeight
                nNewWidth = nTargetWidth

            Case ResizeType.PixelMax
                Select Case True
                    Case nCurrentHeight = nCurrentWidth
                        nNewHeight = System.Math.Max(nTargetHeight, nTargetWidth)
                        nNewWidth = nNewHeight
                    Case nCurrentHeight > nCurrentWidth
                        nNewHeight = nTargetHeight
                        nNewWidth = CType(nCurrentWidth * (nNewHeight / nCurrentHeight), Integer)
                    Case nCurrentWidth > nCurrentHeight
                        nNewWidth = nTargetWidth
                        nNewHeight = CType(nCurrentHeight * (nNewWidth / nCurrentWidth), Integer)
                End Select

        End Select
        If nNewHeight >= nCurrentHeight And nNewWidth >= nCurrentWidth Then
            objAdjustedImage = objImage
        Else
            Select Case eResizeOp
                Case ResizeOp.Bitmap
                    objAdjustedImage = New Bitmap(objImage, nNewWidth, nNewHeight)
                Case ResizeOp.Thumbnail
                    objAdjustedImage = objImage.GetThumbnailImage(nNewWidth, nNewHeight, Nothing, IntPtr.Zero)
            End Select
        End If

        Return objAdjustedImage
    End Function

End Class

Peter
----------------------------------------
Work smarter, not harder.
  #4 (permalink)  
Old November 4th, 2003, 07:15 AM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 56
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cheers for all your help Peter - and thanks for the code - I think it's gonna be extremely useful :)

Lucy

  #5 (permalink)  
Old November 6th, 2003, 12:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Hi Peter.

That code looks very useful but is it not ASP.NET and not classic ASP?
While I know only VBScript and JScript for classic ASP, this looks alot like VB. I would like to use your code and I've got .NET, but how do I implement it in a classic ASP site?
Perhaps like this:

<%
Dim objImageTools
Set objImageTools = New ImageTools
...

Regards - Jon
  #6 (permalink)  
Old November 6th, 2003, 12:38 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Wow.. now I feel like an idiot. I replied to this topic without even noticing it was the ASP forum, not an ASP.Net one!

Because these methods take and return .Net objects, you wouldn't be able to use them directly in class ASP.

HOWEVER!... ;)

With some modifications, one might be able to change the methods to handle simpler arrays that hold the binary data of an image, then you could compile this code in a .Net assembly that was set up to register for COM interop. Then you could use it in classic asp. (this is all just theoretical of course. :D)

Peter
------------------------------------------------------
Work smarter, not harder.
  #7 (permalink)  
Old November 6th, 2003, 12:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

I think I just stick to serverobjects.com :D
Regards - Jon
  #8 (permalink)   Spam!  
Old July 16th, 2008, 05:07 AM
Registered User
 
Join Date: Jul 2008
Location: Brisbane, QLD, Australia.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cropping images online has come a long way since....

Here's an article on how to crop images using javascript and ASP

http://www.imagecowboy.com/articles/...javascript.asp

live hard, live wide, live long and don't regret

Last edited by googleme; January 30th, 2009 at 03:59 AM.. Reason: domain name change
  #9 (permalink)  
Old August 28th, 2009, 03:38 PM
ZTS ZTS is offline
Registered User
 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i think this component can help you:
http://i-load.radactive.com/
  #10 (permalink)  
Old January 20th, 2011, 01:40 AM
Registered User
 
Join Date: Jan 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: How to crop image

if you want crop any image, than i will suggest you to visit the link below
http://www.raiseitsolutions.com/foru...ic.php?f=4&t=2
if you visit this link, you will know how to crop an image easily
thank you very much. :)


Similar Threads
Thread Thread Starter Forum Replies Last Post
set image on <asp:Image> stored in DataBase myself.panku ASP.NET 2.0 Professional 1 August 11th, 2008 10:41 AM
How to replace image in asp?? Swati Chari Classic ASP Basics 1 March 27th, 2008 07:39 AM
Displaying a image in asp dhillon555 ASP.NET 1.0 and 1.1 Basics 0 February 18th, 2007 10:16 PM
Determine an Image dimension - ASP cancer10 Classic ASP Databases 3 November 21st, 2006 10:48 AM
crop image caster11 ASP.NET 1.x and 2.0 Application Design 1 November 12th, 2003 04:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.