2016-07-14 9 views
0

IPカメラ専用のビューアを作成しようとしています。これによりIPカメラから画像を読み込み、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(); 
    } 
} 

:私は(MS)500にtimer1にタイマーを設定した場合

コード、関係なく大したことが、私は、画像をpicturebox1に開かれ、ロードされている問題に来ていないが、それ文句を言わないリフレッシュコードPictureBox1は、urlからロードされたイメージですが、リフレッシュされません。 どうしたらいいですか?

+2

'pictureBox1.Update()がダウンロードを再度トリガーしますか?私はそれを強く疑っています。あなたは、画像をダウンロードし、タイマーの目盛りによってトリガされる 'pictureBox1.Image'に画像を設定する方法を抽出するべきだと考えています... –

+0

私は最後の2行'timer1_Tick'の' Form1_Load'から(必要なローカル変数を抽出して) – Philippe

+0

'pictureBox1'を更新するだけでは不十分です。カメラからイメージを再ダウンロードする必要があります。これは基本的に、 'form1_Load()'コードを 'time1_Tick()'で再実行しなければならないことを意味します。 –

答えて

1

だから、あなたはhereに記載されているか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(); 
} 
関連する問題