Torna al Thread
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System;
partial class _Default : System.Web.UI.Page
{
protected void BtnUpload_Click(object sender, System.EventArgs e)
{
const int bmpW = 300;
//New image canvas width
const int bmpH = 226;
//New Image canvas height
if ((FileUpload1.HasFile))
{
lblError.Text = "";
//Check to make sure the file to upload has a picture file format extention
if ((CheckFileType(FileUpload1.FileName)))
{
int newWidth = bmpW;
int newHeight = bmpH;
//Use the uploaded filename without the '.' extension
string upName = Mid(FileUpload1.FileName, 1, (InStr(FileUpload1.FileName, ".") - 1));
string filePath = "~/Upload/" + upName + ".png";
//Create a new Bitmap using the uploaded picture as a Stream
//Set the new bitmap resolution to 72 pixels per inch
Bitmap upBmp = Bitmap.FromStream(FileUpload1.FileName);
Bitmap newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
//Get the uploaded image width and height
int upWidth = upBmp.Width;
int upHeight = upBmp.Height;
int newX = 0;
int newY = 0;
decimal reDuce;
//Keep the aspect ratio of image the same if not 4:3 and work out the newX and newY positions
//to ensure the image is always in the centre of the canvas vertically and horizontally
//Landscape picture
if (upWidth > upHeight)
{
reDuce = newWidth / upWidth;
//calculate the width percentage reduction as decimal
newHeight = Int(upHeight * reDuce);
//reduce the uploaded image height by the reduce amount
newY = Int((bmpH - newHeight) / 2);
//Position the image centrally down the canvas
newX = 0;
}
//Picture will be full width
//Portrait picture
else if (upWidth < upHeight)
{
reDuce = newHeight / upHeight;
//calculate the height percentage reduction as decimal
newWidth = Int(upWidth * reDuce);
//reduce the uploaded image height by the reduce amount
newX = Int((bmpW - newWidth) / 2);
//Position the image centrally across the canvas
newY = 0;
}
//Picture will be full hieght
//square picture
else if (upWidth == upHeight)
{
reDuce = newHeight / upHeight;
//calculate the height percentage reduction as decimal
newWidth = Int(upWidth * reDuce);
//reduce the uploaded image height by the reduce amount
newX = Int((bmpW - newWidth) / 2);
//Position the image centrally across the canvas
newY = Int((bmpH - newHeight) / 2);
//Position the image centrally down the canvas
}
//Create a new image from the uploaded picture using the Graphics class
//Clear the graphic and set the background colour to white
//Use Antialias and High Quality Bicubic to maintain a good quality picture
//Save the new bitmap image using 'Png' picture format and the calculated canvas positioning
Graphics newGraphic = Graphics.FromImage(newBmp);
try
{
newGraphic.Clear(Color.White);
newGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
newGraphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight);
newBmp.Save(MapPath(filePath), System.Drawing.Imaging.ImageFormat.Png);
//Show the uploaded resized picture in the image control
Image1.ImageUrl = filePath;
Image1.Visible = true;
}
catch (Exception ex)
{
lblError.Text = ex.ToString();
}
finally
{
upBmp.Dispose();
newBmp.Dispose();
newGraphic.Dispose();
}
}
else
{
lblError.Text = "Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png.";
}
}
}
public static string Mid(string param, int startIndex, int length)
{
//start at the specified index in the string ang get N number of
//characters depending on the lenght and assign it to a variable
string result = param.Substring(startIndex, length);
//return the result of the operation
return result;
}
bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case ".gif":
return true;
case ".png":
return true;
case ".jpg":
return true;
case ".jpeg":
return true;
case ".bmp":
return true;
default:
return false;
}
}
}