贴段上传图片代码,截取缩略图

本文介绍了一种图片上传和处理的方法,通过将大尺寸图片切割并生成缩略图,有效提高了网页加载速度。同时,文章提供了详细的代码实现,展示了如何使用C#进行图片的上传、压缩和存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[PS:原创,如转载,请注明转载出处,谢谢]

这段代码杂用?有啥用?能干什么?

最近在做一个图片网,你说能干嘛用呢??呵呵。。。

一张高清图,最少都是一MB以上的,看了这个之后,你还在用原来的,只保存一张图片???

那你显示列表页的时候,不卡死了才怪了,一个页面显示60张图片,一张图片来个不多,1MB,整个列表下来是60MB。。。老大,强悍。。。牛B哇。。。嘿嘿

搞啥?不搞啥,切出来后,在搞,显示缩略图,弄成几十K的大小,显示一张比例为800*600的图,用到详细页面去,在搞个原图,用来点击查看原图用,这样就不用当心了吧?速度自然就上去了

View Code
using System.IO;
using System.Drawing;

/// <summary>
/// 图片上传
/// </summary>
/// <param name="uname">用户名,用于生成图片名称</param>
/// <param name="file">HttpPostedFile</param>
/// <returns>图片路径</returns>
public string[] ImgUpLoad(string uname, HttpPostedFile file)
{
string[] returnImg = new string[3];
int intThumbWidth = 160;
int intThumbHeight = 160;

string sFilename = "";
HttpPostedFile myFile = file;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
return null;
//获取upImage选择文件的扩展名
string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
//判断是否为图片格式
if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
return null;

byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);
sFilename = System.IO.Path.GetFileName(myFile.FileName);

//生成目录名称
string dirName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/ShowPhoto/" + dirName + "/")))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/ShowPhoto/" + dirName + "/"));
}

//----生成未处理原图存放目录名称
string dirNameSrc = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Photo/" + dirNameSrc + "/")))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Photo/" + dirNameSrc + "/"));
}

DateTime dtime = DateTime.Now;
//生成图片文件名称
string fileName = dtime.Year.ToString()
+ dtime.Month.ToString()
+ dtime.Day.ToString()
+ dtime.Hour.ToString()
+ dtime.Minute.ToString()
+ dtime.Second.ToString()
+ dtime.Millisecond.ToString() + extendName;
fileName = uname + fileName;

//处理完显示图片
string savePath = "~/PhotoUploadFile/ShowPhoto/" + dirName + "/";
string dirPath = dirName + "/";

//----未处理原图路径与目录
string savePathSrc = "~/PhotoUploadFile/Photo/" + dirNameSrc + "/";
string dirPathSrc = dirNameSrc + "/";

//----保存未处理原图
System.IO.FileStream newFileSrc
= new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(savePathSrc) + fileName,
System.IO.FileMode.Create, System.IO.FileAccess.Write);
newFileSrc.Write(myData, 0, myData.Length);
newFileSrc.Close();
returnImg[0] = savePathSrc + fileName;
//以上为上传原图

try
{
//原图加载
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(savePathSrc + fileName)))
{
//原图宽度和高度
int width = sourceImage.Width;
int height = sourceImage.Height;
int smallWidth;
int smallHeight;

//获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
//smallWidth = width/(height/intThumbHeight);
smallWidth = width * intThumbHeight / height;
smallHeight = intThumbHeight;
}
else
{
smallWidth = intThumbWidth;
//smallHeight = height/(width/intThumbWidth);
smallHeight = height * intThumbWidth / width;
}

//生成目录名称
dirName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString();
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Thumb/" + dirName + "/")))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("~/PhotoUploadFile/Thumb/" + dirName + "/"));
}

dtime = DateTime.Now;

savePath = "~/PhotoUploadFile/Thumb/" + dirName + "/";
string dirPath2 = dirName + "/";


//缩略图保存的绝对路径
string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(savePath) + fileName;

//新建一个图板,以最小等比例压缩大小绘制原图
using (System.Drawing.Image bitmap3 = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//绘制中间图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap3))
{

//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
g.DrawImage(
sourceImage,
new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.GraphicsUnit.Pixel
);
}
//新建一个图板,以缩略图大小绘制中间图
using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
{
//绘制缩略图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
int lwidth = (smallWidth - intThumbWidth) / 2;
int bheight = (smallHeight - intThumbHeight) / 2;
g.DrawImage(bitmap3, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);

g.Dispose();
bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
returnImg[1] = savePath + fileName;
}
}
}

//生成显示图片 ShowPhoto
intThumbWidth = 800;
intThumbHeight = 600;
string equalSavePath = "~/PhotoUploadFile/ShowPhoto/" + dirName + "/";
string showSrcPath = equalSavePath;
equalSavePath = System.Web.HttpContext.Current.Server.MapPath(equalSavePath) + fileName;
if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
{
//smallWidth = width/(height/intThumbHeight);
smallWidth = width * intThumbHeight / height;
smallHeight = intThumbHeight;
}
else
{
smallWidth = intThumbWidth;
//smallHeight = height/(width/intThumbWidth);
smallHeight = height * intThumbWidth / width;
}
if (width <= 800 && height <= 600)
{
smallWidth = width;
smallHeight = height;
}
//if (height < 800 && width < 600)
//{
// smallWidth = height;
// smallHeight = width;
//}
//if (width <= 800) {
// smallWidth = width;
//}
//if (height <= 600) {
// smallHeight = height;
//}

//新建一个图板,以最小等比例压缩大小绘制原图
using (System.Drawing.Image bitmap3 = new System.Drawing.Bitmap(smallWidth, smallHeight))
{
//绘制中间图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap3))
{

//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
g.DrawImage(
sourceImage,
new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.GraphicsUnit.Pixel
);
}
//新建一个图板,以缩略图大小绘制中间图
using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
{
//绘制缩略图
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
{
//高清,平滑
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Black);
int lwidth = (smallWidth - intThumbWidth) / 2;
int bheight = (smallHeight - intThumbHeight) / 2;
g.DrawImage(bitmap3, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);

g.Dispose();
bitmap1.Save(equalSavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
returnImg[2] = showSrcPath + fileName;
}
}
}
}
}
catch (Exception ex)
{
//出错则删除
//System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(savePath));
throw ex;
}

return returnImg;
}

 

转载于:https://www.cnblogs.com/zhoudemo/archive/2012/01/12/2320288.html

内容概要:本文详细介绍了900W或1Kw,20V-90V 10A双管正激可调电源充电机的研发过程和技术细节。首先阐述了项目背景,强调了充电机在电动汽车和可再生能源领域的重要地位。接着深入探讨了硬件设计方面,包括PCB设计、磁性器件的选择及其对高功率因数的影响。随后介绍了软件实现,特别是程序代码中关键的保护功能如过流保护的具体实现方法。此外,文中还提到了充电机所具备的各种保护机制,如短路保护、欠压保护、电池反接保护、过流保护和过温度保护,确保设备的安全性和可靠性。通讯功能方面,支持RS232隔离通讯,采用自定义协议实现远程监控和控制。最后讨论了散热设计的重要性,以及为满足量产需求所做的准备工作,包括提供详细的PCB图、程序代码、BOM清单、磁性器件和散热片规格书等源文件。 适合人群:从事电力电子产品研发的技术人员,尤其是关注电动汽车充电解决方案的专业人士。 使用场景及目标:适用于需要高效、可靠充电解决方案的企业和个人开发者,旨在帮助他们快速理解和应用双管正激充电机的设计理念和技术要点,从而加速产品开发进程。 其他说明:本文不仅涵盖了理论知识,还包括具体的工程实践案例,对于想要深入了解充电机内部构造和工作原理的人来说是非常有价值的参考资料。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值