陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

C#访问网络图片,先从本地获取,如果没有再从网络获取,保存到本地

2018年4月8日 650点热度 0人点赞 0条评论
using ScreenShowManage.Entity;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ScreenShowManage
{
    public static class EmployeePhotoUtil
    {
        /// <summary>
        /// 获取照片
        /// </summary>
        /// <param name="photoUrl">照片地址</param>
        /// <param name="name">照片名称</param>
        /// <returns></returns>
        public static Image GetEmployeePhoto(string photoUrl, string name)
        {
            Image image = null;
            if (string.IsNullOrEmpty(photoUrl))
            {
                return null;
            }
            try
            {
                image = GetEmpPhotoPic(name);
                if (image != null)
                    return image;

                WebRequest webreq = WebRequest.Create(photoUrl);
                // 设置web访问照片超时时间
                webreq.Timeout = 200;
                WebResponse webres = webreq.GetResponse();
                Stream stream = webres.GetResponseStream();
                image = Image.FromStream(stream);
                if (image != null)
                    image.Save(GetEmpPhotoCachePath() + "/" + name);
                stream.Close();
            }
            catch (WebException ex)
            {
                image = null;
            }
            //if (image == null)
            //{
            //    string path = GetEmpPhotoCachePath() + "unKnown.jpg";
            //    Image myImage = Image.FromFile(path);
            //    return myImage;
            //}
            //else
            return image;
        }

        /// <summary>
        /// 获取本地照片
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private static Image GetEmpPhotoPic(string name)
        {
            try
            {
                string path = GetEmpPhotoCachePath() + name;
                Image myImage = Image.FromFile(path);
                return myImage;
            }
            catch (Exception ex)
            {
                return null;

            }
        }

        /// <summary>
        /// 获取本地缓存职工照片目录
        /// </summary>
        /// <returns></returns>
        private static string GetEmpPhotoCachePath()
        {
            try
            {
                string strName = "EmpPhoto";
                if (!Directory.Exists(strName))
                {
                    Directory.CreateDirectory(strName);
                }
                return strName + "/";
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}

 

标签: C#
最后更新:2021年4月2日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
nginx C# python js linux centos winform DevExpress
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
C# 写个枚举扩展方法,字符串根据枚举描述值Description转成指定枚举对象 C# 一个发送邮件的帮助类 DreamSkin自定义美化控件-带智能下拉提示的TextBox控件 python 常用命令安装 C#通过特性的方式去校验指定数据是否为空 剪切pdf分割程图片和pdf

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS