Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Aug 27, 2011 04:45 PM by smcoxon
Aug 23, 2011 10:00 PM|LINK
You can see a C# version of my code further back in this post on page 3.
Aug 24, 2011 11:23 AM|LINK
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:
Dim filePath As String = "~/Upload/" & upName & ".jpg"
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
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!
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:
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)
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
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!
Aug 27, 2011 03:48 PM|LINK
I FIGURED IT OUT!! I had to change this
string filePath = "~/AcftInspPhotos/" + upName + ".jpg";
string filePath = "~\\HR\\AcftInspPhotos\\" + upName + ".jpg";
Even though both the running program AND the upload folder are already under the "HR" folder!
Aug 27, 2011 04:45 PM|LINK
Excellent, glad you got it working for your website.