LazZiya / ImageResize

Image resizing tool for .Net applications with ability to add text/image watermark, Supports animated images as well.
http://docs.ziyad.info
MIT License
66 stars 16 forks source link

System.Runtime.InteropServices.ExternalException #5

Closed Firas-Shrourou closed 4 years ago

Firas-Shrourou commented 4 years ago

A problem when saving the scaled image to memory stream

            using (var stream = FileUpload1.PostedFile.InputStream)
            {
                using (var img = System.Drawing.Image.FromStream(stream))
                {
                    using (var scaledImage = ImageResize.ScaleAndCrop(img, 400, 400))
                    {
                        using (var ms = new MemoryStream())
                        {
                            scaledImage.Save(ms, ImageFormat.Jpeg);
                            using (ms)
                            {
                                Int64 Image_Size = ms.Length;
                                ms.Position = 0;
                                var task = new FirebaseStorage(FirebaseBucket)
                                 .Child(FirebaseImagesFolder)
                                 .Child(ImageFileNamePreFix + Selected_Restaurant_ID + Next_Image_Version + ImageFileNameSufFix)
                                 .PutAsync(ms);
                                var downloadUrl = await task;
                                System.Diagnostics.Debug.WriteLine($"download URL: {downloadUrl}");
                                UpdateItemRecordWithImage(downloadUrl, Image_Size);
                            }
                        }
                    }
                }
            }
LazZiya commented 4 years ago

Just create a new bitmap from the scaled image and use it for the memory stream.

Additionally, in order to support more image formats replace ImageFormat.Jpeg with scaledImage.RawFormat:

using (var ms = new MemoryStream())
{
    var bm = new Bitmap(scaledImage);
    bm.Save(ms, scaledImage.RawFormat);
    using (ms)
    {
        // ...
    }
    bm.Dispose();
}
LazZiya commented 4 years ago

I'll close this issue since I think the problem is solved, feel free to reopen if you need more help,

Regards, Ziya