473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Resize images ASP.NET

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!

Nov 19 '05 #1
2 2119
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

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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dean | last post by:
First I've must say Im completly new in php scripting What I need to do is upload, resize pictures with path in database Here is theory of it, and plan of doing it Hope somebody can help me...
2
by: rams.kakara | last post by:
hi, My page have background image,on that image have more images and text. My problem is whenever resize browser that images are not resized and also not moved correct place .(i.e look not like...
2
by: J'son | last post by:
Guys, I have built an application for a client that allows people to list their products for sale along with a photo of the product. If the photo is too big, I currently resize it down when the...
14
by: Rudy | last post by:
Hello all! I been trying to get a handle with Images. I have learned alot from the fine people here. So, I also learned that thumbnail images look terrible taken from a digital cam. I know why...
7
by: chad | last post by:
is it just me or does anybody else find the Image.RotateFlip method kind of slow? (I'm comparing to commercial softwares). Same for resizing. I'm using sourceImage =...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
6
by: Bob Bedford | last post by:
Hi all, I've to resize uploaded images with the "imagecopyresampled" but when I've images quite large (common those days) I reach the 16mb limits of the ISP. How can I fix this ? I absolutely...
2
by: Tim Arnold | last post by:
Hi, I'm using the Image module to resize PNG images from 300 to 100dpi for use in HTML pages, but I'm losing some vertical and horizontal lines in the images (usually images of x-y plots). ...
1
by: Starance | last post by:
Well... I'm not sure if this can be fixed, I've tried many ways. But here goes; When you resize your browser window it seems to move my video & photo divs. I want them kept in line with the...
3
by: =?Utf-8?B?UiBSZXllcw==?= | last post by:
Hi! This discussion may help other programmers get a better idea of how to save uploaded images through a website. Why? Well currently, I save 3 versions of every uploaded image on my own...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.