Daniel Lip
Daniel Lip

Reputation: 11319

Getting externalexception when trying to resize images. How can I resolve it?

I'm trying to convert bitmaps images to gif images with resizing. For example if the bitmap is 3MB then I want the gif to be after the convertion for example 500x500

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BatchResize
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] array1 = Directory.GetFiles(@"C:\temp\tempVideo\MVI_8977.MOV\Lightning 0 Length 25 [48 - 73]");

            foreach(string image in array1)
            {
                Console.WriteLine("Working on file:  " + image);
                Image img = ResizeImage(image, 500, 500);
                img.Save(@"C:\temp\newimgs\", System.Drawing.Imaging.ImageFormat.Gif);
                img.Dispose();
            }
        }

        //Image Resize Helper Method
        private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
        {
            using (Image originalImage = Image.FromFile(filename))
            {
                //Caluate new Size
                int newWidth = originalImage.Width;
                int newHeight = originalImage.Height;
                double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
                if (aspectRatio <= 1 && originalImage.Width > maxWidth)
                {
                    newWidth = maxWidth;
                    newHeight = (int)Math.Round(newWidth / aspectRatio);
                }
                else if (aspectRatio > 1 && originalImage.Height > maxHeight)
                {
                    newHeight = maxHeight;
                    newWidth = (int)Math.Round(newHeight * aspectRatio);
                }
                Bitmap newImage = new Bitmap(newWidth, newHeight);
                using (Graphics g = Graphics.FromImage(newImage))
                {
                    //--Quality Settings Adjust to fit your application
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
                    return newImage;
                }
            }
        }
    }
}

The original images are Bitmaps and I want to resize them and save them as gifs.

The exception is on the line:

img.Save(@"C:\temp\newimgs\", System.Drawing.Imaging.ImageFormat.Gif);

System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.'

System.Runtime.InteropServices.ExternalException occurred
  HResult=0x80004005
  Message=A generic error occurred in GDI+.
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(String filename, ImageFormat format)
   at BatchResize.Program.Main(String[] args) in c:\users\ccc\source\repos\BatchResize\BatchResize\Program.cs:line 21

Upvotes: 0

Views: 63

Answers (1)

user7148391
user7148391

Reputation:

To save the Img, You need to specify a file name not the dir

img.Save(@"C:\[New File Name]", ImageFormat.Gif);

Upvotes: 1

Related Questions