2017-01-07 5 views
0

SQL ServerデータベースからC#WindowsアプリケーションのPictureBoxに画像を取得しています。イメージは私が知らない形です。データベースから画像ボックスに画像を取得するときにパラメータが無効です

私はこのコードを試しましたが、「パラメータが無効です」という例外がスローされました。また、これに関連した多くの回答を行っていますが、うまくいきませんでした。

マイコード:

SqlConnection sql_con = new SqlConnection(); 
    sql_con.ConnectionString = @"Server=abc-14;Database=Abcstudent;User Id=sa; Password=abc;"; 

    if (sql_con.State == ConnectionState.Closed) 
     sql_con.Open(); 

    try 
    { 
     string query = "Select grno, PICStudent from GENREG where grno = 1";       
     SqlCommand sql_cmd = new SqlCommand(query, sql_con); 

     sql_cmd.ExecuteNonQuery(); 

     SqlDataAdapter sql_adp = new SqlDataAdapter(sql_cmd);    
     sql_adp.Fill(sql_ds);  
    } 
    catch(Exception ex) 
    { 
    } 

    // code for retrieving image to picture box 
    try 
    { 
     DataRow myRow; 
     byte[] MyData = new byte[0]; 
     myRow = sql_ds.Tables[0].Rows[0]; 

     MyData = (byte[])myRow["PICStudent"]; 
     MemoryStream stream = new MemoryStream(MyData); 
     pictureBox1.Image = Image.FromStream(stream); // getting error here 
    } 
    catch() 
    { 
    } 

データベース・イメージImage.FromStreamを使用しての enter image description here

+0

コードのどの行でエラーを得ていますか? –

+0

私はコメントのエラーの行で – Mamta

答えて

1
SqlConnection con = new SqlConnection("Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=SSPI;"); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("select picbox from picture1 where id=1", con); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["picbox"]); 
      pictureBox1.Image = new Bitmap(ms); 
     } 
+0

を更新しましたありがとうございます@vinothしかし同じエラー:パラメータ無効 – Mamta

+0

行@ママ –

+0

pictureBox1.Image =新しいビットマップ(ms)//パラメータ無効です – Mamta

1

INSEAD、あなたはImageConverterを使用してみたのですか?

ImageConverterを使用したサンプルは次のようになります。

byte[] filedata = (byte[])myRow["PICStudent"]; 
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); 

これで問題が解決するはずです。

おかげに関して、 チェタンRanpariya

+0

にあります。@Chetanにお返事ありがとうございますが、imageConverter.ConvertFrom(fileData)in Systemのエラー "Parameter Invalid"描画。画像; – Mamta

関連する問題