Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my windows form I've made a textbox which the user uses to enter the website address and then click a button to screen scrap that website into a browser control. They can also save that screen scraped website to an sql database. Now the screen scraped page is saved to database as an image and is display in the database as binary data. I have added a picture box an text box to my windows form, which the user uses to enter in the ID number of the image in the database, then click a button, which reads the image from the database into a picture box. I have put the code in for this, but when I run the program and click the button for the reading, it comes up with an error saying "Out of memory"?

Here is my code

private void btnRead_Click(object sender, EventArgs e)
{
    // set up data connection
    SqlConnection cs = new SqlConnection("Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True");
    // Set up adapter manager
    SqlDataAdapter da = new SqlDataAdapter();
    // Data set
    DataSet ds = new DataSet();
    SqlCommand com = new SqlCommand("Select WebsiteImage From Website Where WebsiteID = @ID", cs);

        com.Parameters.Add("@ID", SqlDbType.Int,1);
        com.Parameters["@ID"].Value = this.txtId.Text;

        cs.Open();
       byte[] barrImg = (byte[])com.ExecuteScalar();
      string strfn = Convert.ToString(DateTime.Now.ToFileTime());
       FileStream fs = new FileStream (strfn,  FileMode.CreateNew);
       fs.Write(barrImg,0, barrImg.Length);
       fs.Flush();
       fs.Close();
       picRead.Image = Image.FromFile(strfn);


        cs.Close();
}
Posted
Updated 19-May-11 11:31am
v4
Comments
yesotaso 19-May-11 17:25pm    
Irrelevant maybe but what does com.ExecuteNonQuery(); do? Second from bottom line.
programmer1234 19-May-11 17:31pm    
Whoops, dont actually need that there as it doesnt do anything.

1 solution

Found the following block here... Retrieve Image[^]

public Stream DisplayImage(int theID)
{ 
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
string sql = "SELECT image FROM Table1 WHERE id = @ID";
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", theID);
connection.Open();
object theImg = cmd.ExecuteScalar();
try
{ 
return new MemoryStream((byte[])theImg);
}
catch
{ 
return null;
}
finally
{ 
connection.Close();
}
} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900