Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Resizing Uploaded Images


Message #1 by "Hugh McLaughlin" <hugh@k...> on Wed, 18 Sep 2002 04:39:55
Hello Everyone and thanks for your help in advance.  I am working on an 
application that allows the user to upload an image to the server.  It 
uses the syntax that I have found in many examples, i.e.

ImageStream = PersonImage.PostedFile.InputStream 

Dim ImageContent(intImageSize) As Byte

Dim intStatus As Integer

intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

From there, the ImageContent is set equal to a stored procedure parameter 
and add to a database.  This works fine, however, I want to be able to 
resize the image before inserting the image.  But since the image is read 
into an array, I do not know how to change the array to a 
System.Drawing>image type in order to ascertain the height and width of 
the image, and then perform resizing.  Any help on this topic would be 
greatly appreciated.  Thanks.
Message #2 by "Márcio Roberto de MatosVarella" <marciovarella123@y...> on Wed, 18 Sep 2002 14:26:32
> Hello Everyone and thanks for your help in advance.  I am working on an 
a> pplication that allows the user to upload an image to the server.  It 
u> ses the syntax that I have found in many examples, i.e.

> ImageStream = PersonImage.PostedFile.InputStream 

> Dim ImageContent(intImageSize) As Byte

> Dim intStatus As Integer

> intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

> From there, the ImageContent is set equal to a stored procedure 
parameter 
a> nd add to a database.  This works fine, however, I want to be able to 
r> esize the image before inserting the image.  But since the image is 
read 
i> nto an array, I do not know how to change the array to a 
S> ystem.Drawing>image type in order to ascertain the height and width of 
t> he image, and then perform resizing.  Any help on this topic would be 
g> reatly appreciated.  Thanks.
Message #3 by "Hugh McLaughlin" <hugh@k...> on Wed, 18 Sep 2002 14:30:46
Marcio,

Your response did not post.

Hugh

> > Hello Everyone and thanks for your help in advance.  I am working on 
an 
a> > pplication that allows the user to upload an image to the server.  
It 
u> > ses the syntax that I have found in many examples, i.e.

> > ImageStream = PersonImage.PostedFile.InputStream 

> > Dim ImageContent(intImageSize) As Byte

> > Dim intStatus As Integer

> > intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

> > From there, the ImageContent is set equal to a stored procedure 
p> arameter 
a> > nd add to a database.  This works fine, however, I want to be able 
to 
r> > esize the image before inserting the image.  But since the image is 
r> ead 
i> > nto an array, I do not know how to change the array to a 
S> > ystem.Drawing>image type in order to ascertain the height and width 
of 
t> > he image, and then perform resizing.  Any help on this topic would 
be 
g> > reatly appreciated.  Thanks.
Message #4 by "Mingkun Goh" <mangokun@h...> on Fri, 20 Sep 2002 00:32:56
Below is the full set of my image resizing functions:


#Region "Proportional Image Resizing Functions"

    ' This function returns a proportional size for thumbnail image
    Public Function ScaledThumbnailSize(ByVal ImageWidth As Integer, ByVal 
ImageHeight As Integer, ByVal MaxWidth As Integer, ByVal MaxHeight As 
Integer) As Size
        Dim NewSize As New Size()

        If MaxWidth >= ImageWidth And MaxHeight >= ImageHeight Then
            ' no need to make thumbnail
            ' as current image already smaller than required size
            NewSize.Width = ImageWidth
            NewSize.Height = ImageHeight
        Else
            ' Calculate the Size of the New image  
            Dim tempMultiplier As Double

            If ImageWidth = ImageHeight Then    ' square image
                If MaxWidth <= MaxHeight Then
                    tempMultiplier = MaxWidth / ImageWidth
                Else
                    tempMultiplier = MaxHeight / ImageWidth
                End If
            Else                                    ' landscape/portrait 
image
                If MaxWidth = MaxHeight Then
                    If ImageWidth > ImageHeight Then    ' landscape
                        tempMultiplier = MaxWidth / ImageWidth
                    Else                                    ' portrait
                        tempMultiplier = MaxHeight / ImageHeight
                    End If
                Else
                    If MaxWidth < ImageWidth And MaxHeight < ImageHeight 
Then
                        ' thumbnail area totally inside image area
                        ' try using ImageWidth
                        tempMultiplier = MaxWidth / ImageWidth
                        If CInt(ImageHeight * tempMultiplier) > MaxHeight 
Then
                            tempMultiplier = MaxHeight / ImageHeight
                        End If
                    ElseIf MaxWidth > ImageWidth Then
                        ' thumbnail area longer than image area
                        tempMultiplier = MaxHeight / ImageHeight
                    Else
                        ' thumbnail area taller than image area
                        tempMultiplier = MaxWidth / ImageWidth
                    End If
                End If
            End If

            NewSize.Width = CInt(ImageWidth * tempMultiplier)
            NewSize.Height = CInt(ImageHeight * tempMultiplier)
        End If

        Return NewSize
    End Function

    ' This overloaded function takes in a different set of parameters
    Public Function ScaledThumbnailSize(ByVal ImageSize As Size, ByVal 
MaxSize As Size) As Size
        Return ScaledThumbnailSize(ImageSize.Width, ImageSize.Height, 
MaxSize.Width, MaxSize.Height)
    End Function

    ' This function would take in an image, and returns a proportional 
resized thumbnail image
    ' The 1st parameter OrginalImage would be the image you want to be 
scaled to fit within a container
    ' The 2nd parameter Container can be an control such as PictureBox, or 
an structure such as Rectangle
    Public Function ScaledThumbnailImage(ByVal OriginalImage As Image, 
ByVal Container As Object) As Image
        Dim NewSize As New Size()
        NewSize = ScaledThumbnailSize(OriginalImage.Size, Container.Size)

        Return OriginalImage.GetThumbnailImage(NewSize.Width, 
NewSize.Height, Nothing, Nothing)
    End Function

    ' This overloaded function takes in a different set of parameters
    Public Function ScaledThumbnailImage(ByVal OriginalImage As Image, 
ByVal ContainerSize As Size) As Image
        Dim NewSize As New Size()
        NewSize = ScaledThumbnailSize(OriginalImage.Size, ContainerSize)

        Return OriginalImage.GetThumbnailImage(NewSize.Width, 
NewSize.Height, Nothing, Nothing)
    End Function

    ' This overloaded function takes in a different set of parameters
    Public Function ScaledThumbnailImage(ByVal OriginalImage As Image, 
ByVal ContainerWidth As Integer, ByVal ContainerHeight As Integer) As Image
        Dim NewSize As New Size()
        NewSize = ScaledThumbnailSize(OriginalImage.Size.Width, 
OriginalImage.Size.Height, ContainerWidth, ContainerHeight)

        Return OriginalImage.GetThumbnailImage(NewSize.Width, 
NewSize.Height, Nothing, Nothing)
    End Function

#End Region



Note that you MAY HAVE to add 2 namespace to the top of your code, which 
are: 
Imports System.Drawing
Imports System.Drawing.Imaging


Here are some example usage: 

1.      Dim imgPhoto As Image = Image.FromFile(OpenFileDialog1.FileName)
        PictureBox1.Image = ScaledThumbnailImage(imgPhoto, PictureBox1)

2.      Dim img As Image = GetHttpImage(HttpStockChartImageUri)
        img = ScaledThumbnailImage(img, 300, 200)
        Return img

3.      imgPhoto = Image.FromFile(OpenFileDialog1.FileName)
        Dim thumbSize As New Size()
        thumbSize = ScaledThumbnailSize(imgPhoto.Size, PictureBox1.Size)
        PictureBox1.Image = imgPhoto.GetThumbnailImage(thumbSize.Width, 
thumbSize.Height, Nothing, Nothing)


You can put my 'Proportional Image Resizing Functions' into a Module file 
or a Class Library, or even build it into a reusable DLL (althrough the 
later approach is typically prefered).



Similar Thread in p2p.wrox.com - aspx

Question on ASPToday Image Resizing Article
http://p2p.wrox.com/archive/aspx/2002-04/72.asp


Previous message:

Hello Everyone and thanks for your help in advance.  I am working on an 
application that allows the user to upload an image to the server.  It 
uses the syntax that I have found in many examples, i.e.

ImageStream = PersonImage.PostedFile.InputStream 

Dim ImageContent(intImageSize) As Byte

Dim intStatus As Integer

intStatus = ImageStream.Read(ImageContent, 0, intImageSize)

From there, the ImageContent is set equal to a stored procedure parameter 
and add to a database.  This works fine, however, I want to be able to 
resize the image before inserting the image.  But since the image is read 
into an array, I do not know how to change the array to a 
System.Drawing>image type in order to ascertain the height and width of 
the image, and then perform resizing.  Any help on this topic would be 
greatly appreciated.  Thanks.

  Return to Index