GDI 中发生一般错误;

发布于 2024-07-25 06:40:16 字数 4594 浏览 3 评论 0原文

[ExternalException (0x80004005): A generic error occurred in GDI+.]
   IpitchitImageHandler.Data.ImageRepository.AddNewTempImage(Stream image, String extension, Guid PageId, Guid ImageId, ImageTransformCollection toDoTransforms) +1967
   IpitchitImageHandler.Data.ImageRepository.AddNewTempImage(Stream image, String extension, Guid PageId, Guid ImageId) +85
   IpitchitWeb.Sell.Controls.UploadImagesSubstep.UploadImages(Object sender, EventArgs e) in F:\Documents and Settings\Vjeran\My Documents\Visual Studio 2008\Projects\Ipitchit\IpitchitWeb\Sell\Controls\UploadImagesSubstep.ascx.cs:88
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

我的代码是:

public void AddNewTempImage(Stream image, string extension, Guid PageId, Guid ImageId, 
    ImageTransformCollection toDoTransforms)
{
    //mapping steam to memory stream so it does support the seek

    MemoryStream targetStream = new MemoryStream(ReadStream(image, 1024));
    Image ImageToTransform=null;
    Image transformedImage = null;
    string storagePath = ImageTransformManager.Config.StorageServerPhysicalPath;
    Uri storageUrl = new Uri(ImageTransformManager.Config.StorageServerUrl);

    //string TempPath = Path.Combine(storagePath, GenerateFileName(extension));
    //SaveStream(TempPath, image);
    //File.WriteAllBytes(TempPath, ReadStream(image, 1024));

    if (!HttpContext.Current.User.Identity.IsAuthenticated)
        throw new Exception("Nonauthenticated users image submition is not supported");

        try
        {            
            foreach (ImageTransform transform in toDoTransforms)
            {
            ImageRepositoryTempImage newimage = new ImageRepositoryTempImage();
            newimage.ImageGuid = ImageId;
            newimage.PageGuid = PageId;
            newimage.CreatedBy = HttpContext.Current.User.Identity.Name;
            newimage.CreatedDate = DateTime.UtcNow;
            newimage.Format = transform.OutputType;
            newimage.Width = transform.Width;
            newimage.Height = transform.Height;
            newimage.Watermark = transform.UseWaterMark;
            string filename = GenerateFileName(transform.OutputType);
            string fullStoragePath = Path.Combine(storagePath, Path.Combine(transform.StorageFolder, filename));
            string fullStorageUrl = CombineUri(storageUrl, Path.Combine(transform.StorageFolder, filename));
            newimage.PhysicalStoragePath = fullStoragePath;
            newimage.StoragePath = fullStorageUrl;
            CheckOrAddImageTransform(transform);
            var ImgRepTransform = GetTransformation(transform);
            newimage.ImageRepositoryTransformation = ImgRepTransform;
            newimage.TransformId = ImgRepTransform.Id;

            Bitmap uploaded = new Bitmap(image);

            ImageToTransform = (Image)uploaded.Clone();
            uploaded.Dispose();
            transformedImage = transform.Transform(ImageToTransform);
            AddNewTempImage(newimage);
            //adding named watermark and transformation
            string wname = ImageTransformManager.Config.WaterMarkName;
            string wpath = ImageTransformManager.Config.WaterMarkPath;
            ChechOrAddWaterMark(wname, wpath);


            if (!(string.IsNullOrEmpty(wname) && string.IsNullOrEmpty(wpath)))
                newimage.ImageRepositoryWaterMark = GetWatermark(wname, wpath);

            transformedImage.Save(fullStoragePath, GetFormat(newimage.Format));
            }
        }
        catch (System.Exception ex)
        {
            ErrorHandling.LogErrorEvent("Add new temp image method", ex);
            throw ex;
        }
        finally
        {
            //File.Delete(TempPath);
            if (ImageToTransform!=null)
                ImageToTransform.Dispose();
            image.Dispose();
            targetStream.Dispose();
            if (transformedImage != null)
            transformedImage.Dispose();
        }

}

在我的本地计算机上一切正常 - 但仍然发生..在服务器(2003)上 - 我有文件夹权限..以及一切...

[ExternalException (0x80004005): A generic error occurred in GDI+.]
   IpitchitImageHandler.Data.ImageRepository.AddNewTempImage(Stream image, String extension, Guid PageId, Guid ImageId, ImageTransformCollection toDoTransforms) +1967
   IpitchitImageHandler.Data.ImageRepository.AddNewTempImage(Stream image, String extension, Guid PageId, Guid ImageId) +85
   IpitchitWeb.Sell.Controls.UploadImagesSubstep.UploadImages(Object sender, EventArgs e) in F:\Documents and Settings\Vjeran\My Documents\Visual Studio 2008\Projects\Ipitchit\IpitchitWeb\Sell\Controls\UploadImagesSubstep.ascx.cs:88
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

my code is:

public void AddNewTempImage(Stream image, string extension, Guid PageId, Guid ImageId, 
    ImageTransformCollection toDoTransforms)
{
    //mapping steam to memory stream so it does support the seek

    MemoryStream targetStream = new MemoryStream(ReadStream(image, 1024));
    Image ImageToTransform=null;
    Image transformedImage = null;
    string storagePath = ImageTransformManager.Config.StorageServerPhysicalPath;
    Uri storageUrl = new Uri(ImageTransformManager.Config.StorageServerUrl);

    //string TempPath = Path.Combine(storagePath, GenerateFileName(extension));
    //SaveStream(TempPath, image);
    //File.WriteAllBytes(TempPath, ReadStream(image, 1024));

    if (!HttpContext.Current.User.Identity.IsAuthenticated)
        throw new Exception("Nonauthenticated users image submition is not supported");

        try
        {            
            foreach (ImageTransform transform in toDoTransforms)
            {
            ImageRepositoryTempImage newimage = new ImageRepositoryTempImage();
            newimage.ImageGuid = ImageId;
            newimage.PageGuid = PageId;
            newimage.CreatedBy = HttpContext.Current.User.Identity.Name;
            newimage.CreatedDate = DateTime.UtcNow;
            newimage.Format = transform.OutputType;
            newimage.Width = transform.Width;
            newimage.Height = transform.Height;
            newimage.Watermark = transform.UseWaterMark;
            string filename = GenerateFileName(transform.OutputType);
            string fullStoragePath = Path.Combine(storagePath, Path.Combine(transform.StorageFolder, filename));
            string fullStorageUrl = CombineUri(storageUrl, Path.Combine(transform.StorageFolder, filename));
            newimage.PhysicalStoragePath = fullStoragePath;
            newimage.StoragePath = fullStorageUrl;
            CheckOrAddImageTransform(transform);
            var ImgRepTransform = GetTransformation(transform);
            newimage.ImageRepositoryTransformation = ImgRepTransform;
            newimage.TransformId = ImgRepTransform.Id;

            Bitmap uploaded = new Bitmap(image);

            ImageToTransform = (Image)uploaded.Clone();
            uploaded.Dispose();
            transformedImage = transform.Transform(ImageToTransform);
            AddNewTempImage(newimage);
            //adding named watermark and transformation
            string wname = ImageTransformManager.Config.WaterMarkName;
            string wpath = ImageTransformManager.Config.WaterMarkPath;
            ChechOrAddWaterMark(wname, wpath);


            if (!(string.IsNullOrEmpty(wname) && string.IsNullOrEmpty(wpath)))
                newimage.ImageRepositoryWaterMark = GetWatermark(wname, wpath);

            transformedImage.Save(fullStoragePath, GetFormat(newimage.Format));
            }
        }
        catch (System.Exception ex)
        {
            ErrorHandling.LogErrorEvent("Add new temp image method", ex);
            throw ex;
        }
        finally
        {
            //File.Delete(TempPath);
            if (ImageToTransform!=null)
                ImageToTransform.Dispose();
            image.Dispose();
            targetStream.Dispose();
            if (transformedImage != null)
            transformedImage.Dispose();
        }

}

On my local machine everything works - but still happens.. on server (2003) - i have folder permissions .. and everything...

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

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

发布评论

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

评论(11

面如桃花 2024-08-01 06:40:17

我刚才遇到了同样的错误,谷歌帮助我找到了答案:
加载/保存位图会导致文件锁定。

解决方法是创建其他位图,保存并释放它:

Bitmap tmp = new Bitmap(bitmapToBeSaved);
tmp.SaveToFile(文件名);

I've got the same error just now and Google help me to find answer:
Loading/Saving bitmap causes the locked file.

Workaround is create othe bitmap, save and let it release:

Bitmap tmp = new Bitmap(bitmapToBeSaved);
tmp.SaveToFile(fileName);

勿忘心安 2024-08-01 06:40:17

您的本地计算机是 Vista 或 Windows 7,而您的服务器是 Windows Server 2003? 我相信 GDI+ 实现有所不同,并且您不会在较新的操作系统上看到错误。 我现在刚刚遇到这个问题,这是我遇到的事实之一。

Is your local machine Vista or Windows 7, and your server Windows Server 2003? I believe the GDI+ implementation differs, and you won't see the error on the newer OSs. I'm just running into this issue now, and that's one of the factoids I've come across.

美男兮 2024-08-01 06:40:17

确保 IIS_WPG 对您的上传文件夹以及 ASPNET 拥有正确的权限

我刚刚遇到了同样的问题,这解决了它。

如果需要的话,不要忘记通过子文件夹传播权限(我可能已经忘记了..:))

Make sure IIS_WPG has the correct permissions on your upload folder and also ASPNET.

I just had the same problem and this fixed it.

Don't forget to propagate the permissions through your sub folders if required too ( I may have forgotten that.. :) )

乙白 2024-08-01 06:40:17

不要使用直接路径

    System.Drawing.Image img = Base64ToImage(_ImagePath);
    img.Save(_attachmentPath, ImageFormat.Jpeg);

使用 Server.MapPath 它将创建目录如果需要

    System.Drawing.Image img = Base64ToImage(_ImagePath);
    img.Save(Server.MapPath(_attachmentPath), ImageFormat.Jpeg);

Do not use direct path

    System.Drawing.Image img = Base64ToImage(_ImagePath);
    img.Save(_attachmentPath, ImageFormat.Jpeg);

Use Server.MapPath and it will create Directory If needed

    System.Drawing.Image img = Base64ToImage(_ImagePath);
    img.Save(Server.MapPath(_attachmentPath), ImageFormat.Jpeg);
枫以 2024-08-01 06:40:17

请确定您用于图像的路径,我也面临同样的错误。检查您正在使用的所有路径。

Please be sure about the path you are using for images,I also face the same error.Check all paths you are using.

零時差 2024-08-01 06:40:17

尝试将应用程序池身份设置从 ApplicationPoolIdentity 更改为 LocalSystem 以验证其权限问题。

但是,不要长期使用此设置,因为它存在安全风险; 仅将其用作诊断。

Try changing you application pool identity setting from ApplicationPoolIdentity to LocalSystem to verify its a permission problem.

However, don't use this setting long-term as it's a security risk; use it only as a diagnosis.

我纯我任性 2024-08-01 06:40:16

我非常讨厌这个错误。
通用错误可能是有史以来最无用的错误描述。

当我遇到这个问题时,问题总是与文件 IO 有关。

这是我在笔记中保存的修复列表 - 不确定这些是否适用,但它们通常对我有用。

  • 检查文件路径
    • 确保父目录存在
    • 确保路径包含文件名和扩展名
    • 使用 server.MapPath() 创建路径
  • 确保文件不会被写回其源。 如有必要,关闭并重新打开流。

如果我从其他地方偷了这个列表,我深表歉意。 它在我的笔记本里已经有一段时间了,但我不记得它是从哪里来的。

I hate that error with a passion.
Generic Error is possibly the most useless error description ever written.

When I've encountered it the problem as always been related to file IO.

Here is the list of fixes I keep in my notes- Not sure if these apply but they usually do the trick for me.

  • Check File path
    • Make sure that the parent directory exists
    • Ensure that path includes both the filename and extension
    • Use server.MapPath() to create the path
  • Make sure that the file isn't being written back to it's source. Close and reopen the stream if necessary.

My apologies if i stole this list from somewhere else. It has been in my notebook for awhile and I can't remember where it came from.

哥,最终变帅啦 2024-08-01 06:40:16

这里(以及本网站)的每个人都因权限错误而讨论过此问题。 我偶然发现了另一种味道:记忆。 我的内存不足并开始收到此错误。 因此请记住这一点,这是另一个潜在的错误来源。

在我的特定情况下,我在 .NET 应用程序中运行,并针对多个线程运行(线程数量足够少,因此我不会受到 GDI+ 进程范围锁的阻碍太多)。 在主要任务完成后添加“GC.Collect()”似乎并没有对速度产生太大影响,但确实完全消除了内存不足错误。

Everyone here (and on this site) has discussed this as due to a permissions error. I've stumbled over another flavor: memory. I ran out of memory and started receiving this error as well. So keep that in mind as another potential source of error.

In my particular case I was running in a .NET application, and running against a number of threads (few enough that I wasn't being held back too much by the GDI+ process-wide lock). Adding a "GC.Collect()" after major tasks finished didn't seem to affect the speed much, but did completely get rid of the out of memory errors.

叫嚣ゝ 2024-08-01 06:40:16

我刚刚在我的托管网站上遇到了类似的问题(相同的异常):事实证明这是一个权限问题,ASP.NET 帐户试图从其可用范围之外的目录中读取图像文件。

我建议您仔细检查权限以及 web.config 中的 元素,应根据文件所在位置将其设置为“中”或更高。

I just had an similar problem (same exception) on my hosted website: It turned out to be a permissions issue, the ASP.NET account was trying to read image files from a directory outside its available scope.

I suggest you double-check the permissions as well as the <trust> element in web.config, it should be set to "Medium" or higher depending on where the files are located.

泛滥成性 2024-08-01 06:40:16

这个人跟随了我很长时间。 是的,您可以检查权限,但您还应该做的是正确处理您的位图。

bitmap.Dispose();

This one followed me for a long time. Yes you can check permissions but what you also should do is to dispose of your bitmap correctly.

bitmap.Dispose();
和影子一齐双人舞 2024-08-01 06:40:16

我同意权限是这里的问题。

也许是网络服务?

i agree permissions is the problem here.

network service maybe ?

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