2016-05-31 12 views
1

カメラで画像を取り込んでデータベースに挿入しています(ブロブとして)。それらをデータベースに挿入するには、イメージをバイト配列で渡す必要があります。ここで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(...)」次のようにエラーがスローされます。ここでは

enter image description here

は、メソッドの保存の定義です:

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をへ、またファイルシステムにもない画像を保存することはできません。

+0

を試してみてください、あなたは* imageIn *はSystem.Drawing.Imaging.ImageFormat.Jpeg形式を持っていることを確認していますか? –

+0

まずビットマップに変換してからMemoryStreamに保存してください –

+0

はいこれはJPEG形式であると確信しています。私はそれを "if(ImageFormat.Jpeg.Equals(clonedImage.RawFormat))"でチェックします。親愛なるJericCruzさん、jpeg形式で挿入するよう頼まれていますので、変更する贅沢はありません。 – blablabla

答えて

-2

this-

public static void PerisitImage(string path, IDbConnection connection) 
{ 
    using (var command = connection.CreateCommand()) 
    { 
     Image img = Image.FromFile (path); 
     MemoryStream tmpStream = new MemoryStream(); 
     img.Save (tmpStream, ImageFormat.Png); // change to other format 
     tmpStream.Seek (0, SeekOrigin.Begin); 
     byte[] imgBytes = new byte[MAX_IMG_SIZE]; 
     tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE); 

     command.CommandText = "INSERT INTO images(payload) VALUES (:payload)"; 
     IDataParameter par = command.CreateParameter(); 
     par.ParameterName = "payload"; 
     par.DbType = DbType.Binary; 
     par.Value = imgBytes; 
     command.Parameters.Add(par); 
     command.ExecuteNonQuery(); 
    } 
} 

ソース - How to save image in database using C#

+0

親愛なるSouvik Ghoshさん、私は "img.Save"部。 興味深いことに、この便利なコードをありがとうございます。私はそれが他の人のために役立つだろうと確信しています。私はそれを感謝します。 – blablabla

+3

質問がわからない場合は、Googleのタイトルをコピーして最初のStack Overflowヒットをコピーして貼り付けてください。それは盗作です。質問を理解しようとするか、次の質問に答えてください。 – CodeCaster

+0

@blablabla私の混乱のために申し訳ありません。私は正しい答えのためにこのポストに従うだろう.. –

関連する問題