You try this code:-
private void button3_Click(object sender, EventArgs e)
{
if (textImg.Text != "")
{
string sql = "Select Image from SqlToImage where ImgName='" + textImg.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 startIndex = 0;
savedImageName = textImg.Text;
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);
startIndex = 0;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
while (retval == bufferSize)
{
bw.Write(outbyte);
bw.Flush();
startIndex += bufferSize;
retval = myReader.GetBytes(0, startIndex, outbyte, 0, bufferSize);
}
bw.Write(outbyte, 0, (int)retval - 1);
bw.Flush();
bw.Close();
file.Close();
}
connection.Close();
curImage = Image.FromFile(savedImageName);
pictureBox1.Image = curImage;
pictureBox1.Invalidate();
connection.Close();
}
}