This article will help you advance your skills with the WebBrowser Control in Visual C# 2008.
You will be able to get all the media from a website, all the links from a website, and the source of a website. You will be able to play around with all the information you get. Sych As:
Media:
- Save the selected images from a Website. All the images will be organized in a CheckListBox control.
- Save all the Images from a website.
- Copy an Image.
- Check all the items from a checklistbox control
- Uncheck all the items in a CheckListBox Control.
- View the numer of media elements found on a page.
Links:
- Copy a Link.
- View a Link in the Default Internet Browser on the machine.
- View the number of Links found on a page.
Source:
- Save the Source
- Save the Webpage
- Print Preview the webpage
- Page setup for the Webpage
- Print the webpage
- View the webpage on the Internet Browser.
- Copy the Source.
It would be better if you have a slight experience with the My property of Visual Basic 2008, because we will be using the Visual Basic namespace in our project
private void button1_Click(object sender, EventArgs e)
{
//We need a try Catch statement, because the url can sometimes be
//invalid.
try
{
//Navigate
browser.Navigate(textBox1.Text);
}
catch (Exception)
{
//Exception
MessageBox.Show("Invalid URL, please try again.");
}
}
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Enable all our action buttons.
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
chklstImages.Items.Clear();
lstLinks.Items.Clear();
//Get all the images from the webpage
foreach (HtmlElement img in browser.Document.Images)
{
chklstImages.Items.Add(img.GetAttribute("src"));
}
//Get all the links from the webpage
foreach (HtmlElement lnk in browser.Document.Links)
{
lstLinks.Items.Add(lnk.GetAttribute("href"));
}
//Get the source of the document
txtSource.Text = browser.DocumentText;
//Convert the URL of the document to a String in a variable
String str = Convert.ToString(browser.Url);
//Update the text of the textbox
textBox1.Text = str;
//Change the text of the fORM
this.Text = "Data Grabbed for " + browser.DocumentTitle;
//Convert the count of items to string in our images list and update the lblcount text
String mediacount = Convert.ToString(chklstImages.Items.Count);
lblCount.Text = mediacount;
//Convert the count of items to string in our linklist and update the lbllinkcount text
string lnkcount = Convert.ToString(lstLinks.Items.Count);
lblLinkCount.Text = lnkcount + " Links Found";
}
private void chklstImages_SelectedIndexChanged(object sender, EventArgs e)
{
//Update the lblLinkCount.Text property to the correct count
lblCount.Text = chklstImages.Items.Count.ToString();
//If the images selected index in not 0 then
if (chklstImages.SelectedIndex != 0)
{
//Change the Preview Picturebox's images location
imgMediaWeb.ImageLocation = chklstImages.SelectedItem.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
//Try catch to download and save all images
SaveFileDialog save = new SaveFileDialog();
int q = 1;
if (save.ShowDialog() == DialogResult.OK)
{
try
{
foreach (String strlocation in chklstImages.CheckedItems)
{
//Update the integer so we don't encounter an error in the name of the file
q = q + 1;
Microsoft.VisualBasic.Devices.Network k = new Microsoft.VisualBasic.Devices.Network();
//Save images as JPEG File format
k.DownloadFile(strlocation, save.FileName + q + ".jpeg");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//try catch to select all items in chklstimages
try
{
for (int x = 0; x < this.chklstImages.Items.Count; x++)
{
//Select all items
this.chklstImages.SetItemChecked(x, true);
}
}
catch (Exception)
{
return;
}
//Try catch to download and save all images
SaveFileDialog save = new SaveFileDialog();
int q = 1;
if (save.ShowDialog() == DialogResult.OK)
{
try
{
foreach (String strlocation in chklstImages.CheckedItems)
{
//Change the last integer of the filename
q = q + 1;
Microsoft.VisualBasic.Devices.Network k = new Microsoft.VisualBasic.Devices.Network();
//Save image as a JPEG
k.DownloadFile(strlocation, save.FileName + q + ".jpeg");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button4_Click(object sender, EventArgs e)
{
if (lstLinks.Items.Count != 0)
{
//Convert lstlinks.selecteditem to string in a string variable
String strLink = Convert.ToString(lstLinks.SelectedItem);
//Go to the selected link
System.Diagnostics.Process.Start(strLink);
}
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
//Create a Strign Variable that has the Text we want to Copy
String strText = txtSource.SelectedText;
//To make this easy, decalre a variable using Microsoft.VisualBasic.Devices.Computer
Microsoft.VisualBasic.Devices.Computer varDataCopy = new Microsoft.VisualBasic.Devices.Computer();
//Copy Text to the clipboard
varDataCopy.Clipboard.SetText(strText, TextDataFormat.Html);
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
//Select the Control, so the user notices the select all
txtSource.Focus();
//Select All text
txtSource.SelectAll();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
if (save.ShowDialog() == DialogResult.OK)
{
//Save the text of source
txtSource.SaveFile(save.FileName, RichTextBoxStreamType.RichText);
}
}
private void viewWebpageToolStripMenuItem_Click(object sender, EventArgs e)
{
//Show Print Preview Dialog for webpage
browser.ShowPrintPreviewDialog();
}
private void viewWebpageInBrowserToolStripMenuItem_Click(object sender, EventArgs e)
{
//Convert url to string and store it in a string variable
String str = Convert.ToString(browser.Url);
//Go to the current webpage
System.Diagnostics.Process.Start(str);
}
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
//Show page setup dialog for our current document
browser.ShowPageSetupDialog();
}
private void printPageToolStripMenuItem_Click(object sender, EventArgs e)
{
//Show print dialog to print our document
browser.ShowPrintDialog();
}
private void reloadToolStripMenuItem_Click(object sender, EventArgs e)
{
//Update the text of our txtSource
txtSource.Text = browser.DocumentText;
}
private void button5_Click(object sender, EventArgs e)
{
//try catch to select all items in chklstimages
try
{
for (int x = 0; x < this.chklstImages.Items.Count; x++)
{
//Select all items
this.chklstImages.SetItemChecked(x, true);
}
}
catch (Exception)
{
return;
}
}
private void button6_Click(object sender, EventArgs e)
{
//try catch to select all items in chklstimages
try
{
for (int x = 0; x < this.chklstImages.Items.Count; x++)
{
//deselect all items
this.chklstImages.SetItemChecked(x, false);
}
}
catch (Exception)
{
return;
}
}
private void browser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
//Convert the Progress (long) to int and create variables
Int32 maxint = Convert.ToInt32(e.MaximumProgress);
Int32 crntint = Convert.ToInt32(e.CurrentProgress);
//Set the progress bar value and maximum value
progressBar1.Maximum = maxint;
progressBar1.Value = crntint;
}
private void button7_Click(object sender, EventArgs e)
{
//Convert lstLinks.SelectedItem to String and create it as a variable strdata
string strdata = Convert.ToString(lstLinks.SelectedItem);
//To make this easy, decalre a variable using Microsoft.VisualBasic.Devices.Computer
Microsoft.VisualBasic.Devices.Computer varDataCopy = new Microsoft.VisualBasic.Devices.Computer();
//Set the text of clipborad or copy text
varDataCopy.Clipboard.SetText(strdata, TextDataFormat.Text);
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
//Show SaveAsDialog for the Webpage
browser.ShowSaveAsDialog();
}
private void button8_Click(object sender, EventArgs e)
{
//To make this easy, decalre a variable using Microsoft.VisualBasic.Devices.Computer
Microsoft.VisualBasic.Devices.Computer varDataCopy = new Microsoft.VisualBasic.Devices.Computer();
//Set the image
varDataCopy.Clipboard.SetImage(imgMediaWeb.Image);
}
}
This was a really fun project, because when I first started on programming, my first project was a webbrowser. I love to share my thoughts with others.
No History.
| You must Sign In to use this message board. | ||||||
|
||||||
|
||||||
|
||||||