public void DownloaImage(string fileName, string url, string btnName) { try { WebRequest webreq = WebRequest.Create(url); WebResponse webres = webreq.GetResponse(); Stream stream = webres.GetResponseStream(); Stream fileStream = new FileStream(fileName, FileMode.Create); byte[] bArray = new byte[1024]; int size; do { size = stream.Read(bArray, 0, (int)bArray.Length); fileStream.Write(bArray, 0, size); } while (size > 0); fileStream.Close(); stream.Close(); if (btnName.Contains("btn_Setting_")) { Thread thread = new Thread(() => PicDeal.setWallpaperApi(fileName)); thread.Start(); } else { MethodInvoker methInvo = new MethodInvoker(showMessageForm); BeginInvoke(methInvo); } } catch (Exception ex) { throw new Exception("下载图片失败,原因为:" + ex.Message); } }
/// <summary> /// 图片点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Dp_MouseClick(object sender, DuiMouseEventArgs e) { DuiPictureBox dp = sender as DuiPictureBox; string ImageSavePath = ""; if (string.IsNullOrEmpty(pes.CachePath)) { ImageSavePath = AppDomain.CurrentDomain.BaseDirectory + @"\ImageWallpaper"; } else { ImageSavePath = pes.CachePath + @"\ImageWallpaper"; } //设置墙纸 try { Bitmap bmpWallpaper; WebRequest webreq = WebRequest.Create(dp.Tag.ToString()); WebResponse webres = webreq.GetResponse(); Stream stream = webres.GetResponseStream(); if (!Directory.Exists(ImageSavePath)) { Directory.CreateDirectory(ImageSavePath); } bmpWallpaper = (Bitmap)Image.FromStream(stream); ImageSavePath = ImageSavePath + "\\" + new Uri(dp.Tag.ToString()).Segments[new Uri(dp.Tag.ToString()).Segments.Length - 1]; bmpWallpaper.Save(ImageSavePath, ImageFormat.Jpeg); stream.Close(); PicDeal.setWallpaperApi(ImageSavePath); } catch (Exception ex) { throw new Exception("下载图片失败,原因为:" + ex.Message); } }