Hi everybody,
I am creating an image(jpeg) file from stored bytes[] in sql database, Varbinary datatype, (I used Image datatype also same result), I am using this jpeg image file to set to a Image web control, what seems the problem and how to fix this problem? Do I have proper datatype in sql to store converted jpeg file data? Please provide codes. Thanks.
[code]
string sqlText = "Select * From PictureImages Where ImageName = @ImgName";
SqlCommand cmd = new SqlCommand(sqlText, conn);
cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20, "ImageName").Value = txtImgName.Text.Trim();
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
string filePath = Request.PhysicalApplicationPath + txtImgName.Text + ".jpg";
FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
byte[] storedByte = null;
while (dr.Read())
{
lblImgNo.Text = dr["ImageNo"].ToString();
txtImgDesc.Text = dr["Description"].ToString();
txtImgUrl.Text = dr["ImageUrl"].ToString();
storedByte = (byte[])dr["ImageData"];
}
MemoryStream ms = new MemoryStream(storedByte);
Bitmap img = (Bitmap)System.Drawing.Image.FromStream(ms); <<-- Error Occurred Here
img.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Close();
img.Dispose();
[/code]
Nobody seems to know how to solve this???
den2005