1

I am trying to resize image to 500x500 px. Using below code:

public static Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
    var destRect = new Rectangle(0, 0, width, height);
    var destImage = new Bitmap(width, height);

    destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    using (var graphics = Graphics.FromImage(destImage))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

        using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
        }
    }

    return destImage;
}

But this just resizes horizontally and not doing cropping! Someone suggested me to crop the image to a centered 1:1 ratio and then downscale it. I also wish to increase size of image to 500x500 if it is less than it.

How can I achieve it?

shA.t
  • 16,580
  • 5
  • 54
  • 111
James
  • 1,827
  • 5
  • 39
  • 69
  • if you are using WPF than probably that could be easily be done using `ImageBrush` with playing the properties like `ViewboxUnits="Absolute" Viewbox, Viewport` – Mohit S Sep 03 '15 at 09:30
  • @MohitShrivastava i am not using WPF but asp.net – James Sep 03 '15 at 09:31

1 Answers1

1

Instead of

using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
        }

Use

Rectangle cropRect = new Rectangle(0,0,500,500);
graphics.DrawImage(image, destRect, cropRect, GraphicsUnit.Pixel);

and you can also read more about DrawImageUnscaledAndClipped for cropping purpose.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • what do i pass to cropRect ? – James Sep 03 '15 at 09:40
  • It would be cropped rectangle on the image. So if the image is the bigger in size you can pass the 500*500 rectangle. – Mohit S Sep 03 '15 at 09:42
  • when i paste your code then i get error cannot resolve symbol cropRect – James Sep 03 '15 at 09:43
  • Also cant we crop it as square instead rectangle – James Sep 03 '15 at 09:44
  • you can read about the arguments in of [DrawImage](https://msdn.microsoft.com/en-us/library/ms142040(v=vs.110).aspx) I'll update my code as well – Mohit S Sep 03 '15 at 09:45
  • if i want 640 *640 then how i create rectangle using this code Rectangle cropRect = new Rectangle(50, 50, 150, 150); – James Sep 03 '15 at 09:49
  • `Rectangle cropRect = new Rectangle(50, 50, 690, 690);` – Mohit S Sep 03 '15 at 09:51
  • Can we also make square? Also what was issue with my earlier code ? – James Sep 03 '15 at 09:56
  • Can we make square like new rectangle(0,0,640,640) – James Sep 03 '15 at 09:59
  • Yes you can make square from rectangle function. Rentangle function takes four int type parameters. which can make either a rectangle or Square on the basis of the parameters passed to this Object. **2** Draw image have [30 overloaded function](https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawimage(v=vs.110).aspx) you yourself can have a look on it. – Mohit S Sep 03 '15 at 10:01
  • So what was issue with my approach? – James Sep 03 '15 at 10:10
  • I am not sure about that ... Once I have faced the same problem with image cropping and this idea worked which I shared with you. So I thought this might help you. :) – Mohit S Sep 03 '15 at 10:31
  • i tried but it cuts the content of image so its of no use. Am i right? – James Sep 03 '15 at 14:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88718/discussion-between-happy-and-mohit-shrivastava). – James Sep 03 '15 at 15:18