发生一般性错误 gdi+ c#

a generic error occurred gdi+ c#

我想在旋转后以相同的名称和路径保存图像。 我在方法保存中遇到错误(发生一般性错误 gdi+

代码如下:

string path = @"mypath";
Bitmap image = new Bitmap(path + aspximage.ImageUrl, true);
image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
//The error is generated here
image.Save(path + aspximage.ImageUrl, ImageFormat.Png);
// I have added this line so that the browser can display it
aspximage.ImageUrl = aspximage.ImageUrl + "&t=" + DateTime.Now.Second;

我从硬盘驱动器的本地路径获取图像。为此,我使用 ashx 处理程序,所以我的 imageUrl 类似于 "Handler.ashx?n=nameimage.png"

这是Handler.ashx的代码:

public void ProcessRequest (HttpContext context) {

    string imgName = context.Request.QueryString["n"];
    context.Response.ContentType = "image/png";
    string path = @"myPath" + imgName;
    Image image = Image.FromFile(path);
    image.Save(context.Response.OutputStream, ImageFormat.Png);
}

如果我删除这一行

// I have added this line so that the browser can display it
aspximage.ImageUrl = aspximage.ImageUrl + "&t=" + DateTime.Now.Second;

我的代码运行良好,但浏览器无法重新加载我的图像。

如果我保留它,我的代码运行良好,但在同一图像上 3 次或更多次执行调用 后会生成错误。

你有什么建议?

感谢您的帮助。

终于找到错误了

在 context.Response.outputStream 中保存图像后我应该关闭文件 "Handler.ashx"

为此我添加了 image.Dispose()

这是 Handler.ashx 的新代码:

public void ProcessRequest (HttpContext context) {

    string imgName = context.Request.QueryString["n"];
    context.Response.ContentType = "image/png";
    string path = @"mypath" + imgName;
    Image image = Image.FromFile(path);
    image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
    //I added this line
    image.Dispose();
}