コードで「パラメーターが無効です」という例外が発生するのはなぜですか。
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
の長さbyteArrayIn
は 169014 です。255 を超える値がないにもかかわらず、この例外が発生します。
コードで「パラメーターが無効です」という例外が発生するのはなぜですか。
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
の長さbyteArrayIn
は 169014 です。255 を超える値がないにもかかわらず、この例外が発生します。
私は同じ問題を抱えていて、明らかに解決されましたが、これと他のいくつかの gdi+ 例外は非常に誤解を招くものですが、実際の問題は、Bitmap コンストラクターに送信されるパラメーターが無効であることがわかりました。私はこのコードを持っています:
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)
ファイル ストリームは、Map Server からダウンロードしたファイルから作成されました。私のアプリは画像を取得するためにリクエストを間違って送信し、サーバーは拡張子が jpg の何かを返していましたが、実際にはエラーが発生したことを知らせる html でした。だから私はそのイメージを取り、それでビットマップを構築しようとしていました. 修正は、有効な jpeg 画像の画像を制御/検証することでした。
それが役に立てば幸い!
によってスローされる「パラメーターが無効です」という例外Image.FromStream()
は、ストリームが「有効な」または「認識された」形式ではないことを示します。特に、ファイルからさまざまなバイト オフセットを取得している場合は、メモリ ストリームを監視してください。
// 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 は機能します。validateImageData を有効にするには、useEmbeddedColorManagement を false にする必要があることに注意してください。
メモリ ストリームをファイルにダンプして内容を調べると、最も簡単にデバッグできます。
どの行が例外をスローしていますか? 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);
このエラーは、バイナリ データがバッファに挿入されたことが原因で発生します。この問題を解決するには、コードに 1 つのステートメントを挿入する必要があります。
このステートメントは次のとおりです。
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;
与えられたすべての解決策は機能しません..取得部分だけに集中しないでください。画像の挿入でルーク。私は同じ間違いをしました。ハードディスクから画像を取得し、データベースに保存しました。問題は挿入コマンドにあります。私の障害コードでルーク..:
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();
}
上記のコードは正常に挿入されたことを示しています...しかし、実際には間違ったデータ型の形式で画像を保存しています..一方、データ型は「画像」である必要があります..コードを改善しました..
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();
}
取得時に PARAMETER NOT VALID エラーが発生しないことを 100% 保証します....解決済み!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!
これが発生するほとんどの場合、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'))