public Image ZoomIn(Image originImg, int newWidth, int newHight) { int frameCount = originImg.GetFrameCount(new FrameDimension(originImg.FrameDimensionsList[0])); if (frameCount > 1) { string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".gif"); AnimatedGifEncoder ge = new AnimatedGifEncoder(); ge.Start(path); ge.SetRepeat(0); FrameDimension dim = new FrameDimension(originImg.FrameDimensionsList[0]); for (int i = 0; i < originImg.GetFrameCount(dim); i++)//遍历图像帧 { originImg.SelectActiveFrame(dim, i);//激活当前帧 int delay = 0; for (int j = 0; j < originImg.PropertyIdList.Length; j++)//遍历帧属性 { if ((int)originImg.PropertyIdList.GetValue(j) == 0x5100)//.如果是延迟时间 { PropertyItem pItem = (PropertyItem)originImg.PropertyItems.GetValue(j);//获取延迟时间属性 byte[] delayByte = new byte[4];//延迟时间,以1/100秒为单位 delayByte[0] = pItem.Value[i * 4]; delayByte[1] = pItem.Value[1 + i * 4]; delayByte[2] = pItem.Value[2 + i * 4]; delayByte[3] = pItem.Value[3 + i * 4]; delay = BitConverter.ToInt32(delayByte, 0) * 10; //乘以10,获取到毫秒 break; } } ge.AddFrame(PicSized((Bitmap)originImg, newWidth, newHight)); ge.SetDelay(delay); } ge.Finish(); return Image.FromFile(path); } else { return PicSized((Bitmap)originImg, newWidth, newHight); } } public Bitmap PicSized(Bitmap originBmp, int newWidth, int newHight) { Bitmap resizedBmp = new Bitmap(newWidth, newHight); Graphics g = Graphics.FromImage(resizedBmp); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawImage(originBmp, new Rectangle(0, 0, newWidth, newHight), new Rectangle(0, 0, originBmp.Width, originBmp.Height), GraphicsUnit.Pixel); g.Dispose(); return resizedBmp; }
使用
pbCode.Image = ZoomIn(image, pbCode.Width, pbCode.Height);
转载请注明:http://www.cnblogs.com/Rolends
HAPPY EVERY DAY ! !