C# 将前端传来的图片文件分别以大图和缩略图保存
HttpPostedFile pic_upload = Request.Files["file"]; Bitmap bitmap = (Bitmap)System.Drawing.Image.FromStream(pic_upload.InputStream); Size s = new Size(); s.Height = 50; s.Width = 50; System.Drawing.Image minImage = clsPublic.GetImageThumb(bitmap, s);
//下面这部分不重要,主要内容是在上面,有缩略图和原图后,直接按照常规的方法进行保存即可
string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();
string filepath = "~/upload/" + kis_web.DBHelper.sAcctNumber + "/files/";//图片存储路径:uppad+账套编号+images
string activitecode = Guid.NewGuid().ToString().Replace("-", "");
if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹
{
Directory.CreateDirectory(Server.MapPath(filepath));
}
string virpath = filepath + activitecode + fileExtension;//这是存到服务器上的虚拟路径
string virMinpath = filepath + activitecode+"_min" + fileExtension;
转缩略图方法
public static Bitmap GetImageThumb(Bitmap mg, Size newSize) { double ratio = 0d; double myThumbWidth = 0d; double myThumbHeight = 0d; int x = 0; int y = 0; Bitmap bp; if (newSize.Width == 0) { newSize.Width = 1; } if (newSize.Height == 0) { newSize.Height = 1; } if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height / Convert.ToDouble(newSize.Height))) ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width); else ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height); myThumbHeight = Math.Ceiling(mg.Height / ratio); myThumbWidth = Math.Ceiling(mg.Width / ratio); Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height); bp = new Bitmap(newSize.Width, newSize.Height); x = (newSize.Width - thumbSize.Width) / 2; y = (newSize.Height - thumbSize.Height); System.Drawing.Graphics g = Graphics.FromImage(bp); g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height); g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel); return bp; }
技术参考:https://www.cnblogs.com/zhangchaoran/p/7693166.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构