本文介绍了GDI +通用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When my images are being loaded from my database on my web server, I see the following error:

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

All my code is attempting to do is load the image, can anybody take a look and let me know what I'm doing wrong?

注意 - 这是有效的,如果我测试它在我的本地机器,但不是当我部署到我的Web服务器

Note - This works if I test it on my local machine, but not when I deploy it to my web server.

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

错误发生在行上:

UPDATE

我已将我的代码更改为此位,问题仍然发生:

I've changed my code to this, bit the issue still happens:

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

    } }

谢谢。

推荐答案

可能出于同样的原因,有问题 - 因为对于从 Stream 构造的映像的生命周期,流不能

Probably for the same reason that this guy was having problems - because the for a lifetime of an Image constructed from a Stream, the stream must not be destroyed.

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

So if your GetImage function constructs the returned image from a stream (e.g. a MemoryStream) and then closes the stream before returning the image then the above will fail. My guess is that your GetImage looks a tad like this:

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

如果是这种情况, $ c> GetImage 直接返回 MemoryStream (或者可能的字节数组),以便可以创建 Image ProcessRequest 方法中的code>实例,并且仅当该图像的处理完成时才处理该流。

If this is the case then try having GetImage return the MemoryStream (or possibly the byte array) directrly so that you can create the Image instance in your ProcessRequest method and dispose of the stream only when the processing of that image has completed.

这在中提及,但其在小打印。

This is mentioned in the documentation but its kind of in the small print.

这篇关于GDI +通用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:06