Reputation: 75
For some cases only i'm getting this error. "Parameter is not valid Stack Trace at System.Drawing.Bitmap..ctor(Stream stream)" i am little confused how it is working for some records why not for other. Anyone please guide me to find my mistake will be very helpful..,
Following are my codes.,
private void RefreshImage()
{
if (this.dsPatPhoto.dmDoc.Count <= 0) return;
byte[] patImage = null;
byte[] driverLicImage = null;
foreach (CmData.WrCmDoc.DsCmDoc.dmDocRow row in this.dsPatPhoto.dmDoc)
{
if (!row.IsIDDocTypeNull() &&
row.IDDocType == (short)AppCommonCfg.DocType.PatientDriverLicense)
{
if (!row.IsDocImageNull())
driverLicImage = row.DocImage;
}
else
{
if (!row.IsDocImageNull())
patImage = row.DocImage;
}
}
System.IO.MemoryStream stream;
if (patImage != null && patImage.Length > 0)
{
stream = new System.IO.MemoryStream(patImage, true);
this.ucPictureEditPic.Clear();
this.ucPictureEditPic.Image = new System.Drawing.Bitmap(stream);
}
if (driverLicImage != null && driverLicImage.Length > 0)
{
stream = new System.IO.MemoryStream(driverLicImage, true);
this.ucPictureEditDL.Clear();
this.ucPictureEditDL.Image = new System.Drawing.Bitmap(stream); //Error occurs here.
}
}
Upvotes: 1
Views: 3425
Reputation: 6738
Using the Reference Source we can see that the bitmap class is using GDI+ native methods for constructing the image. From the reference source we can also see the list of exceptions that the constructor could possibly throw. Out of all the exceptions that can be thrown there are 8 places where an ArgumentException could be coming from.
We can eliminate #6-8 immediately since you're not trying to render a font. We can also eliminate #1 since the stream object is being created immediately above the call to the bitmap constructor. Numbers 2, 4 and 5 are a little more complex to evaluate but I've eliminated them as possibilities since memory streams are valid for constructing bitmaps. (I use it often as a go to method for rendering web based images.)
This leaves us with the unknown image format. There are two ways to check that the byte array is valid.
Load a copy of the image from a file and compare the bytes to those from the DataSet
.
if (driverLicImage != null && driverLicImage.Length > 0)
{
byte[] knownGoodImage = System.IO.File.ReadAllBytes("Path to good file on disk");
if (!driverLicImage.SequenceEqual(knownGoodImage))
{
// now you know that the bytes in the database don't match
}
stream = new System.IO.MemoryStream(driverLicImage, true);
this.ucPictureEditDL.Clear();
this.ucPictureEditDL.Image = new System.Drawing.Bitmap(stream); //Error occurs here.
}
Catch the constructor exception and save the file to disk so you can try to open it with a image editor. (Like MS Paint)
if (driverLicImage != null && driverLicImage.Length > 0)
{
try
{
stream = new System.IO.MemoryStream(driverLicImage, true);
this.ucPictureEditDL.Clear();
this.ucPictureEditDL.Image = new System.Drawing.Bitmap(stream); //Error occurs here.
}
catch (ArgumentException ex)
{
System.Diagnostics.Debug.Print(ex.Message);
System.IO.File.WriteAllBytes("Filename", driverLicImage);
}
}
Of course you will want to choose an appropriate file name.
Upvotes: 1