简体   繁体   中英

When I try to save an image I get the error “A generic error occurred in GDI+.”

When I run this code, on the line I commented I always get an "A generic error occurred in GDI+." run time error

    private void ConstructFromResourceSaveAsGif()
    {
        Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");

        //This line
        bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);
    }

You can just use the code

private void ConstructFromResourceSaveAsGif()
{
    Bitmap bmp1 = new Bitmap("Button.bmp");

    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);
}

and it will still work, unless you are trying to accomplish what @DanF is saying.

EDIT

Here's a quick app I used to test it out:

class Program
{
    static void Main(string[] args)
    {
        Bitmap bmp1 = new Bitmap("C:/donut.jpg");

        bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);

        Console.WriteLine("Save Success");
        Console.Read();
    }
}

This constructor combines the namespace of the given type with the string name of the resource and looks for a match in the assembly manifest. For example you can pass in the Button type and Button.bmp to this constructor and it will look for a resource named System.Windows.Forms.Button.bmp.

Do you have a resource named that? If not, that'll be your problem. "A generic error occured..." is GDI+'s not very friendly way of throwing its hands in the air and saying "it broke but I'm not going to tell you why"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM