Forum Libraries, Components, and Plugins

Large image resizes to blank

AmyTwinameAmyTwiname Member ✭✭

When resizing a large image (> 6MB, 14034px by 9921px), the process goes through with no errors, but the image itself doesn't show upon save, only the white background canvas.

The code works fine with smaller images.

Is there a limit on how large a file size can be processed?`

    using (SKMemoryStream sourceStream = new SKMemoryStream(imageData))
        {
            using (SKCodec codec = SKCodec.Create(sourceStream))
            {
                sourceStream.Seek(0);

                using (SKImage image = SKImage.FromEncodedData(SKData.Create(sourceStream)))
                {
                    int newHeight = image.Height;
                    int newWidth = image.Width;

                    if (maxHeight > 0 && newHeight > maxHeight)
                    {
                        double scale = (double)maxHeight / newHeight;
                        newHeight = maxHeight;
                        newWidth = (int)Math.Floor(newWidth * scale);
                    }

                    if (maxWidth > 0 && newWidth > maxWidth)
                    {
                        double scale = (double)maxWidth / newWidth;
                        newWidth = maxWidth;
                        newHeight = (int)Math.Floor(newHeight * scale);
                    }

                    var info = codec.Info.ColorSpace.IsSrgb ? new SKImageInfo(newWidth, newHeight) : new SKImageInfo(newWidth, newHeight, SKImageInfo.PlatformColorType, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
                    using (SKSurface surface = SKSurface.Create(info))
                    {
                        using (SKPaint paint = new SKPaint())
                        {
                            // High quality without antialiasing
                            paint.IsAntialias = true;
                            paint.FilterQuality = SKFilterQuality.High;

                            // Draw the bitmap to fill the surface
                            surface.Canvas.Clear(SKColors.White);
                            var rect = new SKRect(0, 0, newWidth, newHeight);
                            surface.Canvas.DrawImage(image, rect, paint);
                            surface.Canvas.Flush();

                            using (SKImage newImage = surface.Snapshot())
                            {
                                using (SKData newImageData = newImage.Encode(convertToJpeg ? SKEncodedImageFormat.Jpeg : (codec.EncodedFormat == SKEncodedImageFormat.Gif ? SKEncodedImageFormat.Png : codec.EncodedFormat), 
                                    codec.EncodedFormat == SKEncodedImageFormat.Gif || codec.EncodedFormat == SKEncodedImageFormat.Png || !downsampleJpeg ? 100 : 85))
                                {
                                    return newImageData.ToArray();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

`

Tagged:

Posts

  • AmyTwinameAmyTwiname Member ✭✭

    I'm doing some more testing, I've tried creating an SKBitmap of the image before resizing - I get the error Unable to allocate pixels for the bitmap. If anyone has details on how to resolve this I'd be grateful!

Sign In or Register to comment.