语言

Menu
Sites
Language
How to resize image

Hello world,

I am using Tizen 5.0, and we cannot use FFImageloading for security reasons on Samsung TV.
I'm looking for a quick way to resize an image and save memory.

I would like to display a collection of images (+ - 200 images) in my collection view
working with lazy loading, first images are fast but when I scroll down my application slows down also.

the default image size is 900x600, and I would like to divide by 3.


Thank you

响应

6 回复
Ali Demir

I found a way, and I use SkiaSharp for this.

public void ImageResizer(string inputPath, string OutputPath){
    int DesiredWidth=200, DesiredHeight=300;
    const int quality = 75;
    using (var input = File.OpenRead(inputPath))
    using (var inputStream = new SKManagedStream(input))
    using (var original = SKBitmap.Decode(inputStream))
    {
        using (var resized = original.Resize(new SKImageInfo(DesiredWidth, DesiredHeight), SKBitmapResizeMethod.Lanczos3))
        {
            if (resized == null) return;
            using (var image = SKImage.FromBitmap(resized))
            using (var output = File.OpenWrite(OutputPath))
            {
                image.Encode(SKEncodedImageFormat.Jpeg, quality).SaveTo(output);
            }
        }
    }
}

You can replace input or output path by a stream.
 

Thanks

Usha Vilash

Hey hi, then you can directly use any online application such as https://jpegcompressor.com/ it compress images without losing their quality and you can implement image resizing directly in your application by


function resizeImage(imageData, targetWidth, targetHeight) {
    // Implement image resizing logic here
    // For example, if using HTML Canvas:
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    canvas.width = targetWidth;
    canvas.height = targetHeight;
    ctx.drawImage(imageData, 0, 0, targetWidth, targetHeight);
    return canvas.toDataURL('image/jpeg'); // Return resized image data URL
}

// Assuming you have an array of image URLs or data
var imageUrls = [...];
var resizedImages = [];

// Loop through each image and resize
imageUrls.forEach(function(imageUrl) {
    var image = new Image();
    image.onload = function() {
        var resizedImage = resizeImage(image, 900 / 3, 600 / 3); // Resize to 1/3 of original size
        resizedImages.push(resizedImage);
        // If all images are resized, update UI or load into collection view
        if (resizedImages.length === imageUrls.length) {
            // Update UI or load into collection view here
        }
    };
    image.src = imageUrl;
});