C# "Parameter Is Not Valid." Creating New Bitmap

C# Parameter is not valid. creating new bitmap

Keep in mind, that is a LOT of memory you are trying to allocate with that Bitmap.

Refer to http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/37684999-62c7-4c41-8167-745a2b486583/

.NET is likely refusing to create an image that uses up that much contiguous memory all at once.

Slightly harder to read, but this reference helps as well:

Each image in the system has the amount of memory defined by this formula:

bit-depth * width * height / 8

This means that an image 40800 pixels by 4050 will require over 660
megabytes of memory.

new Bitmap - Parameter is not valid

Are you sure the image is located in that directory? (Two dirs up from where your exe is running and then in the images dir). If you're not sure on where to place the file, you can print the path with:

Console.WriteLine(new System.IO.FileInfo("..//..//images//brick.jpg").FullName);

Parameter is not valid exception while trying to create a new bitmap object

Since this is only happening when you process large batches, it's probably a memory issue. The Bitmap contains unmanaged resources, so you should call Dispose on it when you're finished with it. The best way to do this is implicitly via a using statement:

using (Bitmap bmp = new Bitmap(files2.FullName))
{
// Process the bitmap here
}

Parameter is not valid when i try to use bitmap

i have to dispose that because of out of memory exception

No, you have to dispose images you don't use any more. Proper code is:

   Bitmap map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height))
using (Graphics graph = Graphics.FromImage(map)) {
graph.Clear(this.BackColor);
foreach (TimeTable t in OnlineTrainList.ToList()) {
Rectangle rectTrainState = new Rectangle(...);
graph.FillRectangle(RedBrush, rectTrainState);
graph.DrawString(...);
}
}
if (pictureBoxonlineTrain.Image != null) pictureBoxonlineTrain.Image.Dispose();
pictureBoxonlineTrain.Image = map;

Parameter is not valid when creating a bitmap

Creating a bitmap from an EmguImage did not work. Elected to save the image to disk, read all the bytes and then create a bitmap

Parameter is invalid when creating a new bitmap pointing to a filepath

  • Try a fully qualified path to the image file with double backslashes ("C:\test\abc.jpg")
  • Determine if the jpeg file is valid for .net's image codec
    • Create jpeg in MS Paint
    • Create jpeg in Gimp, Photoshop, ...
    • Use imagemagick to convert the jpeg from jpeg to png and then back to jpeg
    • Use jpegtran to optimize the jpeg to force rewriting of the jpeg header
  • Use imagemagick's identify or gimp to determine what is in the jpeg (it could be a different image file format or jpeg 2000)
  • In .net code, use File or FileInfo calls to verify that the file exists, has non-zero length and is acessible

If the file is on a network drive and has just been written, it may take a second or two for the file to be released from exclusive use even after the file has been closed by the creator of the file.

Using (Bitmap image = new Bitmap(path)) Error.. Parameter not valid?

Getting an Invalid Parameter Exception for a new Bitmap(String), where the file exists generally means that the file content is invalid and can't be parsed by any of the image type handlers.

One of the most common reasons for this is that the underlying file is of 0 size.



Related Topics



Leave a reply



Submit