1

我的问题如下:我尝试通过 WebBrowser 打开条形码在线生成器并获取条形码图像。这是我的代码:

/// <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");
    }
}

我执行什么操作: 1. 运行我的项目 2. 在选项中选择图像缩放 250% 3. 粘贴到 textBox1 控制代码 8414034620202 4. 执行单击 button1

预期结果:文本字段输入条码,根据输入的条码更新图像 实际结果:文本字段输入条码,但图像未更新。我不明白为什么我的图像没有更新。我做错了吗?

注意:Id“ValueToEncode”属于文本字段 Id“Label13”属于带有文本“要编码的数据:”的文本标签 我使用过的网站: http ://www.abarcode.net/online.aspx?barcode=EAN13

4

1 回答 1

0

假设允许抓取该网站的内容,如果您不必依赖使用 WebBrowser 控件,您会更好,因为它有很多怪癖。

在您的特定情况下,您只需要两个简单的HttpWebRequest调用即可获取生成的条形码图像:

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);
    }
}

我必须CookieContainer在 WebRequest 调用中捕获和重用 cookie。我需要添加的唯一特殊内容是引用标题以防止水印出现。

您的结果将如下所示:

条码

于 2016-04-06T11:34:43.543 回答