G
Guest
Hi everyone,
Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file, then
retrieve this image from database and display it in a Image web control
dynamically(at runtime).
The process after being displayed in the web control, user click insert/add
button, it converts the image(jpeg) file to bytes[] and store it the database
with Image or VarBinary Datatype. While in retrieval, I get the specific
record, store the Image or VarBinary Data in local byte[] variable, and then
convert this to a image (jpeg) file and used the filepath to display file.
The problem still unable to display image and create image file.
Anyone?
Thanks in Advanced...
den2005
Here is code working on..Trying to insert record with a column with Image
or VarBinary datatype in sql database from a existing jpeg image file, then
retrieve this image from database and display it in a Image web control
dynamically(at runtime).
The process after being displayed in the web control, user click insert/add
button, it converts the image(jpeg) file to bytes[] and store it the database
with Image or VarBinary Datatype. While in retrieval, I get the specific
record, store the Image or VarBinary Data in local byte[] variable, and then
convert this to a image (jpeg) file and used the filepath to display file.
The problem still unable to display image and create image file.
Code:
//Get Image Record Stored and display them
private void GetImageRecord()
{
if (CheckFields(2))
{
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;
//byte storedByte = null;
//MemoryStream ms = new MemoryStream(storedByte);
//StreamWriter sw = new StreamWriter(filePath);
while (dr.Read())
{
lblImgNo.Text = dr["ImageNo"].ToString();
txtImgDesc.Text = dr["Description"].ToString();
txtImgUrl.Text = dr["ImageUrl"].ToString();
storedByte = (byte[])dr["ImageData"];
//long lRes = dr.GetBytes(5,0,storedByte,0,100);
}
MemoryStream ms = new MemoryStream(storedByte, 0,
storedByte.Length);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
[b]<<--- Error Occurred Here[/b]
System.Drawing.Image otherImg = img.GetThumbnailImage(100, 100,
null, new IntPtr());
//otherImg.Save(ms, img.RawFormat);
otherImg.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
//byte[] byteImg = new byte[ms.Length];
//ms.Position = 0;
//ms.Read(byteImg, 0, byteImg.Length);
//System.Drawing.Image img = System.Drawing.Image.FromStream(new
MemoryStream(storedByte, 0, storedByte.Length));
//img.Save(filePath);
//fs.Write(storedByte, 0, storedByte.Length);
//fs.Flush();
//fs.Close();
if (File.Exists(filePath))
PicImg2.ImageUrl = filePath;
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
private void InsertProcess()
{
if (CheckFields(1))
{
FileStream fs = new
FileStream(txtImgFile.Text.Trim(),FileMode.OpenOrCreate,FileAccess.Read);
byte[] imgByte = new byte[fs.Length];
fs.Read(imgByte,0,(int)fs.Length);
fs.Close();
//Testing storing image as binary in database
string sqlText = "Insert PictureImages
Values(@ImgName,@Desc,@Url,@Img)";
SqlCommand cmd = new SqlCommand(sqlText,conn);
cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
"ImageName").Value = txtImgName.Text.Trim();
cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
"Description").Value = txtImgDesc.Text.Trim();
cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
"ImageUrl").Value = txtImgUrl.Text.Trim();
cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
"ImageData").Value = imgByte;
if (conn.State == ConnectionState.Closed)
conn.Open();
int iRes = cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
conn.Close();
if (iRes > 0)
lblMessage.Text = "Successfully Added New Record.";
else
lblMessage.Text = "Failed to add new record.";
}
}
private void InsertProcess2()
{
if (CheckFields(0))
{
HttpPostedFile imgFile = txtFile1.PostedFile;
int imgLen = imgFile.ContentLength;
string imgType = imgFile.ContentType;
byte[] imgByte = new byte[imgLen];
imgFile.InputStream.Read(imgByte, 0, imgLen);
//Testing storing image as binary in database
string sqlText = "Insert PictureImages
Values(@ImgName,@Desc,@Url,@Img)";
SqlCommand cmd = new SqlCommand(sqlText, conn);
cmd.Parameters.Add("@ImgName", SqlDbType.VarChar, 20,
"ImageName").Value = txtImgName.Text.Trim();
cmd.Parameters.Add("@Desc", SqlDbType.VarChar, 100,
"Description").Value = txtImgDesc.Text.Trim();
cmd.Parameters.Add("@Url", SqlDbType.VarChar, 50,
"ImageUrl").Value = txtImgUrl.Text.Trim();
cmd.Parameters.Add("@Img", SqlDbType.Image, 16,
"ImageData").Value = imgByte;
if (conn.State == ConnectionState.Closed)
conn.Open();
int iRes = cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
conn.Close();
if (iRes > 0)
lblMessage.Text = "Successfully Added New Record.";
else
lblMessage.Text = "Failed to add new record.";
}
}
Anyone?
Thanks in Advanced...
den2005