【WPF】wpf image控件加載網絡圖片不顯示問題,


1、加載網絡圖片到內存system.drawing.image對象中
2、內存中的image 轉Bitmap 再轉適合system.windows.controls.image 的BitmapImage類型

為什么:網絡延遲,得不到實時的圖片信息

 

            string userHeadPic = "http://123.56.178.100/headpic/7.jpg";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(userHeadPic);
            WebResponse response = request.GetResponse();
            System.Drawing.Image img = System.Drawing.Image.FromStream(response.GetResponseStream());
            Bitmap bitMap = new Bitmap(img);
            BitmapImage bitUserLogo = BitmapToBitmapImage(bitMap);
            SchoolPubData.UserInfo.imgHead.Source = bitUserLogo;
            SchoolPubData.MainForm.ToSchoolSwitchPage();
        public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height);
            int i, j;
            for (i = 0; i < bitmap.Width; i++)
                for (j = 0; j < bitmap.Height; j++)
                {
                    System.Drawing.Color pixelColor = bitmap.GetPixel(i, j);
                    System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B);
                    bitmapSource.SetPixel(i, j, newColor);
                }
            MemoryStream ms = new MemoryStream();
            bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.BeginInit();
            bitmapImage.StreamSource = new MemoryStream(ms.ToArray());
            bitmapImage.EndInit();
            return bitmapImage;
        }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM