Crop image in c#

后端 未结 4 1457
梦如初夏
梦如初夏 2021-01-19 07:11

I have a image that I want to crop it when I press a button on the form. I have the following code that is run when the button is pressed, but it doesn\'t do anything to the

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 07:22

    I created a method for one of my projects, here is the method try using it and see if it works:

       public void ResizeImage(string sImageFile, decimal dWidth, decimal dHeight, string sOutputFile)
        {
            Image oImg = Bitmap.FromFile(sImageFile);
            Bitmap oBMP = new Bitmap(decimal.ToInt16(dWidth), decimal.ToInt16(dHeight));
    
            Graphics g = Graphics.FromImage(oBMP);
            g.PageUnit = pgUnits;
            g.SmoothingMode = psMode;
            g.InterpolationMode = piMode;
            g.PixelOffsetMode = ppOffsetMode;
    
            g.DrawImage(oImg, 0, 0, decimal.ToInt16(dWidth), decimal.ToInt16(dHeight));
    
            ImageCodecInfo oEncoder = GetEncoder();
            EncoderParameters oENC = new EncoderParameters(1);
    
            oENC.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, plEncoderQuality);
    
            oImg.Dispose();
    
            oBMP.Save(sOutputFile, oEncoder, oENC);
            g.Dispose();
    
        }
    

提交回复
热议问题