Unable to Display Image from database at runtime

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.

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
 
G

Guest

Hi Den,

just wanted to know why you store the images as such in the database.

I believe, it is recommended to save images in file system and store the
filepaths in database rather than storing the entire image in database.

Augustin

den 2005 said:
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.

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
 
W

Winista

This article should be of help..

http://www.netomatix.com/development/GridViewDisplayBlob.aspx

den 2005 said:
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.

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
 
G

Guest

Augustin,
That's the requirement what I am doing right now.

Winista,
Thanks for the link, but I am seen this approach, I am looking for
approach that converts stored sql binary/image datatype data back to an image
(jpeg) file.

dennis


--
MCP Year 2005, Philippines


Winista said:
This article should be of help..

http://www.netomatix.com/development/GridViewDisplayBlob.aspx

den 2005 said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top