470,815 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Resize images ASP.NET


I use an ASP.NET script on my website to upload images (complete script
is listed at the end of this message).

Now, I want to limit the size (in width and height, but also in bytes).
For example, if the image is max. 400 pixel in height or max. 400 pixel
width or max. 100 Kb. The image is not uploaded. How can I do this?

Additionally, I also want to resize the images to a valid size when
they are to big than the specified size (max. 400 pixel in height or
max. 400 pixel width or max. 100 Kb). Is there a function or way to do
this in ASP.NET?

Finally, I want to add a text under each image (like: "This image
copyrighted by ..."). This text should be part of the image, so not
just put a HTML text under it.

I know that you can buy components to exactly do want I want to do. For

The problem is that I need to do it myself because my provider does not
allow me to install DLL-files on his server.

******************* START UPLOAD.ASPX FILE **********************
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
Sub SubmitButton_Click(Source As Object, e As EventArgs)
If Not (oFile.PostedFile Is Nothing) Then
'Declare variables
Dim strFileName As String
Dim strFilePath As String
Dim strFolder As String
Dim strPicType As String
Dim strPicSize As String
'Set Upload Folder
strFolder = "D:\Inetpub\wwwroot\upload\"
'Get the name of the file that is posted
strFileName = oFile.PostedFile.FileName
strFileName = Path.GetFileName(strFileName)
strFilePath = strFolder & strFileName
'Validate that it is an image file
strPicType = oFile.PostedFile.ContentType
strPicSize = oFile.PostedFile.ContentLength
If (strPicType="image/jpeg" or strPicType="image/gif" or
strPicType="image/pjpeg" or strPicType="image/bmp") then
'Save file
Span.InnerHtml = "De afbeelding werd succesvol
bewaard!<BR><IMG SRC='http://www.a-random-website.co**m/upload/" &
strFileName & "'><BR>" & strPicSize & "."
Span.InnerHtml = "De afbeelding is niet van het formaat

End If
Catch ex As Exception
Span.InnerHtml = "Er is een fout opgetreden bij het
bewaren van de afbeelding. Probeer het eens opnieuw."
End Try
End If
End Sub
<FONT FACE="Trebuchet MS, Arial, Helvetica, Verdana" SIZE="2"
<form runat="server" enctype="multipart/form-data">
Selecteer de afbeelding die je wenst toe te voegen:<br />
<input type="file" id="oFile" runat="Server"><br/>
<input type="submit" id="Submit" runat="Server"
value="Upload File" OnServerClick="SubmitButton_Cl**ick">
<span id="Span" runat="Server" />
******************* END UPLOAD.ASPX FILE **********************


Nov 19 '05 #1
2 2029
Concerning limiting the file size, there are many threads on this
newsgroup that discuss this topic. Do a search of the group for the
words "upload file size". Methods of handling this include setting a
max request size in web.config and creating an httphandler that
intercepts the request and does some processing to determine if the
file is acceptable.

If you use an httphandler, you can check the
HttpContext.Request.ContentLength in an event handler that handles the
app.PreRequestHandlerExecute event. If the size is greater than a
preset limit, you can then abort the request. However, I don't know
how you would determine if the image dimensions in pixels were above a
preset limit unless you actually stored the image on the server, opened
it and obtained its dimensions. You can do this by saving the image to
a temporary location then opening the image as a System.Drawing.Image
as in

Dim objImage As System.Drawing.Image =
Image.FromFile(Server.MapPath(strImagePath), True)

Now you have access to information about the image by checking values
of properties such as


Check your documentation on System.Drawing.Image for more information.

Using System.Drawing you can modify the image, place text or graphics
on top of it, combine it with other images, etc. Consult your
documentation for examples on using the various classes within
System.Drawing to create and modify images.

Bill E.
Hollywood, FL

Nov 19 '05 #2
Thank you very, very much! This is what I was looking for!

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by J'son | last post: by
14 posts views Thread by Rudy | last post: by
6 posts views Thread by Bob Bedford | last post: by
2 posts views Thread by Tim Arnold | last post: by
1 post views Thread by Starance | last post: by
3 posts views Thread by =?Utf-8?B?UiBSZXllcw==?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.