カメラで画像を取り込んでデータベースに挿入しています(ブロブとして)。それらをデータベースに挿入するには、イメージをバイト配列で渡す必要があります。ここでC#:System.Drawing.ImageをMemoryStreamに保存しようとするとエラーがスローされます
をバイト配列に変換し、ほとんどのコードです:
public static byte[] JpegToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); //PROBLEM IS HERE!
return ms.ToArray();
}
もののを、私は合格した画像はJPEG形式であることを確信して、「imageIn.Save(...)」次のようにエラーがスローされます。ここでは
は、メソッドの保存の定義です:
public void Save(Stream stream, ImageFormat format);
//
// Summary:
// Saves this System.Drawing.Image to the specified file in the specified format.
//
// Parameters:
// filename:
// A string that contains the name of the file to which to save this System.Drawing.Image.
//
// format:
// The System.Drawing.Imaging.ImageFormat for this System.Drawing.Image.
//
// Exceptions:
// System.ArgumentNullException:
// filename or format is null.
//
// System.Runtime.InteropServices.ExternalException:
// The image was saved with the wrong image format.-or- The image was saved
// to the same file it was created from.
私が検索して関数に渡すイメージは、決してファイルシステムに格納されず/ファイルシステムから読み込まれることはありません。私はデータベースに画像を挿入した後、私はそれを処分する。そして、私が画像を取得するメソッドは、System.Drawing.Image(4要素)のリストを返すだけです。特別なことは何もありません。
どうしてこのようなことが起こる可能性はありますか?
お時間をいただきありがとうございます。
EDIT:
私は、たとえば、ピクチャボックスに画像を設定することができる午前:
pictureBox1.Image = imageIn;
picturebox.Refresh();
しかし、MemoryStreamをへ、またファイルシステムにもない画像を保存することはできません。
を試してみてください、あなたは* imageIn *はSystem.Drawing.Imaging.ImageFormat.Jpeg形式を持っていることを確認していますか? –
まずビットマップに変換してからMemoryStreamに保存してください –
はいこれはJPEG形式であると確信しています。私はそれを "if(ImageFormat.Jpeg.Equals(clonedImage.RawFormat))"でチェックします。親愛なるJericCruzさん、jpeg形式で挿入するよう頼まれていますので、変更する贅沢はありません。 – blablabla