public bool CreateGifThumbnail(string filename, string destinationPath, string thumbnailPrefix)
{
try
{
var img = Image.FromFile(filename);
var frames = img.GetFrameCount(FrameDimension.Time);
if (frames <= 1) throw new Exception("Image not animated");
var frame = Convert.ToInt32(Math.Round(Convert.ToDouble(frames / 2), MidpointRounding.AwayFromZero));
img.SelectActiveFrame(FrameDimension.Time, frame);
if (!Directory.Exists(destinationPath.TrimEnd('\\')))
{
FileHelper.CreateDirectory(destinationPath);
}
var compressedImage = ResizeImage(img, GetComputedImageSize(img.Width, img.Height));
var jpgEncoder = GetEncoder(ImageFormat.Jpeg);
var encoder = Encoder.Quality;
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(encoder, 100L);
compressedImage.Save(destinationPath.TrimEnd('\\') + @"\" + thumbnailPrefix + Path.GetFileNameWithoutExtension(filename) + ".jpg", jpgEncoder, encoderParams);
compressedImage.Dispose();
img.Dispose();
return true;
}
catch (Exception ex)
{
throw new BlogException(ex.Message, ex.InnerException);
}
}