Resize WinForms Button Image ( support time Animated GIF )
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
public static class ButtonEx
{
public static void ResizeImage(this Button btn, Size mSize)
{
if (btn.Image == null)
return;
var mImageFrames = AnimatedGif.ExtractFrames(btn.Image, mSize);
if (mImageFrames.Count > 0)
{
btn.Image = mImageFrames[0].Image;
if (mImageFrames.Count > 1)
{
var mIndex = 0;
var mTimer = new Timer();
mTimer.Interval = mImageFrames[0].Duration * 15;
mTimer.Tick += (object sender, EventArgs e) =>
{
mTimer.Stop();
if (mIndex < mImageFrames.Count - 1)
mIndex++;
else
mIndex = 0;
btn.Image = mImageFrames[mIndex].Image;
mTimer.Interval = mImageFrames[mIndex].Duration * 15;
mTimer.Start();
};
mTimer.Start();
}
}
}
public static Image Resize(this Image mImg, Size mSize)
{
Bitmap newImage = new Bitmap(mSize.Width, mSize.Height);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
gr.DrawImage(mImg, new Rectangle(0, 0, mSize.Width, mSize.Height));
}
return newImage;
}
public class AnimatedGif
{
public static List<animatedgifframe> ExtractFrames(Image img, Size? mNewSize = null)
{
var mImages = new List<animatedgifframe>();
try
{
int frames = img.GetFrameCount(FrameDimension.Time);
if (frames <= 1) throw new ArgumentException("Image not animated");
byte[] times = img.GetPropertyItem(0x5100).Value;
int frame = 0;
for (; ; )
{
int dur = BitConverter.ToInt32(times, 4 * frame);
if (mNewSize == null)
mImages.Add(new AnimatedGifFrame(new Bitmap(img), dur));
else
mImages.Add(new AnimatedGifFrame(new Bitmap(img).Resize(mNewSize.Value), dur));
if (++frame >= frames)
break;
img.SelectActiveFrame(FrameDimension.Time, frame);
}
img.Dispose();
}
catch
{
if (mImages.Count == 0)
{
if (mNewSize == null)
mImages.Add(new AnimatedGifFrame(img, 1000));
else
mImages.Add(new AnimatedGifFrame(img.Resize(mNewSize.Value), 1000));
}
}
return mImages;
}
public class AnimatedGifFrame
{
private int mDuration;
private Image mImage;
internal AnimatedGifFrame(Image img, int duration)
{
mImage = img; mDuration = duration;
}
public Image Image { get { return mImage; } }
public int Duration { get { return mDuration; } }
}
}
}