How can I display an Image in an ASP.NET page that I saved in a database? Which prperty of Image control can I use?
In the winform I've tested the below code and succesfully retrieve picture in a pictureBox.
But I guess this might not be the proper way.
protected void Button2_Click1(object sender, EventArgs e)
{
if (FileUpload1.FileName != "")
{
string sql = "SELECT MyImage FROM MyImages WHERE ImageName ='TextBox1.Text'";
SqlConnection connection = new SqlConnection();
connection.ConnectionString = connectionString;
connection.Open();
FileStream file;
BinaryWriter bw;
int bufferSize = 100;
byte[] outbyte = new byte[bufferSize];
long retval;
long starIndex = 0;
savedImageName = FileUpload1.FileName;
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader myReader = command.ExecuteReader(CommandBehavior.SequentialAccess);
while (myReader.Read())
{
file = new FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(file);
starIndex = 0;
retval = myReader.GetBytes(0, starIndex, outbyte, 0, bufferSize);
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
starIndex += bufferSize;
retval = myReader.GetBytes(0, starIndex, outbyte, 0, bufferSize);
}
bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
file.Close();
}
//Can I write something here to retrieve picture?
connection.Close();
}
else
Response.Write("Upload the image first");
}
}

0 answers