mrichmon
asked on
Images created using ASP.NET won't open in photoshop
I am using asp.net C# to generate thumbnails of photos that I upload.
With any photo that is created by ASP.NET - the following happens:
1) The thumbnails display fine on the web in both IE and and Firefox when show with an img tag
2) If I try to access the thumbnail directly by URL in IE then it shows up as blank.
3) If I save the images to my computer - or access them directly from the server
a) In photoshop I get the following error :
Could not open "thumb23.jpg" because an unknown or invalid JPEG marker type is found
b) In ACDSee it opens fine.
c) In Microsoft Paint - it opens fine
d) In Microsoft Phot Editor It opens fine
e) Doing "Open With" Internet Explorer - it produces a broken image icon
f) Opening directly in Firefox works
Any ideas on what is causing the invalid JPEG marker and how to fix this problem (i.e. how to stop it from occuring?)
Here is the code I am using :
string fileName = Path.GetFileName(UploadedP hoto.Poste dFile.File Name); // get current filename
// Save the photo to the web photos directory
UploadedPhoto.PostedFile.S aveAs(Path .Combine(s erverDestD ir, "Photo" + newPhotoID + fileName.ToLower().Substri ng(fileNam e.Length - 4, 4)));
// Create an image object holding the photo so we can work on it
System.Drawing.Image newPhoto = System.Drawing.Image.FromF ile(Path.C ombine(ser verDestDir , "Photo" + newPhotoID + fileName.ToLower().Substri ng(fileNam e.Length - 4, 4)));
// Determine the thumbnail width and height
int newHeight = 75; // fixed
double tempWidth = newPhoto.Width * ((double)newHeight / (double)newPhoto.Height);
int newWidth = (int)tempWidth; // proportional based on image size
// Now create the thumbnail
CreateThumbnail(newPhoto, newWidth, newHeight, Server.MapPath(thumbDestDi r + "thumb" + newPhotoID + fileName.ToLower().Substri ng(fileNam e.Length - 4, 4)));
// Dispose of newPhoto image object now that thumbnail is created
newPhoto.Dispose();
void CreateThumbnail(System.Dra wing.Image Photo, int width, int height, string ThumbPath)
{
// Callback - does nothing, but required for compatibility
System.Drawing.Image.GetTh umbnailIma geAbort tnCallBack = new System.Drawing.Image.GetTh umbnailIma geAbort(tn CallbackMe thod);
// Get the thumbnail image - witdth, height. 3rd and 4th params are not used, but needed for compatibility
System.Drawing.Image ThumbnailImage = Photo.GetThumbnailImage(wi dth, height, tnCallBack, IntPtr.Zero);
// Save the thumbnail
ThumbnailImage.Save(ThumbP ath);
// Dispose of Thumbnail Image object
ThumbnailImage.Dispose();
}
// Must be called when creating a thumbnail, but not used
bool tnCallbackMethod()
{
return false;
}
Note that in the above code the photo itself will have no error since it is not being created on the fly, but the thumbnail will.
With any photo that is created by ASP.NET - the following happens:
1) The thumbnails display fine on the web in both IE and and Firefox when show with an img tag
2) If I try to access the thumbnail directly by URL in IE then it shows up as blank.
3) If I save the images to my computer - or access them directly from the server
a) In photoshop I get the following error :
Could not open "thumb23.jpg" because an unknown or invalid JPEG marker type is found
b) In ACDSee it opens fine.
c) In Microsoft Paint - it opens fine
d) In Microsoft Phot Editor It opens fine
e) Doing "Open With" Internet Explorer - it produces a broken image icon
f) Opening directly in Firefox works
Any ideas on what is causing the invalid JPEG marker and how to fix this problem (i.e. how to stop it from occuring?)
Here is the code I am using :
string fileName = Path.GetFileName(UploadedP
// Save the photo to the web photos directory
UploadedPhoto.PostedFile.S
// Create an image object holding the photo so we can work on it
System.Drawing.Image newPhoto = System.Drawing.Image.FromF
// Determine the thumbnail width and height
int newHeight = 75; // fixed
double tempWidth = newPhoto.Width * ((double)newHeight / (double)newPhoto.Height);
int newWidth = (int)tempWidth; // proportional based on image size
// Now create the thumbnail
CreateThumbnail(newPhoto, newWidth, newHeight, Server.MapPath(thumbDestDi
// Dispose of newPhoto image object now that thumbnail is created
newPhoto.Dispose();
void CreateThumbnail(System.Dra
{
// Callback - does nothing, but required for compatibility
System.Drawing.Image.GetTh
// Get the thumbnail image - witdth, height. 3rd and 4th params are not used, but needed for compatibility
System.Drawing.Image ThumbnailImage = Photo.GetThumbnailImage(wi
// Save the thumbnail
ThumbnailImage.Save(ThumbP
// Dispose of Thumbnail Image object
ThumbnailImage.Dispose();
}
// Must be called when creating a thumbnail, but not used
bool tnCallbackMethod()
{
return false;
}
Note that in the above code the photo itself will have no error since it is not being created on the fly, but the thumbnail will.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Worked perfectly thanks!
I'm about to test this too as I have the same problem... fingers crossed :)
daz@redstarcreative.co.uk
daz@redstarcreative.co.uk
ASKER
Thanks for the suggestion - I will post the results...