using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
Global Declaration
SqlConnection conn = new SqlConnection("server=192.168...;User ID=....;password=...;database=....");
protected void Save_Click(object sender, EventArgs e)
{
// create a byte[] for the image file that is uploaded
int imagelen = FileUpload1.PostedFile.ContentLength;
byte[] picbyte = new byte[imagelen];
FileUpload1.PostedFile.InputStream.Read(picbyte, 0, imagelen);
// Insert the image and image id into the database
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into TableImage"+"(ImageId,ImageName) values (@imageid,@pic)", conn);
cmd.Parameters.AddWithValue("@pic", picbyte);
cmd.Parameters.AddWithValue("@imageid", TextBox1.Text);
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
protected void Open_Click(object sender, EventArgs e)
{
//For Displaying Image
MemoryStream mstream = new MemoryStream();
conn.Open();
SqlCommand cmd = new SqlCommand("select ImageName from TableImage where ImageId="+TextBox1.Text, conn);
byte[] image = (byte[])cmd.ExecuteScalar();
mstream.Write(image, 0, image.Length);
Bitmap bitmap = new Bitmap(mstream);
Response.ContentType = "image/gif";
bitmap.Save(Response.OutputStream, ImageFormat.Gif);
conn.Close();
mstream.Close();
}
Subscribe to:
Post Comments (Atom)

1 comment:
nice article..
Post a Comment