簡體   English   中英

從IP攝像機加載圖像並將其顯示在PictureBox1中

[英]Load Image from IP camera and show it in PictureBox1

我正在嘗試僅使IP攝像機的查看器。 沒什么大不了,但是我遇到了一個問題,無論我將timer1 timer設置為500(ms),都無法打開Image並將其加載到picturebox1但刷新不會

代碼:

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        string url = "http://IPofIPCamera/now.jpg";
        WebClient webClient = new WebClient();
        CredentialCache myCache = new CredentialCache();
        myCache.Add(new Uri(url), "Basic",
            new NetworkCredential("SomeUser", "SomePass"));
        webClient.Credentials = myCache;
        MemoryStream imgStream = new MemoryStream(webClient.DownloadData(url));
        pictureBox1.Image = new System.Drawing.Bitmap(imgStream);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        pictureBox1.Update();
    }
}

使用此代碼, PictureBox1是從url加載的圖像,但不會刷新。 我做錯了什么?

所以,你有怎樣的誤解PictureBox.Update的作品,這是描述在這里

使控件重新繪制其客戶區域內的無效區域。

這意味着不會再次觸發下載。

要解決您的問題,您應該提取一種下載和設置圖像的方法,該方法將由計時器的滴答聲觸發。

public void DownloadAndUpdatePicture()
{
  string url = "http://IPofIPCamera/now.jpg";
  WebClient webClient = new WebClient();
  CredentialCache myCache = new CredentialCache();
  myCache.Add(new Uri(url), "Basic", new NetworkCredential("SomeUser", "SomePass"));
  webClient.Credentials = myCache;
  MemoryStream imgStream = new MemoryStream(webClient.DownloadData(url));
  pictureBox1.Image = new System.Drawing.Bitmap(imgStream);
}

private void Form1_Load(object sender, EventArgs e)
{
  this.DownloadAndUpdatePicture();
}

private void timer1_Tick(object sender, EventArgs e)
{
  this.DownloadAndUpdatePicture();
}

暫無
暫無

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

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