当前位置:网站首页>. NETCORE enables image scaling and cropping - based on imagesharp

. NETCORE enables image scaling and cropping - based on imagesharp

2022-06-22 17:04:00 Dotnet cross platform

Preface

( I suddenly found that it has been more than a while

When I was blogging recently , Need to implement a similar Lorempixel、LoremPicsum Such random picture function , The picture has , You also need a function to get pictures according to the input width and height , Because before processing pictures, we used ImageSharp library , So this time I immediately thought of using it .

Analyze requirements

The pictures in the picture library are basically Wallpapers I collected before , The sizes are uneven , There are horizontal and vertical screens

Then, you need to input the width and height of the interface , You can randomly select a picture and zoom or crop it

My idea is :

  • Horizontal screen picture , Adjust the height to match the input height , The width is adjusted proportionally

  • Vertical screen picture , Adjust the width to match the input height , Adjust the height proportionally

Then select the middle part to intercept

Of course, there are special circumstances , This is when the input width and height exceed the original height and width of the picture , This can only destroy the original scale of the picture , Force stretching ~

PS: I wanted to draw a picture to express my thoughts , But I haven't found any drawing tools (Draw.io: Don't look at me )

Realization

First read the picture

It's simple , Just pass in the picture path

Of course, it can also be read by stream

using var image = Image.Load("imagePath");

Then, zoom and crop the image according to its size

The code used by many blogs found on the Internet is similar image.Resize and image.Crop And so on. , But this is an old version ImageSharp Code

The new version is all in image.Mutate in , What operation do you want to import again lambda expression

( It's kind of like ORM The operation of )

For example, scaling is like this

image.Mutate(a => a.Resize(newWidth, newHeight));

This is the way of cutting

image.Mutate(a => a.Crop(new Rectangle(x, y, width, height)));

Complete code

The function is very simple , The complete code is here

void GetImage(string imagePath, int width, int height) {
    using var image = Image.Load(imagePath);
    Rectangle cropRect;
    int newWidth;
    int newHeight;
    
    //  Horizontal screen picture 
    if (image.Width > image.Height) {
        if (width > image.Width) {
            newWidth = width;
            newHeight = height;
        }
        else {
            newHeight = height;
            newWidth = image.Width / image.Height * newHeight;
        }

        cropRect = new Rectangle((newWidth - width) / 2, 0, width, height);
    }
    //  Vertical screen picture 
    else {
        if (height > image.Height) {
            newWidth = width;
            newHeight = height;
        }
        else {
            newWidth = width;
            newHeight = newWidth * image.Height / image.Width;
        }

        cropRect = new Rectangle(0, (newHeight - height) / 2, width, height);
    }

    image.Mutate(a => a.Resize(newWidth, newHeight));
    image.Mutate(a => a.Crop(cropRect));
    image.SaveAsPng("output.png");
}

Follow up in my StarBlog Develop notes series in , Next, we'll update ~

Reference material

  • Random picture website :https://picsum.photos/

  • .NetCore How to use ImageSharp Generate pictures :https://www.cnblogs.com/niwan/p/11126239.html

  • https://stackoverflow.com/questions/63639644/how-load-an-image-from-disk-and-save-to-a-stream-using-imagesharp-while-preservi

原网站

版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221536307151.html