Hi Can anyone please show an example on how to resize a image whith code behind Lets say we have an input file to select the image then with code behind grab the info for that file and resize it to a specific width and heigth perserving aspect ratio Thanks
in advance Cheers
Do you mean... on the fly? Let's say an image of 800x600 on the server, but in your page an image sized 400x300? Put the next code in a textfile
response.clear
'Read in the image filename to resize
Dim strImgSrc as String = Request.QueryString("img")
Dim intMaxImgWidth as Integer = Request.QueryString("x")
Dim intMaxImgHeight as Integer = Request.QueryString("y")
Dim intNewWidth as Integer
Dim intNewHeight as Integer
Dim Img as System.Drawing.Image
Img = System.Drawing.Image.FromFile(Server.MapPath(strImgSrc))
If Img.Height > intMaxImgHeight
intNewWidth = (intMaxImgHeight * (Img.Width/Img.Height))
intNewHeight = intMaxImgHeight
Else
intNewWidth = Img.Width
intNewHeight = Img.Height
End if
If intNewWidth > intMaxImgWidth
intNewHeight = (intMaxImgWidth * (intNewHeight/intNewWidth))
intNewWidth = intMaxImgWidth
End if
Img = New system.drawing.bitmap(Img, intNewWidth, intNewHeight)
Img.Save(Response.OutputStream, imageformat.jpeg)
Img.Dispose()
response.end
%>
save it as 'resized.aspx'
In your page use it as the source for your images.
Like
This does the trick... see it work at www.hadesmoon.com/photo.aspx Cheers, Wes
SixString
Member
700 Points
140 Posts
Image resize ??
Jan 26, 2004 04:27 PM|LINK
Hugenstein
Member
145 Points
29 Posts
Re: Image resize ??
Jan 26, 2004 06:59 PM|LINK
webbes
Participant
1987 Points
426 Posts
Re: Image resize ??
Jan 29, 2004 11:28 AM|LINK
response.clear 'Read in the image filename to resize Dim strImgSrc as String = Request.QueryString("img") Dim intMaxImgWidth as Integer = Request.QueryString("x") Dim intMaxImgHeight as Integer = Request.QueryString("y") Dim intNewWidth as Integer Dim intNewHeight as Integer Dim Img as System.Drawing.Image Img = System.Drawing.Image.FromFile(Server.MapPath(strImgSrc)) If Img.Height > intMaxImgHeight intNewWidth = (intMaxImgHeight * (Img.Width/Img.Height)) intNewHeight = intMaxImgHeight Else intNewWidth = Img.Width intNewHeight = Img.Height End if If intNewWidth > intMaxImgWidth intNewHeight = (intMaxImgWidth * (intNewHeight/intNewWidth)) intNewWidth = intMaxImgWidth End if Img = New system.drawing.bitmap(Img, intNewWidth, intNewHeight) Img.Save(Response.OutputStream, imageformat.jpeg) Img.Dispose() response.end %>save it as 'resized.aspx' In your page use it as the source for your images. Like This does the trick... see it work at www.hadesmoon.com/photo.aspx Cheers, Wes