Mr_WTX
2021-06-18 16:38
采纳率: 50%
浏览 33

wpf webclient下载http图片资源问题怎么解决

在wpf使用webclient的downloadfile,下载完之后打开之后显示图片格式不对,没有显示图片就很奇怪。望能解决。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • Mr_WTX 2021-07-13 17:14
    已采纳

    已解决

    已采纳该答案
    打赏 评论
  • Null_Reference 2021-06-18 18:08

    代码贴出来看看

    打赏 评论
  • Mr_WTX 2021-06-22 10:10

     

     try
                {
                    if (!string.IsNullOrEmpty(url))
                    {
                        address = YBHelper.YBUtileTool.getAppPath(@"\LayOutData\") + url.Substring(url.LastIndexOf("/") + 1);
                        if (!File.Exists(address))
                        {  
                            webClient.DownloadFile(url, address);//就是用这个下载资源的
                        }
                        PlaySouce.Add(address);
                        if (File.Exists(address)) return true;
    
                    }
                }
                catch(Exception ex)
                {
                    log.WriteLog(ex.Message,Log.LogType.NetWork,ex.StackTrace);
                }
                return false;


              

    打赏 评论
  • Mr_WTX 2021-07-23 14:49
    
    /// <summary>
            /// 下载资源文件
            /// </summary>
            /// <param name="url">网络地址</param>
            /// <param name="address">下载到本地的地址</param>
            /// <returns></returns>
            public static bool DownLoadFile(string url,string address)
            {
    
                try
                {
                    if (!string.IsNullOrEmpty(url))
                    {
                        if (!File.Exists(address))
                        {
                            //webClient.Headers.Add("Accept-Encoding", "gzip,deflate");
                            byte[] byteArr = webClient.DownloadData(url);
                            string contentEncoding = webClient.ResponseHeaders["Content-Encoding"];
                            if (contentEncoding == "gzip")
                            {
                                Stream stream = new MemoryStream(byteArr);
                                using (Stream zipStream = new GZipStream(stream, CompressionMode.Decompress))
                                {
                                    using (Stream fileStream = new FileStream(address, FileMode.Create))
                                    {
                                        byte[] read = new byte[1024];
                                        int readlen = zipStream.Read(read, 0, read.Length);
                                        while (readlen > 0)
                                        {
                                            fileStream.Write(read, 0, read.Length);
                                            readlen = zipStream.Read(read, 0, read.Length);
                                        }
                                        stream.Close();
                                        fileStream.Close();
                                        zipStream.Close();
                                    }
    
                                }
                            }
                            else
                            {
                                Stream stream = new MemoryStream(byteArr);
                                    using (Stream fileStream = new FileStream(address,FileMode.Create))
                                    {
                                        byte[] read = new byte[1024];
                                        int readlen = stream.Read(read, 0, read.Length);
                                        while (readlen > 0)
                                        {
                                            fileStream.Write(read, 0, read.Length);
                                            readlen = stream.Read(read, 0, read.Length);
                                        }
                                        stream.Close();
                                        fileStream.Close();
                                    }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.WriteLog(ex.Message, Log.LogType.NetWork, ex.StackTrace);
                }
                return false;
            }
    就是判断是否压缩文件了,然后解压,可以改改。
    
    打赏 评论

相关推荐 更多相似问题