//New image target width
const int bmpW = 300;
//New Image target height
const int bmpH = 225;
//Use the uploaded filename for saving without the '.' extension
String upName = fu.FileName.Substring(0, fu.FileName.IndexOf("."));
//Set the save path of the resized image, you will need this directory already created in your web site
string newName = Request.QueryString["TailNum"].ToString() + "_" + upName + ".jpg";
string bigName = Request.QueryString["TailNum"].ToString() + "_" + upName + "Big.jpg";
string filePath = "~\\HR\\AcftInspPhotos\\" + newName;
string filePathBig = "~\\HR\\AcftInspPhotos\\" + bigName;
DirectoryInfo dirInfoEx = new DirectoryInfo(@csPath);
System.IO.FileInfo[] fileNamesEx = dirInfoEx.GetFiles(newName);
if (fileNamesEx.GetLength(0) == 0)
{
//First save full size file
fu.SaveAs(MapPath(filePathBig));
Int32 newWidth = bmpW;
Int32 newHeight = bmpH;
//Create a new Bitmap using the uploaded picture as a Stream
//Set the new bitmap resolution to 72 pixels per inch
Bitmap upBmp = (Bitmap)Bitmap.FromStream(fu.PostedFile.InputStream);
Bitmap newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
newBmp.SetResolution(72, 72);
//Get the uploaded image width and height
Double upWidth = upBmp.Width;
Double upHeight = upBmp.Height;
int newX = 0;
//Set the new top left drawing position on the image canvas
int newY = 0;
Double 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
if (upWidth > upHeight)
{
//Landscape picture
reDuce = newWidth / upWidth;
//calculate the width percentage reduction as decimal
newHeight = ((Int32)(upHeight * reDuce));
//reduce the uploaded image height by the reduce amount
newY = ((Int32)((bmpH - newHeight) / 2));
//Position the image centrally down the canvas
newX = 0;
//Picture will be full width
}
else if (upWidth < upHeight)
{
//Portrait picture
reDuce = newHeight / upHeight;
//calculate the height percentage reduction as decimal
newWidth = ((Int32)(upWidth * reDuce));
//reduce the uploaded image height by the reduce amount
newX = ((Int32)((bmpW - newWidth) / 2));
//Position the image centrally across the canvas
newY = 0;
//Picture will be full hieght
}
else if (upWidth == upHeight)
{
//square picture
reDuce = newHeight / upHeight;
//calculate the height percentage reduction as decimal
newWidth = ((Int32)(upWidth * reDuce));
//reduce the uploaded image height by the reduce amount
newX = ((Int32)((bmpW - newWidth) / 2));
//Position the image centrally across the canvas
newY = ((Int32)((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.Jpeg);
//Show the uploaded resized picture in the image control
//validate file was uploaded successfully
DirectoryInfo dirInfo = new DirectoryInfo(@csPath);
System.IO.FileInfo[] fileNames = dirInfo.GetFiles(newName);
foreach (System.IO.FileInfo fi in fileNames)
{
SDS_DiscrepPhotos.InsertParameters["DefID"].DefaultValue = csDefID;
SDS_DiscrepPhotos.InsertParameters["PhotoFN"].DefaultValue = newName;
SDS_DiscrepPhotos.InsertParameters["PhotoBigFN"].DefaultValue = bigName;
SDS_DiscrepPhotos.InsertParameters["PhotoLegend"].DefaultValue = ((TextBox)gvr.FindControl("TextBox_Legend")).Text;
SDS_DiscrepPhotos.Insert();
bPicsUploaded = true;
}
}
Several months ago I found and used smcoxon's code from
http://forums.asp.net/t/1085119.aspx/1/10 (C# version) successfully on an app for aircraft inspectors to upload photos of discrepancies providing both a thumbnail and a larger picture. However, I'm now getting complaints that the "full size" photos are
too "granular". In inspecting the code (and reviewing ALL the original post) I see the original requirement was for a limited "full size" not the actual full size. I have found 3 lines that have to do with size but I'm afraid to change any of it! My requirement
is this: upload photo file, resize to thumbnail (this size is fine) plus file the original photo file. Exactly what do I change to accomplish that without making a mess?!
janwane
Member
79 Points
152 Posts
Question on changing photo size in smcoxon's code on "Resize Images on upload"
Dec 14, 2012 01:30 PM|LINK
//New image target width const int bmpW = 300; //New Image target height const int bmpH = 225; //Use the uploaded filename for saving without the '.' extension String upName = fu.FileName.Substring(0, fu.FileName.IndexOf(".")); //Set the save path of the resized image, you will need this directory already created in your web site string newName = Request.QueryString["TailNum"].ToString() + "_" + upName + ".jpg"; string bigName = Request.QueryString["TailNum"].ToString() + "_" + upName + "Big.jpg"; string filePath = "~\\HR\\AcftInspPhotos\\" + newName; string filePathBig = "~\\HR\\AcftInspPhotos\\" + bigName; DirectoryInfo dirInfoEx = new DirectoryInfo(@csPath); System.IO.FileInfo[] fileNamesEx = dirInfoEx.GetFiles(newName); if (fileNamesEx.GetLength(0) == 0) { //First save full size file fu.SaveAs(MapPath(filePathBig)); Int32 newWidth = bmpW; Int32 newHeight = bmpH; //Create a new Bitmap using the uploaded picture as a Stream //Set the new bitmap resolution to 72 pixels per inch Bitmap upBmp = (Bitmap)Bitmap.FromStream(fu.PostedFile.InputStream); Bitmap newBmp = new Bitmap(newWidth, newHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); newBmp.SetResolution(72, 72); //Get the uploaded image width and height Double upWidth = upBmp.Width; Double upHeight = upBmp.Height; int newX = 0; //Set the new top left drawing position on the image canvas int newY = 0; Double 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 if (upWidth > upHeight) { //Landscape picture reDuce = newWidth / upWidth; //calculate the width percentage reduction as decimal newHeight = ((Int32)(upHeight * reDuce)); //reduce the uploaded image height by the reduce amount newY = ((Int32)((bmpH - newHeight) / 2)); //Position the image centrally down the canvas newX = 0; //Picture will be full width } else if (upWidth < upHeight) { //Portrait picture reDuce = newHeight / upHeight; //calculate the height percentage reduction as decimal newWidth = ((Int32)(upWidth * reDuce)); //reduce the uploaded image height by the reduce amount newX = ((Int32)((bmpW - newWidth) / 2)); //Position the image centrally across the canvas newY = 0; //Picture will be full hieght } else if (upWidth == upHeight) { //square picture reDuce = newHeight / upHeight; //calculate the height percentage reduction as decimal newWidth = ((Int32)(upWidth * reDuce)); //reduce the uploaded image height by the reduce amount newX = ((Int32)((bmpW - newWidth) / 2)); //Position the image centrally across the canvas newY = ((Int32)((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.Jpeg); //Show the uploaded resized picture in the image control //validate file was uploaded successfully DirectoryInfo dirInfo = new DirectoryInfo(@csPath); System.IO.FileInfo[] fileNames = dirInfo.GetFiles(newName); foreach (System.IO.FileInfo fi in fileNames) { SDS_DiscrepPhotos.InsertParameters["DefID"].DefaultValue = csDefID; SDS_DiscrepPhotos.InsertParameters["PhotoFN"].DefaultValue = newName; SDS_DiscrepPhotos.InsertParameters["PhotoBigFN"].DefaultValue = bigName; SDS_DiscrepPhotos.InsertParameters["PhotoLegend"].DefaultValue = ((TextBox)gvr.FindControl("TextBox_Legend")).Text; SDS_DiscrepPhotos.Insert(); bPicsUploaded = true; } }Several months ago I found and used smcoxon's code from http://forums.asp.net/t/1085119.aspx/1/10 (C# version) successfully on an app for aircraft inspectors to upload photos of discrepancies providing both a thumbnail and a larger picture. However, I'm now getting complaints that the "full size" photos are too "granular". In inspecting the code (and reviewing ALL the original post) I see the original requirement was for a limited "full size" not the actual full size. I have found 3 lines that have to do with size but I'm afraid to change any of it! My requirement is this: upload photo file, resize to thumbnail (this size is fine) plus file the original photo file. Exactly what do I change to accomplish that without making a mess?!