“GDI 中发生一般错误”显示上传的图像时出错

发布于 2024-09-04 20:26:24 字数 2215 浏览 2 评论 0原文

我正在使用以下代码来显示已从我的 asp.net mvc(C#) 应用程序保存在我的数据库中的图像:。

    public ActionResult GetSiteHeaderLogo()
        {
            SiteHeader _siteHeader = new SiteHeader();
            Image imgImage = null;

            long userId = Utility.GetUserIdFromSession();

            if (userId > 0)
            {
                _siteHeader = this.siteBLL.GetSiteHeaderLogo(userId);

                if (_siteHeader.Logo != null && _siteHeader.Logo.Length > 0)
                {
                    byte[] _imageBytes = _siteHeader.Logo;
                    if (_imageBytes != null)
                    {
                        using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes))
                        {
                             imgImage = Image.FromStream(imageStream);
                        }
                    }
                    string sFileExtension = _siteHeader.FileName.Substring(_siteHeader.FileName.IndexOf('.') + 1, 

_siteHeader.FileName.Length - (_siteHeader.FileName.IndexOf('.') + 1));

                    Response.ContentType = Utility.GetContentTypeByExtension(sFileExtension.ToLower());
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BufferOutput = false;
                    if (imgImage != null)
                    {
                         ImageFormat _imageFormat = Utility.GetImageFormat(sFileExtension.ToLower());
                         imgImage.Save(Response.OutputStream, _imageFormat);
                         imgImage.Dispose();
                    }
              }
           }

       return new EmptyResult();
    }

当我上传原始图像时它工作正常。但是当我上传任何下载的图像时,它会抛出以下错误:

 System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
   at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(Stream stream, ImageFormat format)

For。例如:当我上传原始图像时,它在我的网站中显示为徽标,并且我从该网站下载了该徽标,当我重新上传相同的下载图像时,它会抛出上述错误。这对我来说似乎很奇怪,无法找到为什么会发生。对此有什么想法吗?

i am using the following code to show the image that has been saved in my database from my asp.net mvc(C#) application:.

    public ActionResult GetSiteHeaderLogo()
        {
            SiteHeader _siteHeader = new SiteHeader();
            Image imgImage = null;

            long userId = Utility.GetUserIdFromSession();

            if (userId > 0)
            {
                _siteHeader = this.siteBLL.GetSiteHeaderLogo(userId);

                if (_siteHeader.Logo != null && _siteHeader.Logo.Length > 0)
                {
                    byte[] _imageBytes = _siteHeader.Logo;
                    if (_imageBytes != null)
                    {
                        using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes))
                        {
                             imgImage = Image.FromStream(imageStream);
                        }
                    }
                    string sFileExtension = _siteHeader.FileName.Substring(_siteHeader.FileName.IndexOf('.') + 1, 

_siteHeader.FileName.Length - (_siteHeader.FileName.IndexOf('.') + 1));

                    Response.ContentType = Utility.GetContentTypeByExtension(sFileExtension.ToLower());
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BufferOutput = false;
                    if (imgImage != null)
                    {
                         ImageFormat _imageFormat = Utility.GetImageFormat(sFileExtension.ToLower());
                         imgImage.Save(Response.OutputStream, _imageFormat);
                         imgImage.Dispose();
                    }
              }
           }

       return new EmptyResult();
    }

It works fine when i upload original image. But when i upload any downloaded images, it throws the following error:

 System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
   at System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
   at System.Drawing.Image.Save(Stream stream, ImageFormat format)

For. Ex: When i upload the original image, it shows as logo in my site and i downloaded that logo from the site and when i re-upload the same downloaded image, it throws the above error. It seems very weird to me and not able to find why its happening. Any ideas on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彡翼 2024-09-11 20:26:24

我猜你的问题就在这里:

using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes)) 

    { 
      imgImage = Image.FromStream(imageStream); 
    } 

因为使用 .FromStream 之后,Image 拥有该流,如果你关闭它可能会非常沮丧。要验证这是否是问题所在,您可以尝试:

using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes)) 
{ 
  imgImage = new Bitmap( Image.FromStream(imageStream) ); 
} 

I'd guess that your problem lies here:

using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes)) 

    { 
      imgImage = Image.FromStream(imageStream); 
    } 

Because after using .FromStream, the Image owns the stream and might be very upset if you close it. To verify if that's the problem you can just try:

using (System.IO.MemoryStream imageStream = new System.IO.MemoryStream(_imageBytes)) 
{ 
  imgImage = new Bitmap( Image.FromStream(imageStream) ); 
} 
是伱的 2024-09-11 20:26:24

我发现错误通常来自文件访问问题。我意识到,这听起来很明显,但请仔细检查文件路径是否正确、文件是否存在,以及 IIS 进程是否具有该文件的权限。

I've found that error usually comes from a file access problem. Sounds obvious, I realize, but double-check that the file path is correct and that the file exists, and also that the IIS process has permissions to that file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文