需要幫助。我想調整某一圖片,但是當我運行這段代碼它給我的錯誤{「GDI +中發生一般性錯誤。」}調整圖片C#
private void btnPic_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Images|*.bmp;*.jpg;*.gif|All files|*.*";
if (open.ShowDialog(this) == DialogResult.OK)
{
var image = Image.FromFile(open.FileName);
var newImage = ScaleImage(image, 300, 400);
newImage.Save(@open.FileName, ImageFormat.Png);
}
}
public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
{
var ratioX = (double)maxWidth/image.Width;
var ratioY = (double)maxHeight/image.Height;
var ratio = Math.Min(ratioX, ratioY);
var newWidth = (int)(image.Width * ratio);
var newHeight = (int)(image.Height * ratio);
var newImage = new Bitmap(newWidth, newHeight);
Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
return newImage;
}
在你得到錯誤代碼到底是哪行? –
什麼是@ open.FileName? –
如果將圖像保存爲新文件名,它會工作嗎? – CodeCaster