My problem is following: I try to open barcode online generator via WebBrowser and get barcode image. Here is my code:
/// <summary>
/// Main form of barcode server
/// </summary>
public partial class MainForm : Form
{
#region Constants
private const String BarCodeSite = "http://www.abarcode.net/online.aspx?barcode=EAN13";//"http://barcode.tec-it.com/en#";
#endregion
/// <summary>
/// Main form constructor
/// </summary>
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// This event occured after form load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
webBrowser.Navigate(new Uri(BarCodeSite));
}
/// <summary>
/// Occurs when form is closing.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
barcodeServer.Abort();
}
catch (Exception ex)
{
// do nothing
}
}
private void button1_Click(object sender, EventArgs e)
{
var code = textBox1.Text;
var editText = webBrowser.Document.GetElementById("ValueToEncode");
editText.SetAttribute("Value", code.Trim(new char[] { '\0' }));
webBrowser.Document.GetElementById("Label13").InvokeMember("click");
}
}
What actions i perform: 1. Run my project 2. In options choose image zoom 250% 3. Paste into textBox1 controle code 8414034620202 4. Perform click on button1
Expected result: Barcode entered to the text field and image updated according to the entered barcode Actual result: Barcode entered to the text field, but image wasn't updated. I can't understand why my image doesn't update. Am I do something wrong?
Notes: Id "ValueToEncode" belongs to text field Id "Label13" belongs to text label with text "Data to encode:" Site I've used: http://www.abarcode.net/online.aspx?barcode=EAN13
Assuming it is allowed to scrape the content of that website you're better off if you don't have to rely on using the WebBrowser control as it has many quirks.
In your particular case two simple HttpWebRequest
calls is all you need to get the generated barcode image:
CookieContainer cookies = new CookieContainer();
private void button1_Click(object sender, EventArgs e)
{
// do a get to have the session cookie
var wr = (HttpWebRequest) WebRequest.Create("http://www.abarcode.net/online.aspx");
wr.CookieContainer = cookies;
wr.Method = "GET";
var stream = wr.GetResponse().GetResponseStream();
using(var sr = new StreamReader(stream))
{
// debug
Debug.WriteLine(sr.ReadToEnd());
}
// get the image
var imageReq = (HttpWebRequest)WebRequest.Create(
String.Format(
"http://www.abarcode.net/barcode.aspx?value={0}&type=EAN13",
textBox1.Text));
// this makes if you get their watermark in the barcode or not
imageReq.Referer = "http://www.abarcode.net/online.aspx?barcode=EAN13";
imageReq.CookieContainer = cookies;
imageReq.Method = "GET";
// get the image stream
using(stream = imageReq.GetResponse().GetResponseStream())
{
// create the bitmap.
pictureBox1.Image = Bitmap.FromStream(stream);
}
}
I have a CookieContainer
to capture and re-use the cookies across the WebRequest calls. The only special thing that I needed to add was the referer header to prevent the watermark to show up.
Your result will look like this: