예제 #1
0
        // https://github.com/ejohnson-dotnet/heic2jpg/blob/master/Program.cs
        public static async Task Convert(string input, Format format)
        {
            try
            {
                var wInputFile = await StorageFile.GetFileFromPathAsync(input);

                using (var wInputStream = await wInputFile.OpenReadAsync())
                {
                    var wDecoder = await BitmapDecoder.CreateAsync(wInputStream);

                    var wBitmap = await wDecoder.GetSoftwareBitmapAsync();

                    var wOutputName   = Path.GetFileName(Path.ChangeExtension(input, $".{format}".ToLower()));
                    var wParentFolder = await wInputFile.GetParentAsync();

                    var wOutputFile = await wParentFolder.CreateFileAsync(wOutputName, CreationCollisionOption.ReplaceExisting);

                    using (var wOutputStream = await wOutputFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var wEncoder = await BitmapEncoder.CreateAsync(Encoders[format], wOutputStream);

                        wEncoder.SetSoftwareBitmap(wBitmap);
                        wEncoder.IsThumbnailGenerated = true;
                        try
                        {
                            await wEncoder.FlushAsync();
                        }
                        catch (Exception e)
                        {
                            const int WINCODEC_ERR_UNSUPPORTEDOPERATION = unchecked ((int)0x88982F81);
                            switch (e.HResult)
                            {
                            case WINCODEC_ERR_UNSUPPORTEDOPERATION:
                                // If the encoder does not support writing a thumbnail, then try again
                                // but disable thumbnail generation.
                                wEncoder.IsThumbnailGenerated = false;
                                break;

                            default:
                                throw;
                            }
                        }
                        if (wEncoder.IsThumbnailGenerated == false)
                        {
                            await wEncoder.FlushAsync();
                        }
                    }
                    var wPropertiesToRetrieve = SystemProperties.Union(SystemGpsProperties).Union(SystemImageProperties).Union(SystemPhotoProperties).ToList();
                    var wProperties           = await wInputFile.Properties.RetrievePropertiesAsync(wPropertiesToRetrieve);

                    foreach (var wProperty in wProperties)
                    {
                        try
                        {
                            await wOutputFile.Properties.SavePropertiesAsync(new[] { wProperty });
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Key: {wProperty.Key}, Value: {wProperty.Value}");
                            Console.WriteLine(e.ToString());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }