“参数无效”exception加载System.Drawing.Image

为什么我在代码中得到“参数无效”exception:

MemoryStream ms = new MemoryStream(byteArrayIn); System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); 

byteArrayIn的长度是169014.我得到这个exception,尽pipe没有任何值大于255。

我有同样的问题,现在显然是解决了,尽pipe这和其他一些gdi +exception是非常误导,我发现实际上问题是发送到一个位图构造函数的参数是无效的。 我有这个代码:

 using (System.IO.FileStream fs = new System.IO.FileStream(inputImage, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)) { try { using (Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false)) { try { bitmap.Save(OutputImage + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp); GC.Collect(); } catch (Exception ex) { throw ex; } } } catch (ArgumentException aex) { throw new Exception("The file received from the Map Server is not a valid jpeg image", aex); } } 

以下行导致错误:

 Bitmap bitmap = (Bitmap)Image.FromStream(fs, true, false) 

文件stream是从从地图服务器下载的文件构build的。 我的应用程序不正确地发送请求来获取图像,服务器返回的东西与JPG扩展名,但实际上是一个HTML告诉我,一个错误发生。 所以我采取了这个形象,并试图与它build立一个位图。 解决的办法是控制/validation图像的有效JPEG图像。

希望它有帮助!

我的猜测是, byteArrayIn不包含有效的图像数据。

请提供更多信息:

  • 哪一行代码抛出exception?
  • 什么信息?
  • 你从哪里得到了byteArrayIn ,你确定它应该包含一个有效的图像?
 byte[] fileData = null; using (var binaryReader = new BinaryReader(Request.Files[0].InputStream)) { fileData = binaryReader.ReadBytes(Request.Files[0].ContentLength); } ImageConverter imageConverter = new System.Drawing.ImageConverter(); System.Drawing.Image image = imageConverter.ConvertFrom(fileData) as System.Drawing.Image; image.Save(imageFullPath, System.Drawing.Imaging.ImageFormat.Jpeg); 

哪一行是抛出exception? new MemoryStream(...) ? 或Image.FromStream(...) ? 那么byteArrayIn什么? 这是一个byte[]吗? 我只是因为评论而问:“而且其中的任何一个值都不大于255” – 当然这对于一个byte[]是自动的。

作为一个更明显的问题:二进制文件是否实际上包含一个合理格式的图像?

例如,以下(虽然不是伟大的代码)工作正常:

  byte[] data = File.ReadAllBytes(@"d:\extn.png"); // not a good idea... MemoryStream ms = new MemoryStream(data); Image img = Image.FromStream(ms); Console.WriteLine(img.Width); Console.WriteLine(img.Height); 

Image.FromStream()引发的“参数无效”exception告诉您该stream不是“有效”或“识别”格式。 观察内存stream,特别是如果你正在从一个文件中获取不同的字节偏移量。

 // 1. Create a junk memory stream, pass it to Image.FromStream and // get the "parameter is not valid": MemoryStream ms = new MemoryStream(new Byte[] {0x00, 0x01, 0x02}); System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);` // 2. Create a junk memory stream, pass it to Image.FromStream // without verification: MemoryStream ms = new MemoryStream(new Byte[] {0x00, 0x01, 0x02}); System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms, false, true); 

示例2将工作,请注意useEmbeddedColorManagement必须为false,以使validateImageData有效。

将内存stream转储到文件并检查内容可能是最容易debugging的。

这个错误是由插入到缓冲区的二进制数据引起的。 为了解决这个问题,你应该在代码中插入一条语句。

这个陈述是:

 obj_FileStream.Read(Img, 0, Convert.ToInt32(obj_FileStream.Length)); 

例:

 FileStream obj_FileStream = new FileStream(str_ImagePath, FileMode.OpenOrCreate, FileAccess.Read); Byte[] Img = new Byte[obj_FileStream.Length]; obj_FileStream.Read(Img, 0, Convert.ToInt32(obj_FileStream.Length)); dt_NewsFeedByRow.Rows[0][6] = Img; 

所有给出的解决scheme不工作..不要只专注于检索部分。 luk在插入图像。 我也犯了同样的错误。 我从硬盘上打出一张图像并将其保存到数据库中。 问题在于insert命令。 卢克在我的错误代码..:

  public bool convertImage() { try { MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); photo = new byte[ms.Length]; ms.Position = 0; ms.Read(photo, 0, photo.Length); return true; } catch { MessageBox.Show("image can not be converted"); return false; } } public void insertImage() { // SqlConnection con = new SqlConnection(); try { cs.Close(); cs.Open(); da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = " +photo+" WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs); da.UpdateCommand.ExecuteNonQuery(); cs.Close(); cs.Open(); int i = da.UpdateCommand.ExecuteNonQuery(); if (i > 0) { MessageBox.Show("Successfully Inserted..."); } } catch { MessageBox.Show("Error in Connection"); } cs.Close(); } 

上面的代码显示成功插入…但实际上它保存图像的forms错误的数据types..而数据types必须bt“图像”..所以我改进了代码..

  public bool convertImage() { try { MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, ImageFormat.Jpeg); photo = new byte[ms.Length]; ms.Position = 0; ms.Read(photo, 0, photo.Length); return true; } catch { MessageBox.Show("image can not be converted"); return false; } } public void insertImage() { // SqlConnection con = new SqlConnection(); try { cs.Close(); cs.Open(); //THIS WHERE THE CODE MUST BE CHANGED>>>>>>>>>>>>>> da.UpdateCommand = new SqlCommand("UPDATE All_students SET disco = @img WHERE Reg_no = '" + Convert.ToString(textBox1.Text)+ "'", cs); da.UpdateCommand.Parameters.Add("@img", SqlDbType.Image);//CHANGED TO IMAGE DATATYPE... da.UpdateCommand.Parameters["@img"].Value = photo; da.UpdateCommand.ExecuteNonQuery(); cs.Close(); cs.Open(); int i = da.UpdateCommand.ExecuteNonQuery(); if (i > 0) { MessageBox.Show("Successfully Inserted..."); } } catch { MessageBox.Show("Error in Connection"); } cs.Close(); } 

100%保证在检索中不会有PARAMETER NOT VALID错误….已解决!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!

只要按照这个插入值到数据库

//连接string

  con.Open(); sqlQuery = "INSERT INTO [dbo].[Client] ([Client_ID],[Client_Name],[Phone],[Address],[Image]) VALUES('" + txtClientID.Text + "','" + txtClientName.Text + "','" + txtPhoneno.Text + "','" + txtaddress.Text + "',@image)"; cmd = new SqlCommand(sqlQuery, con); cmd.Parameters.Add("@image", SqlDbType.Image); cmd.Parameters["@image"].Value = img; //img is a byte object ** /*MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms,pictureBox1.Image.RawFormat); byte[] img = ms.ToArray();*/** cmd.ExecuteNonQuery(); con.Close(); 

大多数情况下,这是SQL列中的错误数据。 这是插入图像列的正确方法:

 INSERT INTO [TableX] (ImgColumn) VALUES ( (SELECT * FROM OPENROWSET(BULK N'C:\....\Picture 010.png', SINGLE_BLOB) as tempimg)) 

大多数人这样做是不正确的:

 INSERT INTO [TableX] (ImgColumn) VALUES ('C:\....\Picture 010.png')) 

我正在使用Access数据库,我应该怎么做…这里是我的保存代码工作正常..而在检索提供参数无效

 OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\imgDB.accdb"); Bitmap Bimg = new Bitmap(curFileName); MemoryStream Mystream = new MemoryStream(); Bimg.Save(Mystream, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] ByteData = Mystream.ToArray(); con.Open(); string str = "insert into myimg values(2,'" + ByteData + "')"; OleDbCommand cmd = new OleDbCommand(str, con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Picture Saved...!"); } catch (Exception ex) { MessageBox.Show(ex.Message); }