Cropping picture, getting Rectangle from X,Y? [closed]

大憨熊 提交于 2019-12-25 19:00:11

问题


I'm trying to crop a picture in C# from integer x, y coordinates.. I just can't figure out how to get it?

public void doCroppedImage(int pointX, int pointY)
{
    Rectangle cropRect = //???
}

回答1:


You can use this code. It returns the cropped image.

public static Bitmap CropImage(Image source, int x,int y,int width,int height)
{
    Rectangle crop = new Rectangle(x, y, width, height);

    var bmp = new Bitmap(crop.Width, crop.Height);
    using (var gr = Graphics.FromImage(bmp))
    {
        gr.DrawImage(source, new Rectangle(0, 0, bmp.Width, bmp.Height), crop, GraphicsUnit.Pixel);
    }
    return bmp;
} 

But one comment, to crop an image you have to know not only the x and y coordinates of cropping point but also the width and height of the cropped image.



来源:https://stackoverflow.com/questions/13217156/cropping-picture-getting-rectangle-from-x-y

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!