Image resize on the fly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fran7
    New Member
    • Jul 2006
    • 229

    #1

    Image resize on the fly

    Hi, I have this asp.net code to resize my images. It works fine although it resizes images without keeping the aspect ratio. My image display page is an .asp page extension.

    I would rather keep aspect ratio of my images. Could anyone tell me if there is a tinker one could do with this code to do that or would i need to look for another code to do the job.

    Thanks in advance

    Richard

    Code:
    <%@ OutputCache Duration="600" VaryByParam="*" %>
    <%@ Page Debug="false" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    <%@ Import Namespace="System.IO" %>
    <script language="VB" runat="server">
    	
        Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            
            On Error Resume Next		
                 
            'retrieve relative path to image
            Dim imageUrl As String = HttpUtility.UrlDecode(Request.QueryString("img"))	
    		dim doresize As Boolean
    		doresize=true       
    		
            response.contenttype="image/jpeg"
            
            Dim pictureResizeSecCode As String = "quickresize" 'change this code!
            Dim resizePictureToPx As String = HttpUtility.UrlDecode(Request.QueryString("resizePictureToPx"))
            
            'prepare thumbnail
            Dim fullSizeImg As System.Drawing.Image
            fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(imageUrl))        
            
            If Request.QueryString("getWidthOnly") = "true" Then
                Response.Write(fullSizeImg.Width)
                fullSizeImg.Dispose()
                Response.End()
            End If
            
            If fullSizeImg Is Nothing Then Response.End()            
            
            '??
            Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
            dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            
            Dim thumbNailImg As System.Drawing.Image
            Dim newWidth As Integer
            Dim newHeight As Integer
            Dim maxSize As Integer
            If IsNumeric(Request.QueryString("maxSize")) And Request.QueryString("maxSize") <> "" Then
                maxSize = Request.QueryString("maxSize")
            Else
                maxSize = 0
            End If	
            
            'calculate new widht/height, if any  
            
            If fullSizeImg.Width > maxSize Or fullSizeImg.Height > maxSize Then			
            
                'for better quality
                fullSizeImg.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX)
                fullSizeImg.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX)
            
                If fullSizeImg.Width >= fullSizeImg.Height Then
                    newWidth = maxSize
                    newHeight = (fullSizeImg.Height / fullSizeImg.Width) * maxSize
                Else
                    newWidth = (fullSizeImg.Width / fullSizeImg.Height) * maxSize
                    newHeight = maxSize
                End If
            Else
                
                newWidth = fullSizeImg.Width
                newHeight = fullSizeImg.Height
                doresize = False
                
            End If
            
            dim FSR as string = Request.QueryString("FSR")
            IF FSR IS NOTHING ANDALSO FSR.length = 0 Then
            FSR="0"
            END IF
            
            If FSR = "1" Then
                If newWidth < newHeight Then
                    thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth * (newHeight / newWidth), newHeight * (newHeight / newWidth), dummyCallBack, IntPtr.Zero)
                    thumbNailImg = CropImage(thumbNailImg, New Point(0, (newHeight - newWidth) / 2), New Point(newHeight, newHeight + (newHeight - newWidth) / 2))
                Else
                    thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth * (newWidth / newHeight), newHeight * (newWidth / newHeight), dummyCallBack, IntPtr.Zero)
                    thumbNailImg = CropImage(thumbNailImg, New Point((newWidth - newHeight) / 2, 0), New Point(newWidth + ((newWidth - newHeight) / 2), newWidth))
            End If				
            Else
                If doresize=True Then
                    thumbNailImg = fullSizeImg.GetThumbnailImage(newWidth, newHeight, dummyCallBack, IntPtr.Zero)
                else				
                    thumbNailImg = fullSizeImg					
                End If
            End If
            
            
            createOutput(thumbNailImg)                   
            
            
            'Clean up / Dispose...
            thumbNailImg.Dispose()
            
            'Clean up / Dispose...
            fullSizeImg.Dispose()
                
            On Error GoTo 0
        End Sub
    
        Sub createOutput(ByRef ImageObj As System.Drawing.Image)
            
            On Error Resume Next
    		
    		Dim specialEffect as String = HttpUtility.UrlDecode(Request.QueryString("SE"))
    		
    		select case specialEffect
    			case "1"
    				PureBW (ImageObj)
    			case "2"
    				GrayScale (ImageObj)
    			case "3"
    				PixelLoopConvert (ImageObj)
    			
    		end select
    			
            ImageObj.Save(Response.OutputStream, ImageFormat.Jpeg)
    
            On Error GoTo 0
            
        End Sub
        
        Private Function CropImage(ByVal OriginalImage As Bitmap, ByVal TopLeft As Point, ByVal BottomRight As Point) As Bitmap
            Dim btmCropped As New Bitmap((BottomRight.Y - TopLeft.Y), (BottomRight.X - TopLeft.X))
            Dim grpOriginal As Graphics = Graphics.FromImage(btmCropped)
      
            grpOriginal.DrawImage(OriginalImage, New Rectangle(0, 0, btmCropped.Width, btmCropped.Height), _
                TopLeft.X, TopLeft.Y, btmCropped.Width, btmCropped.Height, GraphicsUnit.Pixel)
            grpOriginal.Dispose()
      
            Return btmCropped
        End Function
        
    
        Function ThumbnailCallback() As Boolean
            
            Return False
        
        End Function
    	
    	Public Function PureBW(ByVal image As System.Drawing.Bitmap, Optional ByVal Mode As BWMode = BWMode.By_Lightness, Optional ByVal tolerance As Single = 0) As System.Drawing.Bitmap
            Dim x As Integer
            Dim y As Integer
            If tolerance > 1 Or tolerance < -1 Then
                Throw New ArgumentOutOfRangeException
                Exit Function
            End If
            For x = 0 To image.Width - 1 Step 1
                For y = 0 To image.Height - 1 Step 1
                    Dim clr As Color = image.GetPixel(x, y)
                    If Mode = BWMode.By_RGB_Value Then
                        If (CInt(clr.R) + CInt(clr.G) + CInt(clr.B)) > 383 - (tolerance * 383) Then
                            image.SetPixel(x, y, Color.White)
                        Else
                            image.SetPixel(x, y, Color.Black)
                        End If
                    Else
                        If clr.GetBrightness > 0.5 - (tolerance / 2) Then
                            image.SetPixel(x, y, Color.White)
                        Else
                            image.SetPixel(x, y, Color.Black)
                        End If
                    End If
                Next
            Next
            Return image
        End Function
    	
        Enum BWMode
            By_Lightness
            By_RGB_Value
        End Enum
    	
    	Public Function GrayScale (ByVal image As System.Drawing.Bitmap)
    		
    		Dim X As Integer
    		Dim Y As Integer
    		Dim clr As Integer
    
    		For X = 0 To image.Width - 1
    			For Y = 0 To image.Height - 1
    				clr = (CInt(image.GetPixel(X, Y).R) + _
    					   image.GetPixel(X, Y).G + _
    					   image.GetPixel(X, Y).B) \ 3
    				image.SetPixel(X, Y, Color.FromArgb(clr, clr, clr))
    			Next Y
    		Next X
    		
    		return image
    		
    	End Function
    	
    	Public Function PixelLoopConvert (ByVal Image As System.Drawing.Bitmap)    
       
        For i As Integer = 0 To Image.Width - 1
          For j As Integer = 0 To Image.Height - 1
            Dim iRed As Integer = Image.GetPixel(i, j).R
            Dim iGreen As Integer = Image.GetPixel(i, j).G
            Dim iBlue As Integer = Image.GetPixel(i, j).B
    
            ' Bereken de nieuwe RGB-waarde voor deze pixel:
            Dim iSepiaRed As Integer = Math.Min(Convert.ToInt32(iRed * 0.393 + iGreen * 0.769 + iBlue * 0.189), 255)
            Dim iSepiaGreen As Integer = Math.Min(Convert.ToInt32(iRed * 0.349 + iGreen * 0.686 + iBlue * 0.168), 255)
            Dim iSepiaBlue As Integer = Math.Min(Convert.ToInt32(iRed * 0.272 + iGreen * 0.534 + iBlue * 0.131), 255)
    
            ' Wijzig de RGB-waarde van de huidige pixel van de oude naar de nieuwe:
            Image.SetPixel(i, j, Color.FromArgb(iSepiaRed, iSepiaGreen, iSepiaBlue))
          Next
        Next
    	
        Return Image
    
      End Function
        
    </script>

    Code:
    <img src="showthumb.aspx?fsr=1&amp;maxsize=200&amp;img=<%=Server.URLPathEncode(CStr(thumbnail))%>"/>
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The way I keep the aspect ratio of my thumbnail images is to reduce the original height and width at the same rate until they both fit within the boundaries of my size restrictions.

    Something like:

    Code:
    Dim newHeight As Integer = fullSizeImg.Height
    Dim newWidth As Integer = fullSizeImg.Width
    If  newHeight > maxSize Or  newWidth > maxSize Then
      While newHeight > maxSize Or newWidth > smallWidth
        newHeight = newHeight - (newHeight * 0.05)
        newWidth = newWidth - (newWidth * 0.05)
      End While
    End If
    -Frinny
    Last edited by Frinavale; Dec 9 '14, 03:53 PM.

    Comment

    Working...