裁剪图像并将其放入图片框中
Cropping an image and putting it in a picture box
我正在尝试从 sprite 裁剪图像 sheet 并将其放入图片框中,但它不起作用,欢迎提出任何想法
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;
}
}
我不认为它的裁剪功能有问题,但我不确定
这毫无意义
bmp.Dispose();
return bmp;
您返回的位图不能用于任何用途,因为它已经被处置。
我正在尝试从 sprite 裁剪图像 sheet 并将其放入图片框中,但它不起作用,欢迎提出任何想法
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;
}
}
我不认为它的裁剪功能有问题,但我不确定
这毫无意义
bmp.Dispose();
return bmp;
您返回的位图不能用于任何用途,因为它已经被处置。