代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
ASP.NET图片上传,自动缩放,自动居中
作者:
Dezai.CN
/ 发布于
2013/1/21
/
696
ASP.NET图片上传,自动缩放,自动居中
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Security.Cryptography; using System.Text; using System.IO; using System.Drawing; using System.Threading; public class _PhotoHandler { public _PhotoHandler() { //调用方法! //PhotoHandler.Upload_Server(FileUpload1, "../../Upload", "Upload", 400, 400, true, System.Drawing.Color.White); } #region 生成缩略图 /// <summary> /// 生成缩略图 /// </summary> /// <param name="orginalImagePat">原图片地址</param> /// <param name="thumNailPath">缩略图地址</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="model">生成缩略的模式</param> public static void MakeThumNail(string originalImagePath, string thumNailPath, int width, int height, int oldW, int oldH, System.Drawing.Color color) { System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int thumWidth = width; //缩略图的宽度 int thumHeight = height; //缩略图的高度 int originalWidth = originalImage.Width; //原始图片的宽度 int originalHeight = originalImage.Height; //原始图片的高度 //新建一个bmp图片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(oldW, oldH); //新建一个画板 System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap); //设置高质量查值法 graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并填充背景色 graphic.Clear(color); //在指定位置并且按指定大小绘制原图片的指定部分 int x = 0; int y = 0; if (oldW == thumWidth && oldH > thumHeight) { y = (oldH - thumHeight) / 2; } else if (oldH == thumHeight && oldW > thumWidth) { x = (oldW - thumWidth) / 2; } if (oldW > thumWidth && oldH > thumHeight) { x = (oldW - thumWidth) / 2; y = (oldH - thumHeight) / 2; } graphic.DrawImage(originalImage, new System.Drawing.Rectangle(x, y, thumWidth, thumHeight), new System.Drawing.Rectangle(0, 0, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel); try { bitmap.Save(thumNailPath, System.Drawing.Imaging.ImageFormat.Png); } catch (Exception ex) { throw ex; } finally { originalImage.Dispose(); bitmap.Dispose(); graphic.Dispose(); } } #endregion #region 在图片上添加文字水印 /// <summary> /// 在图片上添加文字水印 /// </summary> /// <param name="path">要添加水印的图片路径</param> /// <param name="syPath">生成的水印图片存放的位置</param> /// <param name="syWord">图片上显示文本</param> public static void AddWaterWord(string path, string syPath, string syWord) { System.Drawing.Image image = System.Drawing.Image.FromFile(path); //新建一个画板 System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(image); graphic.DrawImage(image, 0, 0, image.Width, image.Height); //设置字体 System.Drawing.Font f = new System.Drawing.Font("Verdana", 30); //设置字体颜色 System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Gray); graphic.DrawString(syWord, f, b, 20, 120); graphic.Dispose(); //保存文字水印图片 image.Save(syPath); image.Dispose(); } #endregion #region 在图片上添加图片水印 /// <summary> /// 在图片上添加图片水印 /// </summary> /// <param name="path">原服务器上的图片路径</param> /// <param name="syPicPath">水印图片的路径</param> /// <param name="waterPicPath">生成的水印图片存放路径</param> public static void AddWaterPic(string path, string syPicPath, string waterPicPath) { System.Drawing.Image image = System.Drawing.Image.FromFile(path); System.Drawing.Image waterImage = System.Drawing.Image.FromFile(syPicPath); System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(image); graphic.DrawImage(waterImage, new System.Drawing.Rectangle(image.Width - waterImage.Width, image.Height - waterImage.Height, waterImage.Width, waterImage.Height), 0, 0, waterImage.Width, waterImage.Height, System.Drawing.GraphicsUnit.Pixel); graphic.Dispose(); image.Save(waterPicPath); image.Dispose(); } #endregion #region 处理图片的缩略图 /// <summary> /// 处理图片的缩略图 /// </summary> /// <param name="type">图片类型,例如:jpg,gif,gpeg,png,bmp...</param> /// <param name="path">保存之后的图片路径</param> /// <param name="w">图片处理的宽度</param> /// <param name="h">图片处理的高度</param> /// <param name="OldPath">图片原始路径,例如:../../Upload</param> /// <param name="NewPath">图片保存路径,例如:Upload</param> /// <param name="delete">图片上传完成是否删除原始图片</param> /// <param name="color">图片大小不足时采用什么背景颜色填充</param> /// <returns>返回处理好的图片路径</returns> public static string PhotoHelper(string type, string path, int w, int h, string OldPath, string NewPath, bool delete, System.Drawing.Color color) { System.IO.FileInfo fileInfo = new System.IO.FileInfo(path); string fileName = fileInfo.Name; string sFileName = "0" + fileName; string fileType = type.ToLower(); if (fileType == "bmp" || type == "jpeg" || fileType == "gif" || fileType == "jpg" || type == "png") { string sFilePath = HttpContext.Current.Server.MapPath(OldPath + "/" + sFileName); if (System.IO.File.Exists(path)) { System.Drawing.Bitmap img = new System.Drawing.Bitmap(path); int width = img.Width; int height = img.Height; int NewWidth = w; int NewHeight = h; if (width > w || height > h) { #region 将图片等比例缩放 double proportion_width = 0;//宽度比例 double proportion_height = 0;//高度比例 proportion_width = double.Parse(width.ToString()) / double.Parse(w.ToString()); proportion_height = double.Parse(height.ToString()) / double.Parse(h.ToString()); if (proportion_width > proportion_height) { double h_new = double.Parse(height.ToString()) / proportion_width; string[] temp = h_new.ToString().Split('.'); NewHeight = int.Parse(temp[0]); } else { double w_new = double.Parse(width.ToString()) / proportion_height; string[] temp = w_new.ToString().Split('.'); NewWidth = int.Parse(temp[0]); } #endregion MakeThumNail(path, sFilePath, NewWidth, NewHeight, w, h, color);//返回缩放后的图片 if (delete == true && File.Exists(path) == true) { img.Dispose(); Thread.Sleep(30); File.Delete(path); } } else { MakeThumNail(path, sFilePath, width, height, w, h, color);//返回缩放后的图片 if (delete == true && File.Exists(path) == true) { img.Dispose(); Thread.Sleep(30); File.Delete(path); } } } else { return "0";//文件不存在 } } else { return "1";//文件格式不正确 } return NewPath + "/" + sFileName; } #endregion #region 图片处理|服务器控件上传 /// <summary> /// 图片处理|服务器控件上传 /// </summary> /// <param name="file">FileUpload控件名</param> /// <param name="OldPath">图片原始路径,例如:../../Upload</param> /// <param name="NewPath">图片保存路径,例如:Upload</param> /// <param name="width">图片缩放之后的宽度</param> /// <param name="height">图片缩放之后的高度</param> /// <param name="delete">图片上传完成是否删除原始图片</param> /// <param name="color">图片大小不足时采用什么背景颜色填充</param> /// <returns>返回处理好的图片路径</returns> public static string Upload_Server(FileUpload file, string OldPath, string NewPath, int width, int height, bool delete, System.Drawing.Color color) { string Action = ""; if (file.PostedFile.FileName != null && file.PostedFile.FileName.ToString() != "") { string name = file.PostedFile.FileName.ToString(); string type = name.Substring(name.LastIndexOf(".") + 1).ToLower(); string[] temp = name.Split('\\'); string path = temp[temp.Length - 1]; int bytes = file.PostedFile.ContentLength; if (type == "jpg" || type == "gif" || type == "jpeg" || type == "png" || type == "bmp") { if (bytes > 1024 * 1024 * 2) { Action = "2";//图片大与2M了 } else { path = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + type; System.Web.HttpServerUtility server = System.Web.HttpContext.Current.Server; string PhotoUrl = server.MapPath(OldPath + "/" + path); file.SaveAs(PhotoUrl); Action = PhotoHelper(type, PhotoUrl, width, height, OldPath, NewPath, delete, color); } } else { Action = "1";//图片格式不正确 } } return Action; } #endregion }
试试其它关键字
图片上传
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3