C# 本地图片缓存器ImageFileCache (www.csframework.com)
C# 本地图片缓存器ImageFileCache (www.csframework.com)
C# Code: /// <summary> /// C# 本地图片缓存器(www.csframework.com) /// </summary> public class ImageFileCache { //考虑异步应用,加锁 private object _Locker = new object(); //图片根目录 private string _baseFolder = ""; //图片数据库 private DataTable _Data; /// <summary> /// 构造器 /// </summary> public ImageFileCache(string baseFolder = "") { //默认为应用程序目录下 if (String.IsNullOrWhiteSpace(baseFolder)) baseFolder = Path.Combine(Application.StartupPath, "ImageCache"); _baseFolder = baseFolder; if (!Directory.Exists(baseFolder)) Directory.CreateDirectory(baseFolder); //加载数据库 _Data = ReadDatabase(); } #region Instance static ImageFileCache _Instance = null; public static ImageFileCache Instance { get { if (_Instance == null) _Instance = new ImageFileCache(); return _Instance; } } #endregion; internal bool Exists(ImageBuckleName buckle, string url) { url = ToMD5(url); lock (_Locker) { return _Data.Select($"buckle='{buckle.ToString()}' AND url='{url}'").Length > 0; } } /// <summary> /// 数据库文件路径 /// </summary> private string dbFile { get { return Path.Combine(_baseFolder, "cache_files.db"); } } /// <summary> /// 加载数据库 /// </summary> /// <returns></returns> private DataTable ReadDatabase() { lock (_Locker) { string path = this.dbFile; if (File.Exists(path)) { _Data = GetFileTable(); _Data.ReadXml(path); return _Data; } else { _Data = GetFileTable(); return _Data; } } } /// <summary> /// 保存数据库 /// </summary> public void Save() { lock (_Locker) { _Data.WriteXml(this.dbFile, true); } } /// <summary> /// 获取图片 /// </summary> /// <param name="buckle"></param> /// <param name="url"></param> /// <returns></returns> public Bitmap GetImage(ImageBuckleName buckle, string url) { url = ToMD5(url); lock (_Locker) { var rows = _Data.Select($"buckle='{buckle.ToString()}' AND url='{url}'"); if (rows.Length > 0) { string file = Path.Combine(_baseFolder, rows[0]["name"].ToString()); //C#加载图片文件,不占用文件句柄方式。 byte[] bs = File.ReadAllBytes(file); MemoryStream ms = new MemoryStream(bs); return new Bitmap(Image.FromStream(ms)); } else { return null; } } } /// <summary> /// 添加图片 /// </summary> /// <param name="url"></param> /// <param name="img"></param> /// <param name="buckle"></param> public void AddImage(string url, Image img, ImageBuckleName buckle) { lock (_Locker) { string md5 = ToMD5(url); string dir = Path.Combine(_baseFolder, buckle.ToString()); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); string ext = Path.GetExtension(url); string fileName = md5 + ext; fileName = buckle.ToString() + @"\" + fileName; string file = Path.Combine(_baseFolder, fileName); img.Save(file); if (File.Exists(file)) { DataRow row = _Data.Rows.Add(); row["buckle"] = buckle.ToString(); row["name"] = fileName; row["url"] = md5;//将url转化为md5 row["time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); _Data.AcceptChanges(); } } } private DataTable GetFileTable() { DataTable dt = new DataTable("file"); dt.Columns.Add("isid", typeof(int)); dt.Columns.Add("buckle", typeof(String)); dt.Columns.Add("url", typeof(String)); dt.Columns.Add("name", typeof(String)); dt.Columns.Add("time", typeof(String)); return dt; } /// <summary> /// MD5加密,不可逆 /// </summary> /// <param name="clearText">明文字符串</param> /// <returns></returns> private string ToMD5(string clearText) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] encryptedBytes = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(clearText)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < encryptedBytes.Length; i++) { sb.AppendFormat("{0:x2}", encryptedBytes[i]); } return sb.ToString().ToLower(); } } //来源:C/S框架网 | www.csframework.com | QQ:23404761 C# Code: /// <summary> /// 图片存储子目录(可自定义) /// </summary> public enum ImageBuckleName { large_images, small_images } //来源:C/S框架网 | www.csframework.com | QQ:23404761 缓存图片文件目录结构,以及图片数据库文件: 扫一扫加微信
参考文档:
C#图片加水印(Generate Watermark Picture) 开发应用-观察者模式之C#实现缓存数据更新 C# 实现条码图片自动生成功能 C#数据库本地缓存技术(Database local cache) C#图片转换类支持按比例缩放图片 C# Winform 制作可任意拖动的不规则背景图片窗体 C#.Net 上传图片,限制图片大小,检查类型完整版 Asp.Net(C#)发送带有附件及显示图片的邮件 C# RichTextbox中插入图片 C#切割图片自动填充工具栏按钮图片 C# 窗体上显示透明图片,PNG图片,已审核/未审核图标 C# 窗体上显示透明图片,制作已审核/未审核图标 DevExpress表格打印预览及自定义页眉页脚并绘制图片(C# Winform) C# 百度地图坐标获取详细地址 C# PictureBox设置背景图片及加载图片源码
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |