如题,做一个gif图片缩小并保留动画的东东,每次运行到Image的SaveAdd就报GDI+发生一般性错误。代码是参照的别人的:http://blog.csdn.net/ojlovecd/article/details/8296371
- public Form1()
- {
- InitializeComponent();
-
- PictureBox picImg = new PictureBox();
- int width = 100;
- int height = 60;
- picImg.Width = width;
- picImg.Height = height;
- Image res = Image.FromFile("01.gif");
- Image gif = new Bitmap(width, height);
- Image frame = new Bitmap(width, height);
- Graphics g = Graphics.FromImage(gif);
- Rectangle rg = new Rectangle(0, 0, width, height);
- Graphics gFrame = Graphics.FromImage(frame);
- MemoryStream ms = new MemoryStream();
-
- foreach (Guid gd in res.FrameDimensionsList)
- {
- FrameDimension fd = new FrameDimension(gd);
- FrameDimension f = FrameDimension.Time;
- int count = res.GetFrameCount(fd);
- ImageCodecInfo codecInfo = GetEncoder(ImageFormat.Gif);
- System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.SaveFlag;
- EncoderParameters eps = null;
-
- for (int i = 0; i < count; i++)
- {
- res.SelectActiveFrame(f, i);
- if (i == 0)
- {
-
- g.DrawImage(res, rg);
-
- eps = new EncoderParameters(1);
-
- //第一帧需要设置为MultiFrame
-
- eps.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame);
- bindProperty(res, gif);
- gif.Save(ms, codecInfo, eps);
- }
- else
- {
-
- gFrame.DrawImage(res, rg);
-
- eps = new EncoderParameters(1);
-
- //如果是GIF这里设置为FrameDimensionTime,如果为TIFF则设置为FrameDimensionPage
-
- eps.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionTime);
-
- bindProperty(res, frame);
- gif.SaveAdd(frame, eps);
- }
- }
-
- eps = new EncoderParameters(1);
- eps.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush);
- gif.SaveAdd(eps);
- }
- picImg.Image = Image.FromStream(ms);
- this.Controls.Add(picImg);
- }
-
- private void bindProperty(Image a, Image b)
- {
-
- //这个东西就是每一帧所拥有的属性,可以用GetPropertyItem方法取得这里用为完全复制原有属性所以直接赋值了
-
- //顺便说一下这个属性里包含每帧间隔的秒数和透明背景调色板等设置,这里具体那个值对应那个属性大家自己在msdn搜索GetPropertyItem方法说明就有了
-
- for (int i = 0; i < a.PropertyItems.Length; i++)
- {
- b.SetPropertyItem(a.PropertyItems[i]);
- }
- }
-
- private ImageCodecInfo GetEncoder(ImageFormat format)
- {
-
- ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
-
- foreach (ImageCodecInfo codec in codecs)
- {
- if (codec.FormatID == format.Guid)
- {
- return codec;
- }
- }
- return null;
- }
- GDI+ 中发生一般性错误。
- ErrorCode:-2147467259