2016-03-20 33 views
0

我嘗試從一個精靈表裁剪圖像並把它放在一個圖片,但它不工作,任何想法,歡迎裁剪圖像,並把它放在一個圖片框

 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; 
     } 
    } 

我不認爲它的裁切功能那是錯誤的,但林不知道

回答

0

這是毫無意義

bmp.Dispose(); 
return bmp; 

你回來,不能用於任何一個位圖,因爲它已經佈置。

+0

哦葉大聲笑,它現在的作品,謝謝 – Will