猿问

尝试调整图像大小时获取externalexception。我该如何解决?

我正在尝试通过调整大小将位图图像转换为gif图像。例如,如果位图是3MB,那么我希望gif在转换后出现,例如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))

                

                }

            }

        }

    }

}

原始图像是位图,我想调整它们的大小并将其另存为gif。

红糖糍粑
浏览 117回答 1
1回答

开心每一天1111

要保存图片,您需要指定文件名而不是目录img.Save(@"C:\[New&nbsp;File&nbsp;Name]",&nbsp;ImageFormat.Gif);
随时随地看视频慕课网APP
我要回答