Esempio n. 1
0
        public IAsyncInfo GenerateGif(StorageFile outputFile, int delay, bool repeat, List <byte[]> sourceBytes)
        {
            return(AsyncInfo.Run(async ctx =>
            {
                var outStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite);

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

                if (repeat)     // set repeat property
                {
                    var containerProperties = new BitmapPropertySet
                    {
                        { "/appext/Application", new BitmapTypedValue(Encoding.UTF8.GetBytes("NETSCAPE2.0"), PropertyType.UInt8Array) },
                        { "/appext/Data", new BitmapTypedValue(new byte[] { 3, 1, 0, 0, 0 }, PropertyType.UInt8Array) },
                    };

                    await encoder.BitmapContainerProperties.SetPropertiesAsync(containerProperties);
                }

                try
                {
                    for (int i = 0; i < sourceBytes.Count; i++)
                    {
                        using (MemoryRandomAccessStream frameStream = new MemoryRandomAccessStream(sourceBytes[i]))
                        {
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(frameStream);
                            PixelDataProvider pixels = await decoder.GetPixelDataAsync();

                            encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore,
                                                 decoder.OrientedPixelWidth, decoder.OrientedPixelHeight,
                                                 decoder.DpiX, decoder.DpiY,
                                                 pixels.DetachPixelData());

                            if (i == 0)
                            {
                                var properties = new BitmapPropertySet {
                                    { "/grctlext/Delay", new BitmapTypedValue(delay / 10, PropertyType.UInt16) }
                                };
                                await encoder.BitmapProperties.SetPropertiesAsync(properties);
                            }

                            if (i < sourceBytes.Count - 1)
                            {
                                await encoder.GoToNextFrameAsync();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("exception caught: " + ex.ToString());
                    //   throw ex;
                }
                await encoder.FlushAsync();
                outStream.Dispose();
            }));
        }
Esempio n. 2
0
        public IAsyncInfo GenerateGif(StorageFile outputFile, int delay, bool repeat, List<byte[]> sourceBytes)
        {
            return AsyncInfo.Run(async ctx =>
            {
                var outStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite);

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

                if (repeat)     // set repeat property
                {
                    var containerProperties = new BitmapPropertySet
                        {
                            { "/appext/Application", new BitmapTypedValue(Encoding.UTF8.GetBytes("NETSCAPE2.0"), PropertyType.UInt8Array) },
                            { "/appext/Data", new BitmapTypedValue(new byte[] { 3, 1, 0, 0, 0 }, PropertyType.UInt8Array) },
                        };

                    await encoder.BitmapContainerProperties.SetPropertiesAsync(containerProperties);
                }

                try
                {
                    for (int i = 0; i < sourceBytes.Count; i++)
                    {

                        using (MemoryRandomAccessStream frameStream = new MemoryRandomAccessStream(sourceBytes[i]))
                        {
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(frameStream);
                            PixelDataProvider pixels = await decoder.GetPixelDataAsync();

                            encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore,
                                decoder.OrientedPixelWidth, decoder.OrientedPixelHeight,
                                decoder.DpiX, decoder.DpiY,
                                pixels.DetachPixelData());

                            if (i == 0)
                            {
                                var properties = new BitmapPropertySet{ { "/grctlext/Delay", new BitmapTypedValue(delay / 10, PropertyType.UInt16) } };
                                await encoder.BitmapProperties.SetPropertiesAsync(properties);
                            }

                            if (i < sourceBytes.Count - 1)
                                await encoder.GoToNextFrameAsync();
                        }
                    }

                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("exception caught: " + ex.ToString());
                    //   throw ex;
                }
                await encoder.FlushAsync();
                outStream.Dispose();

            });
        }
Esempio n. 3
0
        static public BitmapImage GetImage(byte[] imagebytes)
        {
            BitmapImage bi = new BitmapImage();

            using (MemoryRandomAccessStream ras = new MemoryRandomAccessStream(imagebytes))
            {
                bi.SetSource(ras);
            }

            return(bi);
        }
Esempio n. 4
0
        static public BitmapImage GetImage(byte[] imagebytes)
        {
            BitmapImage bi = new BitmapImage();

            using (MemoryRandomAccessStream ras = new MemoryRandomAccessStream(imagebytes))
            {
                bi.SetSource(ras);
            }

            return bi;
        }