簡體   English   中英

從本地計算機反復在Picturebox控件中加載圖像

[英]Loading image in picturebox control repeatedly from local machine

我有一些C#代碼,可從網頁獲取圖像,然后將其下載到本地計算機。 這是在后台1 /秒完成的。 如果我讓此程序運行正常,則我的圖片會正確更新。 這些圖片基本上是來自照相機的提要。 我想將這些圖片放入圖片框或其他控件中,以便可以像顯示圖像一樣顯示圖像。 但是,當我嘗試執行此操作時,出現錯誤,指出正在使用該圖像,因此無法將其加載到我的圖片框中。 有一個更好的方法嗎?

謝謝,

 byte[] lnBuffer;
                    byte[] lnFile;

                    HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create(uri);

                    lxRequest.Credentials = credentials;
                    using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse())
                    {

                        using (BinaryReader lxBR = new BinaryReader(lxResponse.GetResponseStream()))
                        {
                            using (MemoryStream lxMS = new MemoryStream())
                            {
                                lnBuffer = lxBR.ReadBytes(1024);
                                while (lnBuffer.Length > 0)
                                {
                                    lxMS.Write(lnBuffer, 0, lnBuffer.Length);
                                    lnBuffer = lxBR.ReadBytes(1024);
                                }
                                lnFile = new byte[(int)lxMS.Length];
                                lxMS.Position = 0;
                                lxMS.Read(lnFile, 0, lnFile.Length);
                                lxMS.Close();
                                lxBR.Close();
                            }
                        }
                        lxResponse.Close();
                    }






                            using (System.IO.FileStream lxFS = new FileStream("images/camppic1.jpg", FileMode.Create))
                            {
                                lxFS.Write(lnFile, 0, lnFile.Length);
                                lxFS.Close();




                            }

這就是我用來創建文件的方式。 然后在此代碼之后的相同方法中執行以下操作:

圖片= Image.FromFile(“ C:\\ camppic1.jpg”);

              pictureBox23.Image = image;

如果需要文件,則加載文件內容並復制到MemoryStream並使用Image.FromStream 如果不需要該文件,則可以跳過該文件並直接從下載中使用MemoryStream ...(這樣便很快捷,因為不需要光盤訪問。)

暫無
暫無

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

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