protected void UploadFile(Object s, EventArgs e)
{
// First we check to see if the user has selected a file
if (fileUpload.HasFile)
{
// Find the fileUpload control
string filename = fileUpload.FileName;
// Check if the directory we want the image uploaded to actually exists or not
if (!Directory.Exists(MapPath(@"Uploaded-Files")))
{
// If it doesn't then we just create it before going any further
Directory.CreateDirectory(MapPath(@"Uploaded-Files"));
}
// Specify the upload directory
string directory = Server.MapPath(@"Uploaded-Files\");
// Create a bitmap of the content of the fileUpload control in memory
Bitmap originalBMP = new Bitmap(fileUpload.FileContent);
// Calculate the new image dimensions
int origWidth = originalBMP.Width;
int origHeight = originalBMP.Height;
int sngRatio = origWidth / origHeight;
int newWidth = 100;
int newHeight = newWidth / sngRatio;
// Create a new bitmap which will hold the previous resized bitmap
Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
// Create a graphic based on the new bitmap
Graphics oGraphics = Graphics.FromImage(newBMP);
// Set the properties for the new graphic file
oGraphics.SmoothingMode = SmoothingMode.AntiAlias;oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the new graphic based on the resized bitmap
oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
// Save the new graphic file to the server
newBMP.Save(directory + "tn_" + filename);
// Once finished with the bitmap objects, we deallocate them.
originalBMP.Dispose();
newBMP.Dispose();
oGraphics.Dispose();
// Write a message to inform the user all is OK
label.Text = "File Name: <b style='color: red;'>" + filename + "</b><br>";
label.Text += "Content Type: <b style='color: red;'>" + fileUpload.PostedFile.ContentType + "</b><br>";
label.Text += "File Size: <b style='color: red;'>" + fileUpload.PostedFile.ContentLength.ToString() + "</b>";
// Display the image to the user
Image1.Visible = true;
Image1.ImageUrl = @"/Uploaded-Files/tn_" + filename;
}
else
{
label.Text = "No file uploaded!";
}
}
Member
41 Points
269 Posts
How resize image when upload in asp.net 4.0 ?
Apr 13, 2012 09:45 AM|cno_kh|LINK
How resize image when upload in asp.net 4.0 ?
Do you have function coding resize ?
All-Star
114593 Points
18503 Posts
MVP
Re: How resize image when upload in asp.net 4.0 ?
Apr 13, 2012 10:39 AM|Rion Williams|LINK
The following StackOverflow thread focuses on resizing images upon uploading in .NET:
.NET Images Uploading and Resizing
There are several different methods that are used within that discussion - as there are many ways to solve this problem.
Mark McDonnell has written a blog post on the same topic that might also be of some help to you:
Resizing an Image Prior to Uploading to Server
which has the following code:
Member
41 Points
269 Posts
Re: How resize image when upload in asp.net 4.0 ?
Apr 13, 2012 11:35 PM|cno_kh|LINK
Do you have VB.NET CODING?
Star
7768 Points
1794 Posts
Re: How resize image when upload in asp.net 4.0 ?
Apr 14, 2012 04:14 AM|amit.jain|LINK
refer resize image before upload to database
amiT jaiN
ASP.NET C# VB Articles And Code Examples