List Info

Thread: Image resize




Image resize
country flaguser name
United States
2008-02-27 17:33:22
I'm working on uploading an image using the file upload
control.  These
will always be images and what I'd like to do is force the
image to be a
certain width and let the height automatically resize itself
to keep the
aspect ratio even.  I'm not finding much online but it seems
as if I'll
be using the system.drawing namespace.  Anyone have any good
resources
for getting this done in VB?   

Is it possible to manipulate it on upload before it's saved
to the
server?  One of my form fields is a product code, my plan is
to resize
the image (so they are all the same width) and rename it
using the value
in the product code text box then save it to the server. 
This way I can
just grab the image for that product easily by referencing
the code.
What do you think?
 
Russ 
 



--- List Settings ---
http://aspadvice.com/list
s/


RE: Image resize
country flaguser name
United States
2008-02-27 19:29:37
Try

http://www.google.com/search?sourceid=n
avclient&ie=UTF-8&rls=GGLG,GGLG:2005-45,GGLG:en&
amp;q=aspect+ratio+vb%2enet

----- Original Message ----- 
From: "Russ Peters" <rpetersredcanoecu.com>
To: <aspnetaspadvice.com>
Sent: Wednesday, February 27, 2008 6:33 PM
Subject: [aspnet] Image resize


> I'm working on uploading an image using the file upload
control.  These
> will always be images and what I'd like to do is force
the image to be a
> certain width and let the height automatically resize
itself to keep the
> aspect ratio even.  I'm not finding much online but it
seems as if I'll
> be using the system.drawing namespace.  Anyone have any
good resources
> for getting this done in VB?
>
> Is it possible to manipulate it on upload before it's
saved to the
> server?  One of my form fields is a product code, my
plan is to resize
> the image (so they are all the same width) and rename
it using the value
> in the product code text box then save it to the
server.  This way I can
> just grab the image for that product easily by
referencing the code.
> What do you think?
>
> Russ
>
>
>
>
> --- List Settings ---
> http://aspadvice.com/list
s/
>
>
>
> -- 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.516 / Virus Database: 269.21.1/1300 -
Release Date: 2/26/2008
7:50 PM
>
>



--- List Settings ---
http://aspadvice.com/list
s/


RE: Image resize
user name
2008-02-28 11:32:53
Here is a simplified VB.NET version of what I use (no error
handling or
graceful failure, assumes the user provided a valid image,
etc.). You
specify a max width and a max height, and it will scale the
image down to
fit within those dimensions while preserving aspect ratio.

You can call it for an uploaded image by passing the
FileUpload.FileContent
property, which is a stream representation of the uploaded
data.

Private Sub SaveImage(ByVal stream As Stream, ByVal filename
As String,
ByVal MaxHeight As Integer, ByVal MaxWidth As Integer)
    Dim imgSrc As System.Drawing.Image =
System.Drawing.Image.FromStream(stream)
    Dim sourceWidth As Integer = imgSrc.Width
    Dim sourceHeight As Integer = imgSrc.Height
    Dim sourceX As Integer = 0
    Dim sourceY As Integer = 0
    Dim destX As Integer = 0
    Dim destY As Integer = 0
    Dim destWidth As Integer = MaxWidth
    Dim destHeight As Integer =
CInt(Math.Round(CDbl((sourceHeight *
(CDbl(destWidth) / CDbl(sourceWidth))))))
    If (destHeight > MaxHeight) AndAlso (MaxHeight >
0) Then
        destWidth = CInt(Math.Round(CDbl((sourceWidth *
(CDbl(MaxHeight) /
CDbl(sourceHeight))))))
        destHeight = MaxHeight
    End If
    Dim newImage As New Bitmap(destWidth, destHeight,
PixelFormat.Format24bppRgb)
    newImage.SetResolution(imgSrc.HorizontalResolution,
imgSrc.VerticalResolution)
    Dim grPhoto As Graphics = Graphics.FromImage(newImage)
    grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic
    grPhoto.DrawImage(imgSrc, New Rectangle(destX, destY,
destWidth,
destHeight), New Rectangle(sourceX, sourceY, sourceWidth,
sourceHeight),
GraphicsUnit.Pixel)
    grPhoto.Dispose()
    
    Dim outputStream As New FileStream("path" +
filename),
FileMode.CreateNew)
    newImage.Save(outputStream, ImageFormat.Jpeg)
    outputStream.Flush()
    outputStream.Close()
End Sub


Best regards

/Rex


-----Original Message-----
From: Russ Peters [mailto:rpetersredcanoecu.com] 
Sent: Wednesday, February 27, 2008 6:33 PM
To: aspnetaspadvice.com
Subject: [aspnet] Image resize

I'm working on uploading an image using the file upload
control.  These
will always be images and what I'd like to do is force the
image to be a
certain width and let the height automatically resize itself
to keep the
aspect ratio even.  I'm not finding much online but it seems
as if I'll
be using the system.drawing namespace.  Anyone have any good
resources
for getting this done in VB?   

Is it possible to manipulate it on upload before it's saved
to the
server?  One of my form fields is a product code, my plan is
to resize
the image (so they are all the same width) and rename it
using the value
in the product code text box then save it to the server. 
This way I can
just grab the image for that product easily by referencing
the code.
What do you think?
 
Russ 
 



--- List Settings ---
http://aspadvice.com/list
s/


--- List Settings ---
http://aspadvice.com/list
s/


RE: Image resize
country flaguser name
United States
2008-02-28 13:13:15
Thanks everyone for the feedback, as common as I think this
function is
you'd think it would be easy to find something out there
that'll do it.
I'll give this a try Rex.  



> -----Original Message-----
> From: Rex Morgan [mailto:rexrexmorgan.net]
> Sent: Thursday, February 28, 2008 9:33 AM
> To: aspnetaspadvice.com
> Subject: [aspnet] RE: Image resize
> 
> Here is a simplified VB.NET version of what I use (no
error handling
or
> graceful failure, assumes the user provided a valid
image, etc.). You
> specify a max width and a max height, and it will scale
the image down
to
> fit within those dimensions while preserving aspect
ratio.
> 
> You can call it for an uploaded image by passing the
> FileUpload.FileContent
> property, which is a stream representation of the
uploaded data.
> 
> Private Sub SaveImage(ByVal stream As Stream, ByVal
filename As
String,
> ByVal MaxHeight As Integer, ByVal MaxWidth As Integer)
>     Dim imgSrc As System.Drawing.Image =
> System.Drawing.Image.FromStream(stream)
>     Dim sourceWidth As Integer = imgSrc.Width
>     Dim sourceHeight As Integer = imgSrc.Height
>     Dim sourceX As Integer = 0
>     Dim sourceY As Integer = 0
>     Dim destX As Integer = 0
>     Dim destY As Integer = 0
>     Dim destWidth As Integer = MaxWidth
>     Dim destHeight As Integer =
CInt(Math.Round(CDbl((sourceHeight *
> (CDbl(destWidth) / CDbl(sourceWidth))))))
>     If (destHeight > MaxHeight) AndAlso (MaxHeight
> 0) Then
>         destWidth = CInt(Math.Round(CDbl((sourceWidth
*
(CDbl(MaxHeight) /
> CDbl(sourceHeight))))))
>         destHeight = MaxHeight
>     End If
>     Dim newImage As New Bitmap(destWidth, destHeight,
> PixelFormat.Format24bppRgb)
>    
newImage.SetResolution(imgSrc.HorizontalResolution,
> imgSrc.VerticalResolution)
>     Dim grPhoto As Graphics =
Graphics.FromImage(newImage)
>     grPhoto.InterpolationMode =
InterpolationMode.HighQualityBicubic
>     grPhoto.DrawImage(imgSrc, New Rectangle(destX,
destY, destWidth,
> destHeight), New Rectangle(sourceX, sourceY,
sourceWidth,
sourceHeight),
> GraphicsUnit.Pixel)
>     grPhoto.Dispose()
> 
>     Dim outputStream As New FileStream("path"
+ filename),
> FileMode.CreateNew)
>     newImage.Save(outputStream, ImageFormat.Jpeg)
>     outputStream.Flush()
>     outputStream.Close()
> End Sub
> 
> 
> Best regards
> 
> /Rex
> 
> 


--- List Settings ---
http://aspadvice.com/list
s/


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )