Results 1 to 2 of 2

Thread: Resample Images

  1. #1

    Thread Starter
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Resample Images

    Hello,

    I am using VS2010 and have an application that lets the user search for an image to store on a SQL database. I want to be able to reduce the size of the image when it passes certain size for obvious reasons.

    Is there a way I can do this in runtime?
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Resample Images

    This function will do the job:

    Code:
    Private Function SizeLimitedImage(image As Image, maxPixels As Integer) As Image
    	If image.Width * image.Height > maxPixels Then
    		Dim aspectRatio As Double = image.Height / image.Width
    		Dim targetHeight As Integer = CInt(Math.Sqrt(aspectRatio * maxPixels))
    		Dim targetWidth As Integer = CInt(maxPixels / targetHeight)
    		Return New Bitmap(image, targetWidth, targetHeight)
    	Else
    		Return image
    	End If
    End Function
    The logic of this is that you want the image to keep the same shape, so if w is the target width and h is the target height:
    Code:
    h/w = aspect ratio (= image height / image width)
    w*h = maximum number of pixels
    Multiplying the two equations to eliminate w we get:
    Code:
    h^2 = aspect ratio * maximum number of pixels
    hence the square root.

    BB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width