How to resize an Image C#

前端 未结 17 2413
暗喜
暗喜 2020-11-21 22:28

As Size, Width and Height are Get() properties of System.Drawing.Image;
How can I resize an Image object

17条回答
  •  情话喂你
    2020-11-21 23:15

    You could try net-vips, the C# binding for libvips. It's a lazy, streaming, demand-driven image processing library, so it can do operations like this without needing to load the whole image.

    For example, it comes with a handy image thumbnailer:

    Image image = Image.Thumbnail("image.jpg", 300, 300);
    image.WriteToFile("my-thumbnail.jpg");
    

    It also supports smart crop, a way of intelligently determining the most important part of the image and keeping it in focus while cropping the image. For example:

    Image image = Image.Thumbnail("owl.jpg", 128, crop: "attention");
    image.WriteToFile("tn_owl.jpg");
    

    Where owl.jpg is an off-centre composition:

    Gives this result:

    First it shrinks the image to get the vertical axis to 128 pixels, then crops down to 128 pixels across using the attention strategy. This one searches the image for features which might catch a human eye, see Smartcrop() for details.

提交回复
热议问题