If you look at my code the first thing set up is the canvas size - the size you want the resized image to be. This is usually a landscape orientation with a 4:3 (width:height) aspect ratio. When the original image is reduced, depending on its original dimensions,
it may or may not be an exact 4:3 ratio. Therefore, to position the resized image centrally (horizontal and vertical) on the canvas, the newX and newY values are used as coordinates for the new top left position of the resized image on the canvas. This is
done because you never know what the size and aspect ratio the original image will be. The white lines you see is because the canvas colour is set to white and the resized image is not always exactly the base canvas size, especially if the original image aspect
ratio is to be maintained, as it is in my code to eliminate image distortion, or if a portrait image (3:4) is uploaded, resized and drawn on to the 4:3 canvas.
An alternative approach would be to resize the base canvas size to match the exact dimensions of the resized image for every resized image. The issue with this is you will end up with images of different dimensions which makes your aspx page layout more
of an issue when displaying the resized images.
You can also change the canvas colour if you want to by changing the following line of code to the colour you want:
newGraphic.Clear(Color.White)
For the image types, I'm no graphics expert but I'd convert and save with the correct image type and file extention. Since posting my original code I convert and save all my images as Jpg. To do this change the line of code for the save filePath and the code that saves the image as follows:
Dim filePath As String = "~/Upload/" & upName & ".jpg"
newBmp.Save(MapPath(filePath), Imaging.ImageFormat.Jpeg)
Yours is the closest I've gotten to finding something that works. I have the code in place in my program with necessary name changes (destination folder and FileUpload object). Stepped thru it in debug with great expectations only to find it errored at
this line:
The code looks correct. The error you have could be due to not having write permissions on the directory where you are trying to save the resized image file or maybe you are saving the resized image with the same name as the original file in to the same
directory? Take a look at the following blogs and forum post:
Neither is the case because (a) it the same upload folder the application currently uploads successfully to and (b) I have code before the "save" that checks whether the file name exists.
csPath = Server.MapPath("AcftInspPhotos"); (done in Page_Load)
DirectoryInfo dirInfoEx = new DirectoryInfo(@csPath);
System.IO.FileInfo[] fileNamesEx = dirInfoEx.GetFiles(FileUpload_R1.FileName);
if (fileNamesEx.GetLength(0) == 0)
{
(clipped)
}
Again, I emphasize this is a working program that already uploads aircraft discrepancy photos that were resized enmass prior to the upload. Now my boss wants the user to skip the resize step and be able to select the fullsize photo and my program to resize it as it uploads. So I've just inserted your code in my program. Folder permissions were already in place.
I suspected it is the way the path is being used and I've tried
changing this line
newBmp.Save(MapPath(filePath), System.Drawing.Imaging.ImageFormat.Jpeg);
to this
newBmp.Save(@csPath, System.Drawing.Imaging.ImageFormat.Jpeg);
However, nothing I've tried so far solves this problem. I know it's Saturday but if a lightbulb goes off in your head, please let me know. I will go slowly thru the blog you cited and keep trying stuff. Thanks!
smcoxon
Contributor
5455 Points
948 Posts
Re: Resize Images on upload
Aug 23, 2011 10:00 PM|LINK
Hi bozzo,
You can see a C# version of my code further back in this post on page 3.
Smcoxon
No Gem is ever polished without some friction.
smcoxon
Contributor
5455 Points
948 Posts
Re: Resize Images on upload
Aug 24, 2011 11:23 AM|LINK
Hi TonyLoco23,
If you look at my code the first thing set up is the canvas size - the size you want the resized image to be. This is usually a landscape orientation with a 4:3 (width:height) aspect ratio. When the original image is reduced, depending on its original dimensions, it may or may not be an exact 4:3 ratio. Therefore, to position the resized image centrally (horizontal and vertical) on the canvas, the newX and newY values are used as coordinates for the new top left position of the resized image on the canvas. This is done because you never know what the size and aspect ratio the original image will be. The white lines you see is because the canvas colour is set to white and the resized image is not always exactly the base canvas size, especially if the original image aspect ratio is to be maintained, as it is in my code to eliminate image distortion, or if a portrait image (3:4) is uploaded, resized and drawn on to the 4:3 canvas.
An alternative approach would be to resize the base canvas size to match the exact dimensions of the resized image for every resized image. The issue with this is you will end up with images of different dimensions which makes your aspx page layout more of an issue when displaying the resized images.
You can also change the canvas colour if you want to by changing the following line of code to the colour you want:
For the image types, I'm no graphics expert but I'd convert and save with the correct image type and file extention. Since posting my original code I convert and save all my images as Jpg. To do this change the line of code for the save filePath and the code that saves the image as follows:
Smcoxon
No Gem is ever polished without some friction.
janwane
Member
77 Points
150 Posts
Re: Resize Images on upload
Aug 27, 2011 01:14 PM|LINK
Yours is the closest I've gotten to finding something that works. I have the code in place in my program with necessary name changes (destination folder and FileUpload object). Stepped thru it in debug with great expectations only to find it errored at this line:
with this message: "A generic error occurred in GDI+"
It was a 2MB jpg file, nothing weird. Please help as I have no idea what to do to fix this. Thanks!
smcoxon
Contributor
5455 Points
948 Posts
Re: Resize Images on upload
Aug 27, 2011 02:28 PM|LINK
The code looks correct. The error you have could be due to not having write permissions on the directory where you are trying to save the resized image file or maybe you are saving the resized image with the same name as the original file in to the same directory? Take a look at the following blogs and forum post:
http://weblogs.asp.net/anasghanem/archive/2009/02/28/solving-quot-a-generic-error-occurred-in-gdi-quot-exception.aspx
http://www.blog.vishalon.net/index.php/bitmapsave-a-generic-error-occurred-in-gdi
http://forums.asp.net/t/624305.aspx
Smcoxon
No Gem is ever polished without some friction.
janwane
Member
77 Points
150 Posts
Re: Resize Images on upload
Aug 27, 2011 03:26 PM|LINK
Neither is the case because (a) it the same upload folder the application currently uploads successfully to and (b) I have code before the "save" that checks whether the file name exists.
csPath = Server.MapPath("AcftInspPhotos"); (done in Page_Load)DirectoryInfo dirInfoEx = new DirectoryInfo(@csPath); System.IO.FileInfo[] fileNamesEx = dirInfoEx.GetFiles(FileUpload_R1.FileName); if (fileNamesEx.GetLength(0) == 0) { (clipped) }Again, I emphasize this is a working program that already uploads aircraft discrepancy photos that were resized enmass prior to the upload. Now my boss wants the user to skip the resize step and be able to select the fullsize photo and my program to resize it as it uploads. So I've just inserted your code in my program. Folder permissions were already in place.
I suspected it is the way the path is being used and I've tried
However, nothing I've tried so far solves this problem. I know it's Saturday but if a lightbulb goes off in your head, please let me know. I will go slowly thru the blog you cited and keep trying stuff. Thanks!
janwane
Member
77 Points
150 Posts
Re: Resize Images on upload
Aug 27, 2011 03:48 PM|LINK
I FIGURED IT OUT!! I had to change this
to this
Even though both the running program AND the upload folder are already under the "HR" folder!
smcoxon
Contributor
5455 Points
948 Posts
Re: Resize Images on upload
Aug 27, 2011 04:45 PM|LINK
Excellent, glad you got it working for your website.
Smcoxon
No Gem is ever polished without some friction.