为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
红糖糍粑 2021-05-05 13:16:51
我正在尝试通过调整大小将位图图像转换为gif图像。例如,如果位图是3MB,那么我希望gif在转换后出现,例如500x500using 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。
查看完整描述

1 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

要保存图片,您需要指定文件名而不是目录

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


查看完整回答
反对 回复 2021-05-29
  • 1 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信