现在的位置: 首页 > 综合 > 正文

在WINFORM中使用HTTP读取图象文件,并表示出来.

2013年10月02日 ⁄ 综合 ⁄ 共 1003字 ⁄ 字号 评论关闭

在WINFORM中使用HTTP读取图象文件,并表示出来.
private void GetPicture()
        {
            string sourceURL = "http://webcam.mmhk.cz/axis-cgi/jpg/image.cgi";
            byte[] buffer = new byte[100000];
            int read, total = 0;
            // create HTTP request
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceURL);
            // get response
            WebResponse resp = req.GetResponse();
            // get response stream
            Stream stream = resp.GetResponseStream();
            // read data from stream
            while ((read = stream.Read(buffer, total, 1000)) != 0)
            {
                total += read;
            }
            // get bitmap
            Bitmap bmp = (Bitmap)Bitmap.FromStream(
                          new MemoryStream(buffer, 0, total));

            Image myimage = Image.FromHbitmap(bmp.GetHbitmap());
            PictureBox picTest = new PictureBox();
            picTest.Image = myimage;
            picTest.Show();
            picTest.Refresh();
            this.Controls.Add(picTest);
            bmp.Dispose();
            bmp = null;
        }

抱歉!评论已关闭.