GDI +通用错误

时间:2011-11-28 12:24:48

标签: c#-4.0

当从我的网络服务器上的数据库加载我的图像时,我看到以下错误:

  

GDI +中发生了一般性错误。在   System.Drawing.Image.Save(Stream stream,ImageCodecInfo encoder,   EncoderParameters encoderParams)at   System.Drawing.Image.Save(Stream stream,ImageFormat格式)at   MyWeb.Helpers.ImageHandler.ProcessRequest(HttpContext context)

我的所有代码都试图加载图片,任何人都可以看看,让我知道我做错了什么?

注意 - 如果我在本地计算机上测试它,则会有效,但在将其部署到我的Web服务器时则不行。

public void ProcessRequest(HttpContext context)
{
    context.Response.Clear();          

    if (!String.IsNullOrEmpty(context.Request.QueryString["imageid"]))         
    {
        int imageID = Convert.ToInt32(context.Request.QueryString["imageid"]);
        int isThumbnail = Convert.ToInt32(context.Request.QueryString["thumbnail"]);

        // Retrieve this image from the database
        Image image = GetImage(imageID);

        // Make it a thumbmail if requested
        if (isThumbnail == 1)
        {
            Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            image = image.GetThumbnailImage(200, 200, myCallback, IntPtr.Zero);
        }

        context.Response.ContentType = "image/png";             

        // Save the image to the OutputStream             
        image.Save(context.Response.OutputStream, ImageFormat.Png);
    }         
    else         
    {             
        context.Response.ContentType = "text/html";             
        context.Response.Write("<p>Error: Image ID is not valid - image may have been deleted from the database.</p>");         
    } 
}

错误发生在该行:

  

image.Save(context.Response.OutputStream,ImageFormat.Png);

更新

我已将代码更改为此,但问题仍然存在:

var db = new MyWebEntities();

var screenshotData = (from screenshots in db.screenshots
                      where screenshots.id == imageID
                      select new ImageModel
                      {
                           ID = screenshots.id,
                           Language = screenshots.language,
                           ScreenshotByte = screenshots.screen_shot,
                           ProjectID = screenshots.projects_ID
                      });

foreach (ImageModel info in screenshotData) 
 {
    using (MemoryStream ms = new MemoryStream(info.ScreenshotByte))
    {
         Image image = Image.FromStream(ms);

         // Make it a thumbmail if requested
         if (isThumbnail == 1)
         {
              Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
              image = image.GetThumbnailImage(200, 200, myCallback, IntPtr.Zero);
         }

         context.Response.ContentType = "image/png";

         // Save the image to the OutputStream             
         image.Save(context.Response.OutputStream, ImageFormat.Png);

    } }

感谢。

1 个答案:

答案 0 :(得分:2)

可能由于this guy遇到问题的原因相同 - 因为从Stream构造的图像的一生中,不得销毁该流。

因此,如果您的GetImage函数从流构建返回的图像(例如MemoryStream),然后在返回图像之前关闭流,则上述操作将失败。我猜你的GetImage看起来有点像这样:

Image GetImage(int id)
{
    byte[] data = // Get data from database
    using (MemoryStream stream = new MemoryStream(data))
    {
        return Image.FromStream(data);
    }
}

如果是这种情况,请尝试让GetImage直接返回MemoryStream(或可能是字节数组),以便您可以在Image中创建ProcessRequest个实例方法并仅在完成该图像的处理时处理流。

the documentation中提到了这一点,但它只是小字体中的一种。