Resizing images on upload
-
Hi I currently have a form for people to fill in, with this they need to uplaod a profile picture of themselves. At the moment I have the following code which uploads the image, but i want to be able to resize the image to a certin size. Ive seen resizing help on the web, but most of these seem to be large/thumbnail codes and im getting a bit boggled!
Boolean fileOK = false;
String path = Server.MapPath("~/uploadedconsultantimages/");
if (UploadImage.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(UploadImage.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}if (fileOK) { try { UploadImage.PostedFile.SaveAs(path + UploadImage.FileName); FileUploaded.Text = "File uploaded!"; } catch (Exception ex) { FileUploaded.Text = "File could not be uploaded."; } } else { FileUploaded.Text = "Cannot accept files of this type."; }
Any help would be appreciated. I working in c# .net2:) Thanks Adam
-
Hi I currently have a form for people to fill in, with this they need to uplaod a profile picture of themselves. At the moment I have the following code which uploads the image, but i want to be able to resize the image to a certin size. Ive seen resizing help on the web, but most of these seem to be large/thumbnail codes and im getting a bit boggled!
Boolean fileOK = false;
String path = Server.MapPath("~/uploadedconsultantimages/");
if (UploadImage.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(UploadImage.FileName).ToLower();
String[] allowedExtensions =
{ ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}if (fileOK) { try { UploadImage.PostedFile.SaveAs(path + UploadImage.FileName); FileUploaded.Text = "File uploaded!"; } catch (Exception ex) { FileUploaded.Text = "File could not be uploaded."; } } else { FileUploaded.Text = "Cannot accept files of this type."; }
Any help would be appreciated. I working in c# .net2:) Thanks Adam
http://www.peterprovost.org/archive/2003/05/29/516.aspx[^] I haven't played around with the Upload stuff yet so not sure whether you can access the uploaded file in memory and resize it before saving it. If not, then you simply need to use something like
Boolean fileOK = false; String path = Server.MapPath("~/uploadedconsultantimages/"); if (UploadImage.HasFile) { String fileExtension = System.IO.Path.GetExtension(UploadImage.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } if (fileOK) { try { UploadImage.PostedFile.SaveAs(path + UploadImage.FileName); Bitmap source = Bitmap.FromFile(path + UploadImage.FileName); Bitmap result = new Bitmap(250,250); using(Graphics g = Graphics.FromImage((Image)result); g.DrawImage(source, 0, 0, 250, 250); result.Dispose(); source.Dispose(); result.Save(path + UploadImage.FileName); FileUploaded.Text = "File uploaded!"; } catch (Exception ex) { FileUploaded.Text = "File could not be uploaded."; } } else { FileUploaded.Text = "Cannot accept files of this type."; }
This is untested, but basically "should" resize the uploaded image to 250x250. -
http://www.peterprovost.org/archive/2003/05/29/516.aspx[^] I haven't played around with the Upload stuff yet so not sure whether you can access the uploaded file in memory and resize it before saving it. If not, then you simply need to use something like
Boolean fileOK = false; String path = Server.MapPath("~/uploadedconsultantimages/"); if (UploadImage.HasFile) { String fileExtension = System.IO.Path.GetExtension(UploadImage.FileName).ToLower(); String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" }; for (int i = 0; i < allowedExtensions.Length; i++) { if (fileExtension == allowedExtensions[i]) { fileOK = true; } } } if (fileOK) { try { UploadImage.PostedFile.SaveAs(path + UploadImage.FileName); Bitmap source = Bitmap.FromFile(path + UploadImage.FileName); Bitmap result = new Bitmap(250,250); using(Graphics g = Graphics.FromImage((Image)result); g.DrawImage(source, 0, 0, 250, 250); result.Dispose(); source.Dispose(); result.Save(path + UploadImage.FileName); FileUploaded.Text = "File uploaded!"; } catch (Exception ex) { FileUploaded.Text = "File could not be uploaded."; } } else { FileUploaded.Text = "Cannot accept files of this type."; }
This is untested, but basically "should" resize the uploaded image to 250x250. -
Thanks for this, but im getting errors of the type or namespace could not be found for bitmap and graphics?
Add System.Drawing in your application
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
Add System.Drawing in your application
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
Thanks, im still getting build errors though: Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?)
Bitmap source = Bitmap.FromFile(path + UploadImage.FileName);
Thanks
-
Thanks, im still getting build errors though: Error 1 Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?)
Bitmap source = Bitmap.FromFile(path + UploadImage.FileName);
Thanks
Bitmap source = (Bitmap)Bitmap.FromFile(path + UploadImage.FileName); try this Does it will help you ? Bitmap.FromFile(path + UploadImage.FileName); return type is Image Why you want to convert it into Bitmap What you want to do ?
Thanks and Regards Sandeep If you want something you never had, do something you have never done!
-
Bitmap source = (Bitmap)Bitmap.FromFile(path + UploadImage.FileName); try this Does it will help you ? Bitmap.FromFile(path + UploadImage.FileName); return type is Image Why you want to convert it into Bitmap What you want to do ?
Thanks and Regards Sandeep If you want something you never had, do something you have never done!