Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
bma  
#1 Posted : Thursday, January 5, 2017 9:26:40 AM(UTC)
bma

Rank: Member

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

Thanks: 8 times
Hello,

Can't resize an animated GIF when using streams instead of directory paths... The stream has 0 length... Could you help me with this?

Code snippet:

Code:

private static byte[] ResizeAnimatedGif(byte[] media, int Width, int height)
{
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = new GifReader(new MemoryStream(media)))
	using (var imageWriter = new GifWriter(exportedImageStream))
	{
   
		var kX = (float)Width / (float)imageReader.Width;
		var kY = (float)height / (float)imageReader.Height;

		imageWriter.BackgroundIndex = imageReader.BackgroundEntryIndex;
		imageWriter.Palette = imageReader.Palette;
		imageWriter.PlaybackCount = imageReader.PlaybackCount;

		for (var i = 0; i < imageReader.Frames.Count; i++)
		{
			using (var frame = imageReader.Frames[i])
			using (var bitmap = frame.GetBitmap())
			{
				var palette = bitmap.Palette;
				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)(bitmap.Width * kX));
				var newHeight = Math.Max(1, (int)(bitmap.Height * kY));

				//Use ResizeInterpolationMode.Low to prevent noise
				bitmap.Transforms.Resize(newWidth, newHeight, ResizeInterpolationMode.Low);

				bitmap.Palette = palette;
				bitmap.ColorManagement.Convert(pixelFormat);

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

				Pipeline.Run(bitmap + imageWriter);
			}
		}

		return exportedImageStream.GetBuffer();
	}

}


I'm using Graphics Mill v9.0.21

Thank you,
Bruno

mf3RIFm.gif

Edited by user Thursday, January 5, 2017 9:27:14 AM(UTC)  | Reason: Not specified

Fedor  
#2 Posted : Thursday, January 5, 2017 11:17:47 PM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,556

Thanks: 5 times
Was thanked: 62 time(s) in 61 post(s)
We confirm the problem (Aurigma Bug #0022949). It seems GifWriter doesn't work correctly with MemoryStream.

We will fix it shortly. I will keep you informed of the status.

Best regards,
Fedor Skvortsov
Fedor  
#3 Posted : Tuesday, February 7, 2017 9:02:43 AM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,556

Thanks: 5 times
Was thanked: 62 time(s) in 61 post(s)
To fix the problem you should:

1. Сlose GifWriter before getting a byte array.
2. Use the MemoryStream.ToArray method instead of MemoryStream.GetBuffer.

Code:
private static byte[] ResizeAnimatedGif(byte[] media, int Width, int height)
{
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = new GifReader(new MemoryStream(media)))
	using (var imageWriter = new GifWriter(exportedImageStream))
	{
		var kX = (float)Width / (float)imageReader.Width;
		var kY = (float)height / (float)imageReader.Height;

		imageWriter.BackgroundIndex = imageReader.BackgroundEntryIndex;
		imageWriter.Palette = imageReader.Palette;
		imageWriter.PlaybackCount = imageReader.PlaybackCount;

		for (var i = 0; i < imageReader.Frames.Count; i++)
		{
			using (var frame = imageReader.Frames[i])
			using (var bitmap = frame.GetBitmap())
			{
				var palette = bitmap.Palette;
				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)(bitmap.Width * kX));
				var newHeight = Math.Max(1, (int)(bitmap.Height * kY));

				//Use ResizeInterpolationMode.Low to prevent noise
				bitmap.Transforms.Resize(newWidth, newHeight, ResizeInterpolationMode.Low);

				bitmap.Palette = palette;
				bitmap.ColorManagement.Convert(pixelFormat);

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

				Pipeline.Run(bitmap + imageWriter);
			}
		}

		exportedImageStream.Position = 0;

		imageWriter.Close();
		return exportedImageStream.ToArray();
	}
}

Best regards,
Fedor Skvortsov
Users browsing this topic
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.