Resize image and save as file

0

My objective is to resize an image that I create and after resizing it with the same ratio and finally save it on my drive. so just below the code is working I'm 100% sure that the code work The problem is on the ResizeImage method. Everything work well until the decoder, I don't know why but it always return me the code:

System.Exception: 'The component was not found. (Exception from HRESULT: 0x88982F50)'

private async Task<StorageFile> SaveSquaresPhotos(WriteableBitmap WB, FileFormat fileFormat, string username) {
            string FileName = username + "-Carre.";
            Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
            switch (fileFormat)
            {
                case FileFormat.Jpeg:
                    FileName += "jpeg";
                    BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
                    break;

                case FileFormat.Png:
                    FileName += "png";
                    BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
                    break;

                case FileFormat.Bmp:
                    FileName += "bmp";
                    BitmapEncoderGuid = BitmapEncoder.BmpEncoderId;
                    break;

                case FileFormat.Tiff:
                    FileName += "tiff";
                    BitmapEncoderGuid = BitmapEncoder.TiffEncoderId;
                    break;

                case FileFormat.Gif:
                    FileName += "gif";
                    BitmapEncoderGuid = BitmapEncoder.GifEncoderId;
                    break;
            }
            if (config.getFolder() == null)
            {
                try
                {
                    var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
                    var folder = await StorageFolder.GetFolderFromPathAsync(resourceLoader.GetString("Folder"));
                    var file = await folder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
                        Stream pixelStream = WB.PixelBuffer.AsStream();
                        byte[] pixels = new byte[pixelStream.Length];
                        await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                            (uint)WB.PixelWidth,
                                            (uint)WB.PixelHeight,
                                            96.0,
                                            96.0,
                                            pixels);
                        await ResizeImage(pixels, 75, 75, pixelStream);
                        await encoder.FlushAsync();
                    }
                    return file;
                }
                catch {
                    var file = await _captureFolder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
                        Stream pixelStream = WB.PixelBuffer.AsStream();
                        byte[] pixels = new byte[pixelStream.Length];
                        await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                            (uint)WB.PixelWidth,
                                            (uint)WB.PixelHeight,
                                            96.0,
                                            96.0,
                                            pixels);
                        await ResizeImage(pixels, 75, 75, pixelStream);
                        await encoder.FlushAsync();
                    }
                    return file;
                }
            }
            else
            {

                var file = await config.getFolder().CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
                    Stream pixelStream = WB.PixelBuffer.AsStream();
                    byte[] pixels = new byte[pixelStream.Length];
                    await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                        (uint)WB.PixelWidth,
                                        (uint)WB.PixelHeight,
                                        96.0,
                                        96.0,
                                        pixels);
                    await ResizeImage(pixels, 75, 75, pixelStream);
                    await encoder.FlushAsync();
                }
                return file;
            }
}


public async Task<byte[]> ResizeImage(byte[] imageData, int reqWidth, int reqHeight, Stream stream)
        {

            var memStream = new MemoryStream();
            stream.Position = 0;
            await stream.CopyToAsync(memStream);

            IRandomAccessStream imageStream = memStream.AsRandomAccessStream();
            imageStream.Seek(0);

//The problem start just below
            var decoder = await BitmapDecoder.CreateAsync(imageStream);
            if (decoder.PixelHeight > reqHeight || decoder.PixelWidth > reqWidth)
            {
                using (imageStream)
                {
                    var resizedStream = new InMemoryRandomAccessStream();

                    BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
                    double widthRatio = (double)reqWidth / decoder.PixelWidth;
                    double heightRatio = (double)reqHeight / decoder.PixelHeight;

                    double scaleRatio = Math.Min(widthRatio, heightRatio);

                    if (reqWidth == 0)
                        scaleRatio = heightRatio;

                    if (reqHeight == 0)
                        scaleRatio = widthRatio;

                    uint aspectHeight = (uint)Math.Floor(decoder.PixelHeight * scaleRatio);
                    uint aspectWidth = (uint)Math.Floor(decoder.PixelWidth * scaleRatio);

                    encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;

                    encoder.BitmapTransform.ScaledHeight = aspectHeight;
                    encoder.BitmapTransform.ScaledWidth = aspectWidth;

                    await encoder.FlushAsync();
                    resizedStream.Seek(0);
                    var outBuffer = new byte[resizedStream.Size];
                    uint x = await resizedStream.WriteAsync(outBuffer.AsBuffer());
                    return outBuffer;
                }
            }
            return imageData;
        }

if someone can tell me where the problem is from, I'll be very grateful.

Edit: I found the solution. First my call for the second method was not on the correct line. Finally I found an other method for resizing my file, that was more close to what I had.

private async Task<StorageFile> SaveThumbsPhotos(WriteableBitmap WB, FileFormat fileFormat, string username)
        {
            string FileName = username + "-Thumbs.";
            Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
            switch (fileFormat)
            {
                case FileFormat.Jpg:
                    FileName += "jpg";
                    BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
                    break;

                case FileFormat.Png:
                    FileName += "png";
                    BitmapEncoderGuid = BitmapEncoder.PngEncoderId;
                    break;

                case FileFormat.Bmp:
                    FileName += "bmp";
                    BitmapEncoderGuid = BitmapEncoder.BmpEncoderId;
                    break;

                case FileFormat.Tiff:
                    FileName += "tiff";
                    BitmapEncoderGuid = BitmapEncoder.TiffEncoderId;
                    break;

                case FileFormat.Gif:
                    FileName += "gif";
                    BitmapEncoderGuid = BitmapEncoder.GifEncoderId;
                    break;
            }
            if (config.getFolder() == null)
            {
                try
                {
                    var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
                    var folder = await StorageFolder.GetFolderFromPathAsync(resourceLoader.GetString("Folder"));
                    var file = await folder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
                        Stream pixelStream = WB.PixelBuffer.AsStream();
                        byte[] pixels = new byte[pixelStream.Length];
                        await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                            (uint)WB.PixelWidth,
                                            (uint)WB.PixelHeight,
                                            96.0,
                                            96.0,
                                            pixels);
                        await encoder.FlushAsync();
                    }
                    await RescaleImage(file, 75, 75, username);
                    return file;
                }
                catch
                {
                    var file = await _captureFolder.CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
                        Stream pixelStream = WB.PixelBuffer.AsStream();
                        byte[] pixels = new byte[pixelStream.Length];
                        await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                            (uint)WB.PixelWidth,
                                            (uint)WB.PixelHeight,
                                            96.0,
                                            96.0,
                                            pixels);
                        await encoder.FlushAsync();
                    }
                    await RescaleImage(file, 75, 75, username);
                    return file;
                }
            }
            else
            {

                var file = await config.getFolder().CreateFileAsync(FileName, CreationCollisionOption.GenerateUniqueName);
                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream);
                    Stream pixelStream = WB.PixelBuffer.AsStream();
                    byte[] pixels = new byte[pixelStream.Length];
                    await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                    encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore,
                                        (uint)WB.PixelWidth,
                                        (uint)WB.PixelHeight,
                                        96.0,
                                        96.0,
                                        pixels);


                    await encoder.FlushAsync();
                }
                await RescaleImage(file, 75, 75, username);
                return file;
            }
        }
private async Task<StorageFile> RescaleImage(StorageFile source, uint width, uint height, string username)
        {
            var imageStream = await source.OpenReadAsync();
            var decoder = await BitmapDecoder.CreateAsync(imageStream);
            using (var resizedStream = await source.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateForTranscodingAsync(resizedStream, decoder);
                encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
                encoder.BitmapTransform.ScaledWidth = width;
                encoder.BitmapTransform.ScaledHeight = height;
                await encoder.FlushAsync();
            }
            return source;
        }
c#
uwp
asked on Stack Overflow Jan 28, 2020 by Kaanayci • edited Jan 29, 2020 by Kaanayci

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0