Wow cool stuff. Maybe I can use this when someone uploads and image and have
it resized if it is to big. I have people that upload a photo to a site that
upload 1meg plus images that arent necessary when the thing only displays
at 250 width..... I wonder id I can incorporate this into the routine I
have? I hope my code helps someone who needs to upload photos and check on
types etc. Its all in VB.net
Here is how I do it now.
'FILE UPLOAD CODE ------------
Dim MyPath, MyName As String
' Display the names in C:\ that represent directories.
MyPath = "D:\Websites\images\Staff" ' Set the
path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
If MyName = "" Then ' The folder is not there & to be created
MkDir("D:\Websites\images\Staff\") 'Folder created
'Span2.InnerHtml = "A Folder (D:\Websites\images\Staff) is
created at the Page_Load"
End If
'FILE UPLOAD CODE ------------
Dim strFileName As String
strFileName = MyFile.PostedFile.FileName
'Response.Write("strFileName: " & strFileName & "<BR>")
Dim c As String = System.IO.Path.GetFileName(strFileName) ' only the
attched file name not its path
'Response.Write("C: " & c & "<BR>")
'Response.Write("D:\Websites\images\Staff\" & "<B>" & c &
"</B><BR>")
' Let us Save uploaded file to server at C:\ServerFolder\
'Change name of the file to the username + Photo#
Dim strSaveFileName, strPhotoNumber, strError
strPhotoNumber = 1
strSaveFileName = Session("Username") & "_" & strPhotoNumber
'---------------------------------------------------------
'This uses original filename
'strSaveFileName = c
'Response.Write("strSaveFileName: " & strSaveFileName & "<BR>")
Dim PicType
PicType = MyFile.PostedFile.ContentType.ToString()
If (PicType = "image/jpeg" Or PicType = "image/gif" Or PicType =
"image/pjpeg" Or PicType = "image/bmp") Then
Try
If PicType = "image/jpeg" Then
strSaveFileName = strSaveFileName & ".jpeg"
ElseIf PicType = "image/pjpeg" Then
strSaveFileName = strSaveFileName & ".jpeg"
ElseIf PicType = "image/gif" Then
strSaveFileName = strSaveFileName & ".gif"
ElseIf PicType = "image/bmp" Then
strSaveFileName = strSaveFileName & ".bmp"
ElseIf PicType = "" Then
strError = "No File To Upload"
Else
strError = "Invalid FilyType"
End If
MyFile.PostedFile.SaveAs("D:\Websites\images\Staff\" +
strSaveFileName)
-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Matías Niño
ListMail
Sent: Thursday, November 10, 2005 9:13 PM
To: [email protected]
Subject: RE: [AspNetAnyQuestionIsOk] Resize Images
For what it's worth, here's the function my picture manager app uses to
process uploaded images.
Summary: The parameter is the full path to the uploaded image. The function
creates the following:
1) A copy of the image (known as the SOURCE IMAGE)
2) A "base" image (basically a mid-size image no greater than 500x428
pixel dimensions)
3) A thumbnail (no greater than 150x150 pixel dimensions
Hope it helps!
M.
Public Function ProcessImageFile(ByVal ProcessFilePath As String)
'ORIGINAL IMAGE
Dim ProcessFileName As String = Path.GetFileName(ProcessFilePath)
Dim ProcessImage As System.Drawing.Image
'SOURCE IMAGE
Dim SourceFileName As String = "(SOURCE_" & ImageID & ") " &
ProcessFileName
Dim SourceFilePath As String = SourcePath & SourceFileName
'BASE IMAGE
Dim BaseFileName As String = "(BASE_" & ImageID & ") " &
ProcessFileName
Dim BaseFilePath As String = BasePath & BaseFileName
'THUMB IMAGE
Dim ThumbFileName As String = "(THUMB_" & ImageID & ") " &
ProcessFileName
Dim ThumbFilePath As String = ThumbPath & ThumbFileName
Dim ThumbImage As System.Drawing.Image
'CREATE SOURCE IMAGE
File.Copy(ProcessFilePath, SourceFilePath)
'OPEN PROCESS FILE
ProcessImage = ProcessImage.FromFile(ProcessFilePath)
'CREATE BASE IMAGE
Dim inp As New System.IntPtr
Dim BaseImageWidth As Integer
Dim BaseImageHeight As Integer
Dim BaseImageNewHeight As Integer
BaseImageWidth = ProcessImage.Width
BaseImageHeight = ProcessImage.Height
If BaseImageWidth > 500 Then
BaseImageWidth = 500
BaseImageHeight = ProcessImage.Height * BaseImageWidth /
ProcessImage.Width
End If
If BaseImageHeight > 428 Then
BaseImageNewHeight = 428
BaseImageWidth = BaseImageWidth * BaseImageNewHeight /
BaseImageHeight
BaseImageHeight = 428
End If
Dim BaseImageBitmap As New Bitmap(BaseImageWidth, BaseImageHeight,
ProcessImage.PixelFormat.Format24bppRgb)
Dim BaseImageGraphics As Graphics =
Graphics.FromImage(BaseImageBitmap)
BaseImageGraphics.DrawImage(ProcessImage, 0, 0,
BaseImageBitmap.Width, BaseImageBitmap.Height)
BaseImageBitmap.Save(BaseFilePath, ProcessImage.RawFormat)
BaseImageGraphics.Dispose()
BaseImageGraphics = Nothing
BaseImageBitmap.Dispose()
BaseImageBitmap = Nothing
'CREATE THUMBNAIL
If BaseImageWidth > 150 Then
BaseImageWidth = 150
BaseImageHeight = ProcessImage.Height * BaseImageWidth /
ProcessImage.Width
End If
If BaseImageHeight > 150 Then
BaseImageNewHeight = 150
BaseImageWidth = BaseImageWidth * BaseImageNewHeight /
BaseImageHeight
BaseImageHeight = 150
End If
ThumbImage = ProcessImage.GetThumbnailImage(BaseImageWidth,
BaseImageHeight, Nothing, inp)
ThumbImage.Save(ThumbFilePath, ProcessImage.RawFormat)
'RELEASE FILES
ProcessImage.Dispose()
ProcessImage = Nothing
ThumbImage.Dispose()
ThumbImage = Nothing
End Function
________________________________
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Falls, Travis D
(HTSC, CASD)
Sent: Thursday, November 10, 2005 9:43 AM
To: [email protected]
Subject: [AspNetAnyQuestionIsOk] Resize Images
I would like to write or find an open source a modular in C# that looks at a
directory and takes all the images in it and creates thumbnails, mediums,
and large equivalents. Does anyone have any advice, links, thoughts?
Basically I am tired of resizing images I put up on the web. I want to
build a photo album type of mod but I have seen any that do this simply.
Thanks.
Travis D. Falls | Consultant RAFT.Net IT | 860.547.4070 |
[EMAIL PROTECTED]
*************************************************************************
This communication, including attachments, is
for the exclusive use of addressee and may contain proprietary,
confidential and/or privileged information. If you are not the intended
recipient, any use, copying, disclosure, dissemination or distribution is
strictly prohibited. If you are not the intended recipient, please notify
the sender immediately by return e-mail, delete this communication and
destroy all copies.
*************************************************************************
________________________________
YAHOO! GROUPS LINKS
* Visit your group "AspNetAnyQuestionIsOk
<http://groups.yahoo.com/group/AspNetAnyQuestionIsOk> " on the web.
* To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]
e>
* Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
<http://docs.yahoo.com/info/terms/> .
________________________________
[Non-text portions of this message have been removed]
Yahoo! Groups Links
------------------------ Yahoo! Groups Sponsor --------------------~-->
Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life.
http://us.click.yahoo.com/A77XvD/vlQLAA/TtwFAA/saFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/