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;
        }
Kaanayci
  • 1
  • 4
  • The 0x88982f50 error is generally related to a failure to encode/decode an image file correctly. I can't see anything immediately wrong with your code but from previous experience, this is usually caused by incorrect image data from the stream. Perhaps this information will be useful to help debug. – Adam McMahon Jan 28 '20 at 13:34
  • Does this answer your question? [The component cannot be found. (Exception from HRESULT: 0x88982F50)](https://stackoverflow.com/questions/30273307/the-component-cannot-be-found-exception-from-hresult-0x88982f50) – mortb Jan 29 '20 at 08:41

0 Answers0