Resize images ASP.NET

I

IkBenHet

Hello,

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
example: http://64.39.14.230/ig-asp/common/abcphotoadmin.asp

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
Try
'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
oFile.PostedFile.SaveAs(strFil­­ePath)
Span.InnerHtml = "De afbeelding werd succesvol
bewaard!<BR><IMG SRC='http://www.a-random-website.co­­m/upload/" &
strFileName & "'><BR>" & strPicSize & "."
Else
Span.InnerHtml = "De afbeelding is niet van het formaat

GIF, JPG,
JPEG of BMP!"
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


</script>


<html>
<head>
<title>Title</title>
</head>
<body>
<FONT FACE="Trebuchet MS, Arial, Helvetica, Verdana" SIZE="2"
COLOR="#4E69B0">
<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">
<p>
<span id="Span" runat="Server" />
</form>
</FONT>
</body>
</html>
******************* END UPLOAD.ASPX FILE **********************

Thanks!
 
B

billmiami2

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

PhysicalDimension
PixelFormat
Size

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top