WebBrowser cant open Instagram c# - c#

when i put Instagram's address in my WebBrowser object, on loading of program, it just shows a blank white page, and nothing else, where is problem?!
first i think it should be cause WebBrowser using IE, but IE(ver.11) loads Instagram successfully .
UPDATE:
this is the code which i use:
this.webB.Location = new System.Drawing.Point(93, 17);
this.webB.MinimumSize = new System.Drawing.Size(20, 20);
this.webB.Name = "webB";
this.webB.Size = new System.Drawing.Size(642, 324);
this.webB.TabIndex = 1;
this.webB.Url = new System.Uri("https://www.instagram.com/", System.UriKind.Absolute);
this.webB.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webB_DocumentCompleted);
this.webB.ScriptErrorsSuppressed = true;

Try this one :). This code is useful for loading urls(ex:Instagram) and download the source code of the appropriate page.
protected void LoadUrl(object sender, EventArgs e)
{
string url = txtUrl.Text.Trim();
Thread thread = new Thread(delegate()
{
using (WebBrowser browser = new WebBrowser())
{
browser.ScrollBarsEnabled = false;
browser.AllowNavigation = true;
browser.Navigate(url);
browser.Width = 1024;
browser.Height = 768;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = sender as WebBrowser;
string sourcecode=browser.documentText;
}

Related

Trying to stall main-thread while background worker displays a user-interactive form in C#

I'm making(trying) to make a dialogue box similar to the openFileDialogue where the program flow of the Main Thread is stalled until the OPF.ShowDialogue() function stalls while running a separate thread.
I have a OpenFileDialogue_Gui class which uses a semaphore and a background worker.
The background worker is first launched to generate a separate form with the necessary user-interface
Then the semaphore stalls the main thread until the user clicks one of the buttons on the background thread's form.
Then it releases the semaphore and the result is sent back to the calling function.
Sounds nice in theory, but there's a problem. The background-worker is not displaying its form and it's not working.
Here's the code :
public class openfiledialogue_Gui
{
Semaphore semStall = new Semaphore(0, 1);
BackgroundWorker bck = new BackgroundWorker();
public openfiledialogue_Gui()
{
bck.WorkerSupportsCancellation = true;
bck.DoWork += Bck_DoWork;
bck.RunWorkerCompleted += Bck_RunWorkerCompleted;
}
public enum enuResult { Ok, Cancel, _num };
public enuResult ShowDialogue()
{
bck.RunWorkerAsync();
semStall.WaitOne();
return eResult;
}
enuResult eResult = enuResult.Cancel;
void EventOk_click(object sender, EventArgs e)
{
eResult = enuResult.Ok;
strFilename = "ok clicked";
semStall.Release();
}
void EventCancel_click(object sender, EventArgs e)
{
eResult = enuResult.Cancel;
strFilename = "cancel clicked";
semStall.Release();
}
private void Bck_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){semStall.Release();}
private void Bck_DoWork(object sender, DoWorkEventArgs e)
{
Form frmShow = new Form();
Button btnOk = new Button();
btnOk.Text = "ok";
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnOk.AutoSize
= btnCancel.AutoSize
= true;
frmShow.Controls.Add(btnOk);
frmShow.Controls.Add(btnCancel);
frmShow.ControlBox = false;
btnOk.Location = new Point(100, 25);
btnCancel.Location = new Point(100, 75);
btnCancel.Click += EventCancel_click;
btnOk.Click += EventOk_click;
frmShow.Show();
}
public string strFilename = "this is a test";
public string Filename
{
get { return strFilename; }
}
}
Is there any way to make this work?
Sounds like you want to make a 'Modal Form'.
var form = new MyModalForm();
form.ShowDialog();
This will 'stall' the UI until you cancel (or otherwise close) the form.

Web browser doesn't retrieve all data

I am new to WebBrowser library.
When fetching data from https://www.binance.com/indexSpa.html website, I used the following code. But it does not return all the data. What should I do?
WebBrowser wb = new WebBrowser();
wb.DocumentCompleted += Wb_DocumentCompleted;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(#"https://www.binance.com/indexSpa.html#");
private void Wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var wb = (WebBrowser)sender;
Console.WriteLine(wb.DocumentText);
}
Thank you for help.

Adding progresindicator to image downloading

I've got application page where is view from street camera. Photo form camera is refreshing about every 30 sec.
I created a button which is used to refresh photo.
I want to add progress indicator which will be show every time when photo is downloading.
I don't know how and where exactly I have to add code.
I tried many examples but fail.
Because I don't really understand how to turn it on and off.
public void downloading()
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(ImageOpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://przeprawa.swi.pl/cgi-bin/kam.cgi?6&1399042906515&" + Guid.NewGuid()));
}
private void ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.Result);
image1.Source = bmp;
}
}
public void Refresh(object sender, EventArgs e)
{
downloading();
}
Make this change to your code:
public void downloading()
{
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(ImageOpenReadCompleted);
webClient.OpenReadAsync(new Uri("http://przeprawa.swi.pl/cgi-bin/kam.cgi?6&1399042906515&" + Guid.NewGuid()));
var _progressIndicator = new ProgressIndicator
{
IsIndeterminate = true,
IsVisible = true,
Text = "Downloading...",
};
SystemTray.SetProgressIndicator(this, _progressIndicator);
}
private void ImageOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.Result);
image1.Source = bmp;
var _progressIndicator = new ProgressIndicator
{
IsVisible = false,
};
SystemTray.SetProgressIndicator(this, _progressIndicator);
}
}
public void Refresh(object sender, EventArgs e)
{
downloading();
}

printPreview landscape mode inC#

How can I set the print Preview and print code to landscape orientation ?
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(700, 600);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.Visible = false;
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage_1);
This did the trick
this.printDocument1.DefaultPageSettings.Landscape = true;
Won't this
var doc = new PrintDocument();
doc.DefaultPageSettings.Landscape = true;
do the trick?
It will probably take care of the print preview issue as well.
This works for me
private void button_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printDocument1.DefaultPageSettings.Landscape = true;
printPreviewDialog1.ShowDialog();
}

Need help getting listbox item text from secondary form to main form

I have been looking all over the Internet for this and have found similar, but none of it will work for me. Everything I have found assumes the listbox is on the main form and not the secondary form, or the code is for an older version of C# or Visual Studio (I am using VS2008).
I am creating a web browser that has a button on the main form (called frmMyBrowser) to open a dialog (frmBookmarks) with a listbox (lstBookmark) with bookmarked URLs. I need to be able to double click on an item (the bookmarked URL) and have the text pasted into the address bar (cmbAddress) of the main form.
Any help would be GREATLY appreciated.
I figured out that if I create the window at runtime, I can get it to work. In case anyone is interested, the code I used is below.
public void frmMyBrowser_ShowFavorites(object sender, EventArgs e)
{
frmFavorites.ShowIcon = false;
frmFavorites.ShowInTaskbar = false;
frmFavorites.MinimizeBox = false;
frmFavorites.MaximizeBox = false;
frmFavorites.ControlBox = false;
frmFavorites.Text = "Bookmarks";
frmFavorites.Width = 500;
frmFavorites.Height = 350;
frmFavorites.Controls.Add(lstFavorites);
frmFavorites.Controls.Add(btnRemoveFavorite);
frmFavorites.Controls.Add(btnAddFavorite);
frmFavorites.Controls.Add(btnCloseFavorites);
frmFavorites.Controls.Add(txtCurrentUrl);
lstFavorites.Width = 484;
lstFavorites.Height = 245;
btnRemoveFavorite.Location = new Point(8, 280);
btnAddFavorite.Location = new Point(8, 255);
btnCloseFavorites.Location = new Point(400, 255);
txtCurrentUrl.Location = new Point(110, 255);
txtCurrentUrl.Size = new Size(255, 20);
btnAddFavorite.Text = "Add";
btnRemoveFavorite.Text = "Remove";
btnCloseFavorites.Text = "Close";
txtCurrentUrl.Text = wbBrowser.Url.ToString();
btnAddFavorite.Click += new EventHandler(btnAddFavorite_Click);
btnRemoveFavorite.Click += new EventHandler(btnRemoveFavorite_Click);
frmFavorites.Load += new EventHandler(frmFavorites_Load);
btnCloseFavorites.Click += new EventHandler(btnCloseFavorites_Click);
lstFavorites.MouseDoubleClick += new MouseEventHandler(lstFavorites_MouseDoubleClick);
frmFavorites.Show();
}
public void btnCloseFavorites_Click(object sender, EventArgs e)
{
if (lstFavorites.Items.Count > 0)
{
using (StreamWriter writer = new System.IO.StreamWriter(#Application.StartupPath + "\\favorites.txt"))
{
for (int i = 0; i < lstFavorites.Items.Count; i++)
{
writer.WriteLine(lstFavorites.Items[i].ToString());
}
writer.Close();
}
}
frmFavorites.Hide();
}
public void btnAddFavorite_Click(object sender, EventArgs e)
{
string strFavoriteAddress = wbBrowser.Url.ToString();
if (!lstFavorites.Items.Contains(strFavoriteAddress))
{
lstFavorites.Items.Add(strFavoriteAddress);
MessageBox.Show("Favorite Added", "Message");
}
else if (lstFavorites.Items.Contains(strFavoriteAddress))
{
MessageBox.Show("This site already exists in your Favorites list!", "Error");
}
else
{
}
}
public void btnRemoveFavorite_Click(object sender, EventArgs e)
{
try
{
lstFavorites.Items.RemoveAt(lstFavorites.SelectedIndices[0]);
}
catch
{
MessageBox.Show("You need to select an item", "Error");
}
}
public void frmFavorites_Load(object sender, EventArgs e)
{
try
{
using (StreamReader reader = new System.IO.StreamReader(#Application.StartupPath + "\\favorites.txt"))
{
while (!reader.EndOfStream)
{
for (int i = 0; i < 4; i++)
{
string strListItem = reader.ReadLine();
if (!String.IsNullOrEmpty(strListItem))
{
lstFavorites.Items.Add(strListItem);
}
}
}
reader.Close();
}
}
catch
{
MessageBox.Show("An error has occured", "Error");
}
}
private void lstFavorites_MouseDoubleClick(object sender, MouseEventArgs e)
{
string strSelectedAddress = lstFavorites.Text.ToString();
cmbAddress.Text = strSelectedAddress;
wbBrowser.Navigate(strSelectedAddress);
frmFavorites.Hide();
}

Categories

Resources