Amazon S3 animated gif - only download the first frame

(I have looked into all similar questions I can find about this on Stackoverflow, and none of them help me solve my problem)

I can upload a gif for Amazon S3 like this, where request.FileBytes is a byte array:

public bool UploadFile(CdnFile request)
{
  var transfer = new TransferUtility(CdnConfig.AccessKey, CdnConfig.SecretKey, ep);
  var uploadRequest = new TransferUtilityUploadRequest();
  uploadRequest.InputStream = new MemoryStream(request.FileBytes);

  var result = transfer.BeginUpload(s3Req2, cb, null);
  transfer.EndUpload(result);
}

      

This works great for .jpg, however for animated .gif only the first frame of the gif is loaded (if I load the same gif via S3 it works fine). So it must be related to how I am transferring or converting the file, however I am not sure how to find out where the problem is.

I create request.FileBytes as follows, where "imageData" is the raw data for the image (Chrome uses this image data when it displays a preview of the animated gif that I am trying to upload to S3 and it animates fine)

var data = Convert.FromBase64String(imageData);
var img = new Bitmap(new MemoryStream(data));
fileBytes = img.SaveAsGif();

public static byte[] SaveAsGif(this Image img, long quality = 100)
{
    ImageCodecInfo gifEncoder = GetEncoder(ImageFormat.Gif);

    Encoder myEncoder = Encoder.Quality;

    EncoderParameters myEncoderParameters = new EncoderParameters(1);

    EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
    myEncoderParameters.Param[0] = myEncoderParameter;

    using (MemoryStream msGif = new MemoryStream())
    {
        img.Save(msGif, gifEncoder, myEncoderParameters);
        return msGif.ToArray();
    }
}

      

+3


source to share


2 answers


This code works great for me, maybe you can adapt it for your use case. I am loading a GIF from a local file and it shows / works fine after loading:



        using (var client = new AmazonS3Client("AKIAI5ZL44fake4442COJA", "4sYnPuA1zMkhghghghghTpX4F5/FUXGDiAKm", RegionEndpoint.USEast1))
        {
            var request = new PutObjectRequest
            {
                BucketName = "bucketname",
                ContentType = "image/gif",
                Key = "test.gif",
                FilePath = @"c:\dev\006.gif"
            };
            var response = client.PutObject(request);
        }

      

+1


source


My SaveAsGif method was the problem, so the problem is with the codec. I can create my request.fileBytes without using a codec like this and it works:



var imageStream = new MemoryStream(data);
fileBytes = imageStream.ToArray();

      

0


source







All Articles