Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
So 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. What I want to do now is read that data from the sql database onto the web browser control. Now the screen scraped page is saved to database as an image and is display in the database as binary data. I have some of the code for this, but i'm not sure how to read it to the browser control.

Here is my code

The code that I have done so far for the reading is displayed in the method called btnRead.
wbHtmlPage is the name of my web broswer control.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Data.SqlClient;
 
namespace FilmAndEntertainmentSystem
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private string GetWebsiteHtml(string url)
        {
            WebRequest request = WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            string result = reader.ReadToEnd();
            stream.Dispose();
            reader.Dispose();
            return result;
        }
 
        private void btnGetHTML_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
        }
 
        private void btnScreenSave_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
 
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(html);
 
            // 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();
 
            using (SqlCommand com = new SqlCommand("INSERT INTO Website (WebsiteImage) VALUES (@Image)", cs))
            {
                com.Parameters.AddWithValue("@Image", bytes);
                cs.Open();
 
                com.ExecuteNonQuery();
 
                cs.Close();
            }
        }
 
        private void btnRead_Click(object sender, EventArgs e)
        {
            string html = this.GetWebsiteHtml(this.txtUrl.Text);
            this.wbHtmlpage.DocumentText = html;
 
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(html);
 
            // 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();
 
            da.SelectCommand = new SqlCommand("Select WebsiteImage From Website Where WebsiteID = 3", cs);
 
            da.Fill(ds, "Website");
 
            cs.Open();
 
            response.ContentType = "Image";
            response.BinaryWrite(bytes);
 
            
            cs.Close();
 
 
 
        }
    }
}
Posted
Updated 19-May-11 5:00am
v2

1 solution

If it's saved as an image, you could do this to get it on the screen (using the appropriate ImageFormat enum of course):

C#
// Set the content type and return the image
Response.ContentType = "image/GIF";
bytes.Save(Response.OutputStream, ImageFormat.Gif);
 
Share this answer
 
Comments
programmer1234 19-May-11 11:46am    
It doesnt recognize ImageFormat or Response, would I need to put the web response from the private string into the btnread method?

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