ExternalException"GDI +中发生一般错误." [英] ExternalException "A generic error occurred in GDI+."

查看:106
本文介绍了ExternalException"GDI +中发生一般错误."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个图像合并项目.(图像合并)第一个前景图像位于背景图像的(0,0)位置.通过使用numericUpdown控件可以更改位置.代码在下面

I''m developing a project of image merging.(Image Combine) 1st foreground image is on the(0,0) position of background image. The position is changing by using a numericUpdown Control. The code is below

private void nudXPositions_ValueChanged(object sender, EventArgs e) 
{
    {
        lock (typeof(FrmImageMerge))
        {
            nudXPositions.Maximum = imgBackWidth;

            if (pbMergeImagePreview.Image != null)
            {
                pbMergeImagePreview.Image.Dispose();
            }

            pbMergeImagePreview.InitialImage = null;

            posX = decimal.ToInt16(nudXPositions.Value);
            posY = decimal.ToInt16(nudYPosition.Value);
            MergeImages(tbxBackImage.Text, tbxForeImage.Text);
            lblMergeImagePreview.Text = "";
            pbBackgroundImagePreview.Refresh();
            try
            {
                Thread.Sleep(100);
                Image image1 = Image.FromFile(tempName);
                this.pbMergeImagePreview.Image = image1;
                pbBackgroundImagePreview.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            btnSaveImageAs.Enabled = true;
        }
    }
}



然后调用MergeImage()方法.该方法如下



then call MergeImage() method. That method is below

private void MergeImages(string ImageBack,string ImageFore) 
{ 
    pbMergeImagePreview.Image = null; pbMergeImagePreview.Refresh(); 
    //try 
    //{ 
    backExtension = Path.GetExtension(tbxBackImage.Text); 
        System.Drawing.Graphics myGraphic = null; 
        Image imgB; imgB = Image.FromFile(ImageBack); 
        Image imgF; imgF = Image.FromFile(ImageFore); 
        Image m; m = Image.FromFile(ImageBack); 
        myGraphic = System.Drawing.Graphics.FromImage(m); 
        myGraphic.DrawImageUnscaled(imgB,0,0); 
        myGraphic.DrawImageUnscaled(imgF,posX,posY);

        myGraphic.Save();
        getMyDocument = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        tempName = string.Format("{0}{1}{2}{3}{4}{5}",
                                 getMyDocument,
                                 "\\",
                                 "Merge Image",
                                 "\\",
                                 "Imageback", 
                                 backExtension);

        if (File.Exists(tempName))
        {
            File.Delete(tempName);
        }
        m.Save(tempName);       

        imgB.Dispose();
        imgF.Dispose();
        m.Dispose();
        myGraphic.Dispose();

    //}
    //catch (Exception ex)
    //{
    //    MessageBox.Show(ex.Message);
    //}
}



当执行程序时以及更改位置时,位置ImageFore会根据位置更改.它保存到一个临时图像.然后,如果我们再次更改位置,则将重新创建临时图像并将其替换为旧图像.但是,当我一段时间执行此过程时,将会有一个例外.

External Exception has Occurred.

这也是

A generic error occurred in GDI+.

我尝试了很多,然后搜索浏览器.我无法解决我的问题.谢谢您



When program is execute and when change the position ImageFore is change according to the position. It''s save to a temporary image. Then If we change the position again temporary image is recreated and it replace to the old image. But when I do this procedure for a some time there will be an exception;

External Exception has Occurred.

Also this is a

A generic error occurred in GDI+.

I tried a lot and search browser. It I couldn''t solve my problem. Thank You

推荐答案

您应该将图像加载到内存中. Image.FromFile锁定它,这就是为什么您无法保存它.

使用例如读取图像. FileStream,然后使用Image.FromStream.可能对您有帮助.
You should load images in your memory. Image.FromFile lock it thats why you can''t save it.

Read image using for eg. FileStream and then use Image.FromStream. It might help you.


尝试写文件时,可能是权限问题(试图将文件保存到您没有权限的文件夹中) "如果您实际上并未关闭用于加载文件的流,则尝试将图像保存回相同的文件.
It could be a permissions problem (trying to save the file to a folder you don''t have privileges for) when trying to write the file, or that you''re trying to save the image back to the same file if you didn''t actually close the stream used to load the file to begin with.


浏览http://support.microsoft.com/?id=814675 [
Go through http://support.microsoft.com/?id=814675[^] and use the workaround mentioned there.


这篇关于ExternalException"GDI +中发生一般错误."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆