簡體   English   中英

無法通過在WebBrowser站點中打開來獲取更新的條形碼圖像

[英]Can't get updated barcode image via opened in WebBrowser site

我的問題是:我嘗試通過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.執行單擊按鈕1

預期結果:條形碼輸入到文本字段並根據輸入的條形碼更新圖像實際結果:條形碼輸入到文本字段,但圖像未更新。 我無法理解為什么我的圖像不會更新。 我做錯了嗎?

注意:Id“ValueToEncode”屬於文本字段Id“Label13”屬於文本標簽,帶有文本“要編碼的數據:”我使用過的網站: http ://www.abarcode.net/online.aspx?barcode = EAN13

如果您不必依賴於使用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。 我需要添加的唯一特殊內容是referer標題,以防止水印出現。

您的結果將如下所示:

條碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM