This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

Notification

Icon
Error

Options
Go to last post Go to first unread
bma  
#1 Posted : Monday, January 25, 2016 3:01:53 AM(UTC)
bma

Rank: Member

Groups: Guest
Joined: 1/12/2016(UTC)
Posts: 23

Thanks: 8 times
Hello,

When I try to resize an animated gif image, the new image loses quality and is deformed (See images attached).

Code:
public byte[] ResizeGif(byte[] media, int width, int height)
{
	using (var imageStream = new MemoryStream(media))
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = ImageReader.Create(imageStream))
	{

		using (var imageWriter = new GifWriter(exportedImageStream))
		{
			imageWriter.BackgroundIndex = ((GifReader)imageReader).BackgroundEntryIndex;
			imageWriter.Palette = ((GifReader)imageReader).Palette;
			imageWriter.PlaybackCount = ((GifReader)imageReader).PlaybackCount;

			for (var i = 0; i < ((GifReader)imageReader).Frames.Count; i++)
			{
				// Read a frame
				using (var frame = ((GifReader)imageReader).Frames[i])
				using (var bitmap = frame.GetBitmap())
				{
					Console.WriteLine(i);
					// Preserve the original palette
					var palette = bitmap.Palette;
					// Preserve the original pixel format
					var pixelFormat = bitmap.PixelFormat;

					// Convert the bitmap to a non-indexed format
					bitmap.ColorManagement.Convert(ColorSpace.Rgb, true, false);

					// Resize the bitmap in a high quality mode
					bitmap.Transforms.Resize(width, height, ResizeInterpolationMode.High);

					// Return to the indexed format
					bitmap.Palette = palette;
					bitmap.ColorManagement.Convert(pixelFormat);

					// Copy frame delay
					imageWriter.FrameOptions.Delay = frame.Delay;

					// Add the frame
					Pipeline.Run(bitmap + imageWriter);
				}
			}
		}

		return exportedImageStream.GetBuffer();
	}
}

Kind regards

Edited by user Monday, January 25, 2016 3:05:38 AM(UTC)  | Reason: Not specified

File Attachment(s):
image_resized.gif (8,192kb) downloaded 4 time(s).
original_image.gif (5,332kb) downloaded 2 time(s).
Fedor  
#2 Posted : Tuesday, January 26, 2016 5:58:23 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Guest
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
Here is the updated code which fixes the problems:

Code:
public byte[] ResizeGif(byte[] media, int width, int height)
{
	using (var imageStream = new MemoryStream(media))
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = ImageReader.Create(imageStream))
	{
		var kX = (float)width / (float)imageReader.Width;
		var kY = (float)height / (float)imageReader.Height;

		using (var imageWriter = new GifWriter(exportedImageStream))
		{
			imageWriter.BackgroundIndex = ((GifReader)imageReader).BackgroundEntryIndex;
			imageWriter.Palette = ((GifReader)imageReader).Palette;
			imageWriter.PlaybackCount = ((GifReader)imageReader).PlaybackCount;

			for (var i = 0; i < ((GifReader)imageReader).Frames.Count; i++)
			{
				// Read a frame
				using (var frame = ((GifReader)imageReader).Frames[i])
				using (var bitmap = frame.GetBitmap())
				{
					Console.WriteLine(i);
					bitmap.Save("../../../" + i + ".png");

					// Preserve the original palette
					var palette = bitmap.Palette;
					// Preserve the original pixel format
					var pixelFormat = bitmap.PixelFormat;

					// Convert the bitmap to a non-indexed format
					bitmap.ColorManagement.Convert(ColorSpace.Rgb, true, false);

					var newWidth = Math.Max(1, (int)((float)bitmap.Width * kX));
					var newHeight = Math.Max(1, (int)((float)bitmap.Height * kY));
					//Use low quality resize mode to prevent noise
					bitmap.Transforms.Resize(newWidth, newHeight, ResizeInterpolationMode.Low);

					// Return to the indexed format
					bitmap.ColorManagement.Palette = palette;
					bitmap.ColorManagement.Convert(pixelFormat);

					// Copy frame delay
					imageWriter.FrameOptions.Left = (ushort)((float)frame.Left * kX);
					imageWriter.FrameOptions.Top = (ushort)((float)frame.Top * kY);
					imageWriter.FrameOptions.Delay = frame.Delay;
					imageWriter.FrameOptions.DisposalMethod = frame.DisposalMethod;

					// Add the frame
					Pipeline.Run(bitmap + imageWriter);
				}
			}
		}

		return exportedImageStream.GetBuffer();
	}
}

Edited by user Tuesday, January 26, 2016 8:34:32 AM(UTC)  | Reason: Not specified

Best regards,

Fedor Skvortsov

thanks 1 user thanked Fedor for this useful post.
bma on 1/26/2016(UTC)
bma  
#3 Posted : Tuesday, January 26, 2016 6:21:18 AM(UTC)
bma

Rank: Member

Groups: Guest
Joined: 1/12/2016(UTC)
Posts: 23

Thanks: 8 times
Thank you! It worked!
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.