Solved

Images created using ASP.NET won't open in photoshop

Posted on 2004-09-06
4
807 Views
Last Modified: 2012-08-14
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(UploadedPhoto.PostedFile.FileName); // get current filename
// Save the photo to the web photos directory
UploadedPhoto.PostedFile.SaveAs(Path.Combine(serverDestDir, "Photo" + newPhotoID + fileName.ToLower().Substring(fileName.Length - 4, 4)));

// Create an image object holding the photo so we can work on it
System.Drawing.Image newPhoto = System.Drawing.Image.FromFile(Path.Combine(serverDestDir, "Photo" + newPhotoID + fileName.ToLower().Substring(fileName.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(thumbDestDir + "thumb" + newPhotoID + fileName.ToLower().Substring(fileName.Length - 4, 4)));

// Dispose of newPhoto image object now that thumbnail is created
newPhoto.Dispose();


void CreateThumbnail(System.Drawing.Image Photo, int width, int height, string ThumbPath)
{
      // Callback - does nothing, but required for compatibility
      System.Drawing.Image.GetThumbnailImageAbort tnCallBack = new System.Drawing.Image.GetThumbnailImageAbort(tnCallbackMethod);

      // Get the thumbnail image - witdth, height.  3rd and 4th params are not used, but needed for compatibility
      System.Drawing.Image ThumbnailImage = Photo.GetThumbnailImage(width, height, tnCallBack, IntPtr.Zero);
      
      // Save the thumbnail
      ThumbnailImage.Save(ThumbPath);
      
      // 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.
0
Comment
Question by:mrichmon
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
Thogek earned 500 total points
ID: 11993001
Have you tried replacing
    ThumbnailImage.Save(ThumbPath);
with
    ThumbnailImage.Save(ThumbPath, System.Drawing.Imaging.ImageFormat.Jpeg);
0
 
LVL 35

Author Comment

by:mrichmon
ID: 12045404
No, I will try that.

Thanks for the suggestion - I will post the results...
0
 
LVL 35

Author Comment

by:mrichmon
ID: 12050878
Worked perfectly thanks!
0
 

Expert Comment

by:daz_oldham
ID: 24503799
I'm about to test this too as I have the same problem... fingers crossed :)  

daz@redstarcreative.co.uk
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now