Nick
Nick

Reputation: 9183

WinRT Convert Jpg or PNG to GIF

I'm working on a WinRT application that will do some image processing and one of the things I want to do is convert some jpgs or pngs to gif. I have something that sort of works. For some of my test jpgs it works others it's a scrambled image that get's output. Just wondering if there was something I was missing. Here is what I have so far

    public async static void ConvertToGif(IRandomAccessStream stream)
    {
        var decoder = await BitmapDecoder.CreateAsync(stream);
        var pixels = await decoder.GetPixelDataAsync();

        var file = await KnownFolders.PicturesLibrary.CreateFileAsync("test.gif", CreationCollisionOption.ReplaceExisting);
        var outStream = await file.OpenAsync(FileAccessMode.ReadWrite);

        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.GifEncoderId, outStream);

        encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore,
            decoder.PixelWidth, decoder.PixelHeight,
            decoder.DpiX, decoder.DpiY,
            pixels.DetachPixelData());

        await encoder.FlushAsync();
        outStream.Dispose();
    }

Smaller jpgs seem to work, but larger ones come out scrambled. Is there another way to achieve this?

Upvotes: 4

Views: 2366

Answers (1)

Nick
Nick

Reputation: 9183

Duh, the problem was that I was using PixelWidth/Height and I sholud have been using OrientedPixelWidth/Height.

That seems to have resolved my issue for this.

Upvotes: 4

Related Questions