Ben
Ben

Reputation: 5777

Bad parameter when using save() on Image

I am having a bit of trouble with saving an Image, it says "Bad paremeter" on the line where I try and save the image.

I'm not sure if it's how I am creating the image or if it's just saving that's the problem.

public static void Fullscreen()
{
    string fileName = Helper.RandomStr(10) + ".png";

    try
    {
        var image = ScreenCapture.CaptureFullscreen();
        image.Save(fileName, ImageFormat.Png);

        System.Diagnostics.Process.Start(fileName);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Unable to capture fullscreen because: " + ex.ToString() + "\r\n\r\nFile: " + fileName);
    }
}

Edit:

Here is the method that gets the Bitmap

    public static Bitmap CaptureFullscreen()
    {
        using (Bitmap bmp = new Bitmap(ScreenDimensions.Width, ScreenDimensions.Height))
        {
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(Point.Empty, Point.Empty, bmp.Size);
            }

            return bmp;
        }
    }

Upvotes: 0

Views: 131

Answers (2)

stepandohnal
stepandohnal

Reputation: 457

Bad parameter is the way GDI+ tells that there was some problem. Its a shame that the errors are not very much descriptive.

First try to wrap image parameter to Bitmap constructor like:

image = new Bitmap(image);

This forces to process the bitmap immediately.

It was even simpler, remove using on the bitmap.

Upvotes: 1

noelicus
noelicus

Reputation: 15055

Try using a known path and see if starts working. If so then you might want a new random string generator that makes valid paths or a different way of naming the file.

Upvotes: 0

Related Questions