Resize an ASP.NET Image

We need the ability to gracefully resize any photo / image to the exact width / height without distorting the image. We can use a third-party solution, or we can use the built-in .NET functionality for this. I thought this should be an easy solution, without the need for programming a complex algorithm.

Example scenario (we want all downloads to be changed to 200x100 pixels)
Landscape photo with dimensions of 1250x800:
Resizing to 200px would proportionally increase the height by 128 pixels, so that an additional 28px would be cropped top and bottom.

Landscape photo with dimensions of 1250x500:
Resizing the width to 200 pixels proportionally puts the vertex at 80 pixels, so we will need to first catch and resize it in height. Having set the height to 100 pixels, proportionally set the width to 250 pixels. An additional 50px will need to be cropped from the sides of the photo.

Portrait photo with dimensions of 800x950:
Resizing to 200px would proportionally place the height at 238 pixels, so that an additional 138px would be cropped top and bottom.

+3
source share
6 answers

, Bitmap class, :

using (Bitmap original= new Bitmap(...)) {   //A stream or a filename
    //Calculate the desired size using original.Height and original.Width

    Bitmap newImage = new Bitmap(newWidth, newHeight);

    using(Graphics g = Graphics.FromImage(newImage) {

g.DrawImage, .

    }
}
+1

. , , . , , .

1250x800 156.25x100. 200x100. , , , ,

0
0

-. .net. JPG . GIF.

, . , , .

, . , . . , , , .

Here is a short article about cropping an image .

I have a working code that I am currently using, but now I can not get to it. Maybe I'll add my own code later.

0
source

You can see on this post, someone just created a sample application for resizing an image.

They also provide Rescue Image Resizer for reference.

Image Filter Example in C #
Image Resizer Class C #

0
source

Source: https://habr.com/ru/post/1719042/


All Articles