裁剪图像并将其放入图片框中

时间:2016-03-20 01:43:38

标签: c# crop

即时尝试从精灵表中裁剪图像并将其放入图片框但不起作用,欢迎任何想法

        private void Form1_Load(object sender, EventArgs e)
    {
        Image Result = Crop(@"C:\Users\William\Documents\Sprites\Player\Male\Default\Light.png", 40, 60, 367, 701);
        pictureBox1.Image = Result;
    }

    public Image Crop(string img, int width, int height, int x, int y)
    {
        try
        {
            Image image = Image.FromFile(img);
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            bmp.SetResolution(80, 60);

            Graphics gfx = Graphics.FromImage(bmp);
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
            // Dispose to free up resources
            image.Dispose();
            bmp.Dispose();
            gfx.Dispose();

            return bmp;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return null;
        }
    }

我认为它的作物功能不对,但我不确定

1 个答案:

答案 0 :(得分:0)

这是毫无意义的

bmp.Dispose();
return bmp;

您正在返回一个无法用于任何内容的位图,因为它已经被处理掉了。