retrieve images from database (c# windows)
how to retrieve image from sql server db and save it to my picture box
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
manoj kumarPosted Dec 30, 2009, 4:25 AM
private int SaveToDB(string imgname, byte[] imgbin, string imgcontenttype)
{
SqlCommand command=new SqlCommand ("insert into myimages(imgname,imgfile,imgtype) values (@img_name,@img_file,@img_type)",connection);
SqlParameter param0=new SqlParameter ("@img_name",SqlDbType.VarChar ,30);
param0.Value =imgname;
command.Parameters .Add (param0);
SqlParameter param1=new SqlParameter ("@img_file",SqlDbType.Image);
param1.Value =imgbin;
command.Parameters .Add (param1);
SqlParameter param2=new SqlParameter ("@img_type",SqlDbType.VarChar ,30);
param2.Value =imgcontenttype;
command.Parameters .Add (param2);
connection.Open();
int num=command.ExecuteNonQuery ();
Response.Write("Image has stored");
connection.Close ();
return num;
}
protected void Button2_Click(object sender, System.EventArgs e)
{
connection.Open();
SqlCommand command1 = new SqlCommand("select imgfile,imgname from myimages where imgname=@param", connection);
SqlParameter myparam =command1.Parameters .Add ("@param",SqlDbType.VarChar ,30);
myparam.Value=txtimgname.Text ;
byte[] img=(byte[])command1.ExecuteScalar();
MemoryStream str=new MemoryStream();
str.Write (img,0,img.Length );
Bitmap bit=new Bitmap (str);
Response.ContentType ="image/jpeg";//or you can select your imagetype from database or directly write it here
bit.Save (Response.OutputStream ,ImageFormat.Jpeg);
connection.Close ();
}
if you dnt satis fied this code..u can chk my attch fils article..
Santhosh NPosted Dec 29, 2009, 6:26 AM
Me.SqlConnection1.Open()
Me.SqlDataAdapter1.Fill(Me.DataSet11.Pic)
With Me.ListBox1
.DataSource = Me.DataSet11.Pic
.DisplayMember = "Name"
End With
Me.SqlConnection1.Close()
Now, assign the image to picturebox
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
Dim arrayImage() As Byte =
CType(Me.DataSet11.Tables(0).Rows(Me.ListBox1.SelectedIndex) _
("Picture"), Byte())
Dim ms As New MemoryStream(arrayImage)
With Me.PictureBox1
.Image = Image.FromStream(ms)
.SizeMode = PictureBoxSizeMode.CenterImage
End With
End Sub
Lalit MPosted Dec 29, 2009, 6:23 AM