How to crop an image using C#?

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

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

14条回答
  •  無奈伤痛
    2020-11-22 06:03

    Only this sample working without problem:

    var crop = new Rectangle(0, y, bitmap.Width, h);
    var bmp = new Bitmap(bitmap.Width, h);
    var tempfile = Application.StartupPath+"\\"+"TEMP"+"\\"+Path.GetRandomFileName();
    
    
    using (var gr = Graphics.FromImage(bmp))
    {
        try
        {
            var dest = new Rectangle(0, 0, bitmap.Width, h);
            gr.DrawImage(image,dest , crop, GraphicsUnit.Point);
            bmp.Save(tempfile,ImageFormat.Jpeg);
            bmp.Dispose();
        }
        catch (Exception)
        {
    
    
        }
    
    }
    

提交回复
热议问题