System.ArgumentException in System.Drawing.dll "Parameter is invalid"

3.6k Views Asked by At

I am creating a wallpaper changer that selects the newest wallpaper based off some criteria set by the user. Everything should be working fine, in fact I found out the error is caused by not disposing of the image before setting it again so I did that but it still happened.

Here is a snippet of my code that makes two copies of the image. One for a history grid as a thumbnail and one for later if the user wants to save the image.

    WebClient wc = new WebClient();
    byte[] bytes = wc.DownloadData(url);

    if (bytes.Count().Equals(0))
    {
        changeWallpaperTimer.Enabled = true;
    }
    else
    {
        MemoryStream ms = new MemoryStream(bytes);
        memoryStreamImage = System.Drawing.Image.FromStream(ms);
        ms.Dispose();
        ms.Close();

        if (currentWallpaper != null)
        {
            currentWallpaper.Dispose();
        }

        currentWallpaper = new Bitmap(memoryStreamImage);
        dataGridNumber += 1;

        historyDataGrid.Rows.Insert(0, new Bitmap(memoryStreamImage, new Size(100, 100)), title, dataGridNumber, threadID);
        memoryStreamImage.Dispose();
    }
0

There are 0 best solutions below