Пример #1
0
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private string DownloadImage(string url)
        {
            string localFile = Path.GetTempPath() + Guid.NewGuid().ToString("N");

            // 下载
            bool isSuccessed = StorageUtils.DownloadImage(url, localFile);

            if (isSuccessed)
            {
                try
                {
                    string md5 = Hash.GetMD5HashFromFile(localFile);

                    if (File.Exists(localFile))
                    {
                        string destFile = $"{StorageManager.GetHeader()}/{md5}";

                        try
                        {
                            File.Delete(destFile);
                        }
                        catch { }

                        // 移动到指定位置
                        File.Move(localFile, destFile);

                        return(md5);
                    }
                    else
                    {
                        return(null);
                    }
                }
                catch (Exception e)
                {
                    Utils.Debugging.Console.PrintLine("DownloadImage()发生异常: {0}", e);
                    LogManager.Error("StorageHeader", e);
                    return(null);
                }
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// 获取用户头像缩略图
        /// </summary>
        /// <param name="header"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public BitmapImage GetHeaderThumbnail(string header, int width, int height)
        {
            if (header == null)
            {
                return(null);
            }

            try
            {
                Bitmap bitmap    = new Bitmap(header);
                Image  thumbnail = bitmap.GetThumbnailImage(width, height, null, IntPtr.Zero);

                return(StorageUtils.BitmapToBitmapImage(new Bitmap(thumbnail)));
            }
            catch (ArgumentException)
            {
                try
                {
                    SimpleDecoder simpleDecoder = new SimpleDecoder(header);
                    Bitmap        bitmap        = simpleDecoder.WebPtoBitmap();
                    Image         thumbnail     = bitmap.GetThumbnailImage(width, height, null, IntPtr.Zero);

                    return(StorageUtils.BitmapToBitmapImage(new Bitmap(thumbnail)));
                }
                catch (Exception ex)
                {
                    Utils.Debugging.Console.PrintLine("GetHeaderThumbnail()发生异常: {0}", ex);
                    LogManager.Error("StorageHeader.GetHeaderThumbnail()", ex);

                    return(null);
                }
            }
            catch (Exception e)
            {
                Utils.Debugging.Console.PrintLine("GetHeaderThumbnail()发生异常: {0}", e);
                LogManager.Error("StorageHeader.GetHeaderThumbnail()", e);

                return(null);
            }
        }