0

Im having issue creating thumbnail image based on original image that is already stored on disk.

App throws: 'A generic error occurred in GDI+.'

Here is my code:

public static string UploadThumbnailImage(string existingImagePath)
{
   var fileName = "thumb_" + Guid.NewGuid().ToString() + ".jpg";
   var filePath = Path.Combine("Uploads/Images", fileName);

   originalFilePath = Path.GetFullPath(existingImagePath);

   using (var stream = File.Create(filePath))
   {
        // getting original image that I want create thumbnail image based on
        var image = Image.FromFile(originalFilePath);
        // created thumbnail image based on original image
        var thumb = image.GetThumbnailImage(150, 150, () => false, IntPtr.Zero);
        // saving thumbnail image - and this throws
        // 'A generic error occurred in GDI+.'
        thumb.Save(filePath);
    }

    string thumbnailImageUrl = string.Format("/{0}/{1}", "Uploads/Images", fileName);
    return thumbnailImageUrl;
}
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • `System.Drawing` is not for ASP.NET Core. Read the linked thread to learn the alternatives. – Lex Li Nov 08 '22 at 06:46

1 Answers1

3

Looks like you are trying to open two different streams to the same file.

Change thumb.Save(filePath); to something like thumb.Save(stream);

Or just get rid of the wrapped File.Create

barrett777
  • 282
  • 3
  • 14