Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP.NET (Non-Forum Related)
 Grabbing file, resize, then rename. How?
 New Topic  Topic Locked
 Printer Friendly
Author Previous Topic Topic Next Topic  

Senior Member

1218 Posts

Posted - 25 November 2007 :  16:25:00  Show Profile  Send Astralis a Yahoo! Message
I use the following code to resize an image on upload. But now I want to resize an image that is already in a folder. I will grab the image name from the querystring.

I know I need to use but converting this is not working very well because I'm new to .net. I'm thinking this should be easy to do. How could this be converted to grab the file, resize it, then save it to a new location?

<%@ Page Trace="False" Language="vb" aspcompat="false" debug="true" validateRequest="false"%>
<%@ Import Namespace=System.Drawing %>
<%@ Import Namespace=System.Drawing.Imaging %>
<%@ Import Namespace=System %>
<%@ Import Namespace=System.Web %>
<SCRIPT LANGUAGE="VBScript" runat="server">
const Lx = 193 ' max width for thumbnails
const Ly = 138 ' max height for thumbnails
const Px = 100 ' max width for tinythumbnails
const Py = 75 ' max height for tinythumbnails
const upload_dir = "article_images/" ' directory to upload file
const upload_original = "sample" ' filename to save original as (suffix added by script)
const upload_thumb = "thumb" ' filename to save thumbnail as (suffix added by script)
const upload_tinythumb = "tiny" ' filename to save tiny thumbnail as (suffix added by script)
const dirname = "c:/Inetpub/wwwroot/site/"
const upload_max_size = 300 ' max size of the upload (KB) note: this doesn't override any server upload limits
dim fileExt ' used to store the file extension (saves finding it mulitple times)
dim fileNm ' used to store filename
dim newWidth, newHeight as integer ' new width/height for the thumbnail
dim tnewWidth, tnewHeight as integer ' new width/height for the tiny thumbnail
dim l2 ' temp variable used when calculating new size
dim fileFld as HTTPPostedFile ' used to grab the file upload from the form
Dim originalimg As System.Drawing.Image ' used to hold the original image
dim msg ' display results
dim upload_ok as boolean ' did the upload work ?
Dim i As Integer

randomize() ' used to help the cache-busting on the preview images
upload_ok = false

if lcase(Request.ServerVariables("REQUEST_METHOD"))="post" then
For i = 0 To Request.Files.Count - 1
fileFld = request.files(i)
' fileFld = request.files(0) ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)

'Start iteration
'Do Until iCount = fileFld.Count
if fileFld.ContentLength > upload_max_size * 1024 then
msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
originalImg = System.Drawing.Image.FromStream(fileFld.InputStream)
' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
' Note: if the original is smaller than the thumbnail size it will be scaled up
If (originalImg.Width/Lx) > (originalImg.Width/Ly) Then
L2 = originalImg.Width
newWidth = Lx
newHeight = originalImg.Height * (Lx / L2)
if newHeight > Ly then
newWidth = newWidth * (Ly / newHeight)
newHeight = Ly
end if
L2 = originalImg.Height
newHeight = Ly
tnewHeight = Py
newWidth = originalImg.Width * (Ly / L2)
tnewWidth = originalImg.Width * (Py / L2)
if newWidth > Lx then
newHeight = newHeight * (Lx / newWidth)
newWidth = Lx
end if
if tnewWidth > Px then
tnewHeight = tnewHeight * (Px / tnewWidth)
tnewWidth = Px
end if
End If

Dim thumb As New Bitmap(newWidth, newHeight)
Dim tinythumb As New Bitmap(tnewWidth, tnewHeight)
'Create a graphics object
Dim gr_dest As Graphics = Graphics.FromImage(thumb)
Dim gr_Tdest As Graphics = Graphics.FromImage(tinythumb)

' just in case it's a transparent GIF force the bg to white
dim sb = new SolidBrush(System.Drawing.Color.White)
gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
gr_Tdest.FillRectangle(sb, 0, 0, tinythumb.Width, tinythumb.Height)

'Re-draw the image to the specified height and width
gr_dest.DrawImage(originalImg, 0, 0, thumb.Width, thumb.Height)
gr_Tdest.DrawImage(originalImg, 0, 0, tinythumb.Width, tinythumb.Height)

fileNm = System.IO.Path.GetFileName(fileFld.FileName).ToLower()
fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower() & upload_dir & upload_original & fileNm ), originalImg.rawformat) & upload_dir & upload_thumb & fileNm ), originalImg.rawformat) & upload_dir & upload_tinythumb & fileNm ), originalImg.rawformat)
msg = "Uploaded " & fileFld.FileName & " to " & (dirname & upload_dir & upload_original & fileNm )
upload_ok = true
msg = "Sorry, there was a problem saving the image."
msg = msg & dirname & upload_dir & upload_thumb & fileNm
end try
' Housekeeping for the generated thumbnail
if not thumb is nothing then
thumb = nothing
end if
if not tinythumb is nothing then
tinythumb = nothing
end if
msg = "Sorry, that was not an image we could process."
msg = msg & dirname & upload_dir & upload_thumb & fileNm
end try
end if

' House Keeping !
if not originalImg is nothing then
originalImg = nothing
end if

'iCount += 1 'End iteration
'Loop 'End iteration
end if

Forum Admin

United Kingdom
20550 Posts

Posted - 26 November 2007 :  03:40:30  Show Profile  Visit HuwR's Homepage
this is the line that loads the image

originalImg = System.Drawing.Image.FromStream(fileFld.InputStream)

youy need to replace the fileFld.InputStream with a local filestream to the file you want to open something like
Dim myFileStream As New FileStream("C:\FileName.txt")
originalImg = System.Drawing.Image.FromStream(myFileStream)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2019 Snitz™ Communications Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000 Version 3.4.07