Aurigma Forums
»
Graphics Mill
»
Discussions – Graphics Mill
»
Can't resize an animated GIF when using streams instead of directory paths
Rank: Member
Groups: Guest
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
Edited by user Thursday, January 5, 2017 9:27:14 AM(UTC)
| Reason: Not specified
|
|
|
|
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)
|
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
|
|
|
|
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)
|
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
|
|
|
|
Aurigma Forums
»
Graphics Mill
»
Discussions – Graphics Mill
»
Can't resize an animated GIF when using streams instead of directory paths
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.