2013-08-27 2 views
1

미리 저장 한 이미지를 SQL Server CE VarBinary 열에서 PictureBox으로로드하려고합니다.SQL Server CE의 varbinary 비트 맵을 그림 상자에로드하십시오.

열 콘텐츠는 varbinary 형식으로 저장된 비트 맵 이미지입니다.

MemoryStream ms = new MemoryStream(); 
byte[] outbyte = new byte[100]; 
Int32 ordinal = 0; 

conn.Open(); 
SqlCeDataReader reader = cmd.ExecuteReader(); 

while (reader.Read()) 
{ 
    ordinal = reader.GetOrdinal("FaceStamp");//FaceStamp: VarBinary column storing Bmp. 
    outbyte = (byte[])reader[ordinal]; 
    ms.Write(outbyte, 0, outbyte.Length); 
    ms.Seek(0, SeekOrigin.Begin); 
    pictureBox1.Image = Image.FromStream(ms); 
}   

conn.Close(); 

// Code below is the code I used to save the Bitmap image to the database 
Bitmap bmi = cam.GetBitmap(); // Capture image from webcam which I've tested working. 
ImageConverter converter = new ImageConverter(); 
byte[] byteArray = new byte[0]; 
byteArray = (byte[])converter.ConvertTo(bmi, typeof(byte[])); 
insert.Parameters.AddWithValue("@image", byteArray); 
insert.ExecuteNonQuery(); 

나는 다음 줄에 오류가 발생합니다 :

pictureBox1.Image = Image.FromStream(ms); 

을 말하는 { "매개 변수가 유효하지 않습니다."}

모든 팁은?

감사합니다. 바이트 배열로 저장된 이미지를 변환 할 때

+0

'이미지'가 올바르게'byte [] '로 데이터베이스에 저장 되었습니까? 적어도 당신은 이미지를'byte []'로 변환하여 그에 따라 변환하는 방법을 알아야합니다. –

+0

예 필드를 확인한 결과이 필드에 이진 데이터가 있음을 알 수 있습니다. –

+0

아니요, 바이너리 데이터가 이미지로 '변환'될 수 있다는 것을 의미하지는 않습니다. 'Serialization'을 사용하여 비트 맵 데이터를'byte []'로 변환했다면, 'deserialization'을 사용하여'byte []'에서 다시 얻어야합니다. –

답변

0

나는 보통 다음을 수행하십시오

byte[] outbyte = (byte[])reader[ordinal]; 
using (MemoryStream ms = new MemoryStream(outbyte)) 
{ 
    Bitmap image = new Bitmap(ms); 
    pictureBox1.Image = image; 
} 

바이트 배열로 원본 이미지를 변환하는 당신은 this SO question 좀 걸릴 수 있습니다.

+0

_Bitmap image = new Bitmap (ms) _ 줄에 같은 오류가 나타납니다. –

+0

PNG 대신 System.Drawing.Imaging.ImageFormat.Bmp를 사용하여 이미지를 저장하려면 다른 SO 질문의 MemoryStream 예제를 따르십니까? – ThoBa

관련 문제