How to crop an image using C#?

后端 未结 14 1082
忘掉有多难
忘掉有多难 2020-11-22 05:16

How can I write an application that will crop images in C#?

14条回答
  •  猫巷女王i
    2020-11-22 06:12

    You can use Graphics.DrawImage to draw a cropped image onto the graphics object from a bitmap.

    Rectangle cropRect = new Rectangle(...);
    Bitmap src = Image.FromFile(fileName) as Bitmap;
    Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
    
    using(Graphics g = Graphics.FromImage(target))
    {
       g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), 
                        cropRect,                        
                        GraphicsUnit.Pixel);
    }
    

提交回复
热议问题