Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 08, 2012 03:40 AM by MahadTECH
Member
243 Points
484 Posts
Aug 06, 2012 05:19 AM|LINK
Hi,
I have 4 file upload controls on my aspx and the code below collects all files on the page and uploads to directory.
What I want to happen is that this code automatically resets the Image Height ONLY to 350px for each one??
could you add code to below to do this very helpful. Thank you. HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile file = uploadedFiles[i]; string fileExt = Path.GetExtension(file.FileName).ToLower(); string fileName = Path.GetFileName(file.FileName); if (fileName != string.Empty) { try { if (fileExt == ".jpg" || fileExt == ".jpeg" || fileExt == ".JPEG" || fileExt == ".JPG") if (!File.Exists(Server.MapPath("/Over8/" + newUser))) { Directory.CreateDirectory(Server.MapPath("/Over8/")); const string BrochureDirectory = "/Over8/"; string brochurePath = BrochureDirectory + fileName; string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(file.FileName); int iteration = 1; while (System.IO.File.Exists(Server.MapPath(brochurePath))) { brochurePath = string.Concat(BrochureDirectory, fileNameWithoutExtension, "-", iteration, ".jpg"); iteration++; } file.SaveAs(Server.MapPath(brochurePath)); FileUploadObj.InsertParameters["ImageUrl"].DefaultValue = brochurePath.ToString(); FileUploadObj.InsertParameters["ModelId"].DefaultValue = newUserId.ToString(); FileUploadObj.Insert(); } else { } } catch (Exception ex) { throw ex; } } }
Participant
1058 Points
144 Posts
Aug 06, 2012 05:33 AM|LINK
Try reading these links
http://www.4guysfromrolla.com/articles/012203-1.aspx
http://www.glennjones.net/2005/10/high-quality-dynamically-resized-images-with-dot-net/
Aug 06, 2012 07:58 PM|LINK
hi
thanks will have a look but a solution in c -sharp would be best for me - and if possible using existing code.
best
Star
8723 Points
1677 Posts
Aug 06, 2012 10:15 PM|LINK
This works for me in a lot projects:
using System.Drawing; using System.IO;
protected void Button1_Click(object sender, EventArgs e) { resizeImage(); } public void resizeImage() { string fName, trgDir; string MyImageExt = Path.GetExtension(FileUpload1.PostedFile.FileName); string MyNewGuid = Guid.NewGuid().ToString(); fName = FileUpload1.FileName; fName = fName.ToLower(); trgDir = Server.MapPath("~/Uploads/"); Bitmap origBMP = new Bitmap(FileUpload1.FileContent); string temp = "450"; int max = int.Parse(temp); if (origBMP.Width <= max) { origBMP.Save(trgDir + MyNewGuid + MyImageExt); return; } else { int origWidth = origBMP.Width; int origHeight = origBMP.Height; int newWidth = max; int newHeight = newWidth * origHeight / origWidth; Bitmap newBMP = new Bitmap(origBMP, newWidth, newHeight); Graphics objGra = Graphics.FromImage(newBMP); objGra.DrawImage(origBMP, new Rectangle(0, 0, newBMP.Width, newBMP.Height), 0, 0, origWidth, origHeight, GraphicsUnit.Pixel); objGra.Dispose(); newBMP.Save(trgDir + MyNewGuid + MyImageExt); } Bitmap origBMPTH = new Bitmap(FileUpload1.FileContent); string tempTH = "250"; int maxTH = int.Parse(tempTH); if (origBMPTH.Width <= maxTH) { origBMPTH.Save(trgDir + MyNewGuid + "TH" + MyImageExt); return; } else { int origWidthTH = origBMPTH.Width; int origHeightTH = origBMPTH.Height; int newWidthTH = maxTH; int newHeightTH = newWidthTH * origHeightTH / origWidthTH; Bitmap newBMPTH = new Bitmap(origBMPTH, newWidthTH, newHeightTH); Graphics objGraTH = Graphics.FromImage(newBMPTH); objGraTH.DrawImage(origBMPTH, new Rectangle(0, 0, newBMPTH.Width, newBMPTH.Height), 0, 0, origWidthTH, origHeightTH, GraphicsUnit.Pixel); objGraTH.Dispose(); newBMPTH.Save(trgDir + MyNewGuid + "TH" + MyImageExt); } }
Let me know if you have any issue!
Aug 07, 2012 04:07 AM|LINK
Hi Primillo,
Thanks for your detailed reply.
Could you confirm / highlight in your code where I would enter the new height value of 350px - as the code is not that familiar.
also will it work out the width accordingly to the height which would be most ideal?
thanks
Prontonet
Aug 07, 2012 04:44 AM|LINK
The above code generade two images with new names.
See:
string temp = "450"; // max Width
And:
string tempTH = "250"; // max Width
Try the code, you just need the fileupload control, the button and some big image files.
You can modify the code depending on your own needs
Aug 07, 2012 04:48 AM|LINK
And create the folder Uploads in your app root.
Aug 07, 2012 01:24 PM|LINK
thanks again - so the folder uploads can go in the aspx code behind page? within
a creater User Event? or button on click event?
also will this manipulate just the height and wort the width accordingly?
Aug 07, 2012 03:57 PM|LINK
Hi
You can create a folder for each user, but that's not a good practice. Don' t go there.
Save the images in the same folder Uploads, inside you can have more folders ...
Uploads UserImages ClassifiedsImages BlogImages
Uploads
UserImages
ClassifiedsImages
BlogImages
In the following code line specify where to save the images:
trgDir = Server.MapPath("~/Uploads/");
Aug 07, 2012 11:40 PM|LINK
hi Primillo,
great thanks will take your advice.
finally does the code apply to onlyt 1 file upload because there are 4 on my page and the code I use collects all the files
in each control as you can see.
Can i somehow integrate it with my code so all 4 are resized.
prontonet
Member
243 Points
484 Posts
Trying to ReSize Each Uploaded Image to 350px ??
Aug 06, 2012 05:19 AM|LINK
Hi,
I have 4 file upload controls on my aspx and the code below collects all files on the page and uploads to directory.
What I want to happen is that this code automatically resets the Image Height ONLY to 350px for each one??
could you add code to below to do this very helpful. Thank you.
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile file = uploadedFiles[i];
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);
if (fileName != string.Empty)
{
try
{
if (fileExt == ".jpg" || fileExt == ".jpeg" || fileExt == ".JPEG" || fileExt == ".JPG")
if (!File.Exists(Server.MapPath("/Over8/" + newUser)))
{
Directory.CreateDirectory(Server.MapPath("/Over8/"));
const string BrochureDirectory = "/Over8/";
string brochurePath = BrochureDirectory + fileName;
string fileNameWithoutExtension =
System.IO.Path.GetFileNameWithoutExtension(file.FileName);
int iteration = 1;
while (System.IO.File.Exists(Server.MapPath(brochurePath)))
{
brochurePath = string.Concat(BrochureDirectory, fileNameWithoutExtension,
"-", iteration, ".jpg");
iteration++;
}
file.SaveAs(Server.MapPath(brochurePath));
FileUploadObj.InsertParameters["ImageUrl"].DefaultValue = brochurePath.ToString();
FileUploadObj.InsertParameters["ModelId"].DefaultValue = newUserId.ToString();
FileUploadObj.Insert();
}
else
{
}
}
catch (Exception ex)
{
throw ex;
}
}
}
sanddy19
Participant
1058 Points
144 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 06, 2012 05:33 AM|LINK
Try reading these links
http://www.4guysfromrolla.com/articles/012203-1.aspx
http://www.glennjones.net/2005/10/high-quality-dynamically-resized-images-with-dot-net/
Saurabh Nandu
Systenics Solutions
My Blog
prontonet
Member
243 Points
484 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 06, 2012 07:58 PM|LINK
hi
thanks will have a look but a solution in c -sharp would be best for me - and if possible using existing code.
best
Primillo
Star
8723 Points
1677 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 06, 2012 10:15 PM|LINK
This works for me in a lot projects:
protected void Button1_Click(object sender, EventArgs e) { resizeImage(); } public void resizeImage() { string fName, trgDir; string MyImageExt = Path.GetExtension(FileUpload1.PostedFile.FileName); string MyNewGuid = Guid.NewGuid().ToString(); fName = FileUpload1.FileName; fName = fName.ToLower(); trgDir = Server.MapPath("~/Uploads/"); Bitmap origBMP = new Bitmap(FileUpload1.FileContent); string temp = "450"; int max = int.Parse(temp); if (origBMP.Width <= max) { origBMP.Save(trgDir + MyNewGuid + MyImageExt); return; } else { int origWidth = origBMP.Width; int origHeight = origBMP.Height; int newWidth = max; int newHeight = newWidth * origHeight / origWidth; Bitmap newBMP = new Bitmap(origBMP, newWidth, newHeight); Graphics objGra = Graphics.FromImage(newBMP); objGra.DrawImage(origBMP, new Rectangle(0, 0, newBMP.Width, newBMP.Height), 0, 0, origWidth, origHeight, GraphicsUnit.Pixel); objGra.Dispose(); newBMP.Save(trgDir + MyNewGuid + MyImageExt); } Bitmap origBMPTH = new Bitmap(FileUpload1.FileContent); string tempTH = "250"; int maxTH = int.Parse(tempTH); if (origBMPTH.Width <= maxTH) { origBMPTH.Save(trgDir + MyNewGuid + "TH" + MyImageExt); return; } else { int origWidthTH = origBMPTH.Width; int origHeightTH = origBMPTH.Height; int newWidthTH = maxTH; int newHeightTH = newWidthTH * origHeightTH / origWidthTH; Bitmap newBMPTH = new Bitmap(origBMPTH, newWidthTH, newHeightTH); Graphics objGraTH = Graphics.FromImage(newBMPTH); objGraTH.DrawImage(origBMPTH, new Rectangle(0, 0, newBMPTH.Width, newBMPTH.Height), 0, 0, origWidthTH, origHeightTH, GraphicsUnit.Pixel); objGraTH.Dispose(); newBMPTH.Save(trgDir + MyNewGuid + "TH" + MyImageExt); } }Let me know if you have any issue!
Primillo
http://www.facebook.com/programandopuntonet
prontonet
Member
243 Points
484 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 07, 2012 04:07 AM|LINK
Hi Primillo,
Thanks for your detailed reply.
Could you confirm / highlight in your code where I would enter the new height value of 350px - as the code is not that familiar.
also will it work out the width accordingly to the height which would be most ideal?
thanks
Prontonet
Primillo
Star
8723 Points
1677 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 07, 2012 04:44 AM|LINK
The above code generade two images with new names.
See:
And:
Try the code, you just need the fileupload control, the button and some big image files.
You can modify the code depending on your own needs
Primillo
http://www.facebook.com/programandopuntonet
Primillo
Star
8723 Points
1677 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 07, 2012 04:48 AM|LINK
And create the folder Uploads in your app root.
Primillo
http://www.facebook.com/programandopuntonet
prontonet
Member
243 Points
484 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 07, 2012 01:24 PM|LINK
thanks again - so the folder uploads can go in the aspx code behind page? within
a creater User Event? or button on click event?
also will this manipulate just the height and wort the width accordingly?
Primillo
Star
8723 Points
1677 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 07, 2012 03:57 PM|LINK
Hi
You can create a folder for each user, but that's not a good practice. Don' t go there.
Save the images in the same folder Uploads, inside you can have more folders ...
In the following code line specify where to save the images:
trgDir = Server.MapPath("~/Uploads/");Primillo
http://www.facebook.com/programandopuntonet
prontonet
Member
243 Points
484 Posts
Re: Trying to ReSize Each Uploaded Image to 350px ??
Aug 07, 2012 11:40 PM|LINK
hi Primillo,
great thanks will take your advice.
finally does the code apply to onlyt 1 file upload because there are 4 on my page and the code I use collects all the files
in each control as you can see.
Can i somehow integrate it with my code so all 4 are resized.
thanks
Prontonet