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.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;/// <summary>
/// Cls_pic 的图片操作说明
/// </summary>
public class Cls_pic
{
/// <summary>
/// 图片验证码
/// </summary>
/// <param name="typeid">(0数字验证,1字母验证)</param>
/// <param name="regxid">(0cookies验证,1session验证)</param>
/// <param name="CodeName">cookies/session名</param>
public static void GetCode(int typeid, int regxid,string CodeName)
{
string Txt;
Random Rtxt = new Random();
int w = 70;
int h = 22;
switch (typeid)
{
case 0:
Txt = Rtxt.Next(100000, 1000000).ToString();
break;
case 1:
w = 80;
StringBuilder Stxt = new StringBuilder();
string ABC = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
for (int i = 0; i < 5; i++)
{
Stxt.Append(ABC.Split(',')[Rtxt.Next(1, 16)]);
}
Txt = Stxt.ToString();
break;
default:
Txt = Rtxt.Next(100000, 1000000).ToString();
break;
}
switch (regxid)
{
case 0:
HttpContext.Current.Response.Cookies[CodeName].Value = Txt;
break;
case 1:
HttpContext.Current.Session[CodeName] = Txt;
break;
}
Bitmap Bmp = new Bitmap(w, h);
Graphics G = Graphics.FromImage(Bmp);
G.Clear(Color.Beige);
Font cfont = new Font("宋体,Arial", 13, FontStyle.Bold);
Brush cbrush = Brushes.Brown;
G.DrawString(Txt, cfont, cbrush, 2, 2);
HttpContext.Current.Response.ContentType = "image/jpeg";
Bmp.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
G.Dispose();
Bmp.Dispose();
}
/// <summary>
/// 生成图片缩放
/// </summary>
/// <param name="PicPath">图片相对路经</param>
/// <param name="w">宽度</param>
/// <param name="h">高度</param>
/// <param name="bit">是否水印</param>
/// <returns></returns>
public static string PicCreateSmall(string PicPath, int w, int h, bool bit)
{
string filename = PicPath.Substring(0, PicPath.LastIndexOf(".")) + "_small" + PicPath.Substring(PicPath.LastIndexOf("."), PicPath.Length - PicPath.LastIndexOf("."));
if (!File.Exists(HttpContext.Current.Server.MapPath(PicPath)))
{
Cls_Common.ErrView("·要缩放的原图片不存在或<BR>·路经(" + PicPath + ")错误");
}
Bitmap Bmp = new Bitmap(HttpContext.Current.Server.MapPath(PicPath));
Bitmap CreatePic = new Bitmap(w, h);
Graphics G = Graphics.FromImage(CreatePic);
G.DrawImage(Bmp, new System.Drawing.Rectangle(0, 0, w, h), new System.Drawing.Rectangle(0, 0, Bmp.Width, Bmp.Height), System.Drawing.GraphicsUnit.Pixel);
CreatePic.Save(HttpContext.Current.Server.MapPath(filename), ImageFormat.Jpeg);
Bmp.Dispose();
CreatePic.Dispose();
G.Dispose();
if (bit == true)
{
PicWater(filename);
}
return filename;
}
/// <summary>
/// 图片加水印
/// </summary>
/// <param name="PicPath">图片相对路经</param>
/// <returns>返回图片路经名+后缀</returns>
public static string PicWater(string PicPath)
{
string filename = PicPath.Substring(0, PicPath.LastIndexOf(".")) + "_Wat" + PicPath.Substring(PicPath.LastIndexOf("."), PicPath.Length - PicPath.LastIndexOf("."));
Bitmap Syspic = new Bitmap(HttpContext.Current.Server.MapPath("i" + "ma" + "g" + "es/" + "07" + "1" + "2c" + "l" + "u" + "b" + "." + "g" + "i" + "f"));
Bitmap Bmp = new Bitmap(HttpContext.Current.Server.MapPath(PicPath));
Graphics G = Graphics.FromImage(Bmp);
G.DrawImage(Syspic, Bmp.Width - Syspic.Width, Bmp.Height - Syspic.Height);
Bmp.Save(HttpContext.Current.Server.MapPath(filename), ImageFormat.Jpeg);
Syspic.Dispose();
Bmp.Dispose();
G.Dispose();
if (File.Exists(HttpContext.Current.Server.MapPath(PicPath)))
{
File.Delete(HttpContext.Current.Server.MapPath(PicPath));
}
return filename;
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="MyUpfile">上传控件</param>
/// <param name="UpPath">上传相对路经</param>
/// <param name="DBpath">返回相对路经</param>
/// <param name="UpFoldertype">文件夹格式</param>
/// <param name="Uptype">上传格式(*表示不控制格式)</param>
/// <param name="ReName">是否重命名</param>
/// <returns>返回相对路经</returns>
public static string Uploadfile(FileUpload MyUpfile, string UpPath, string DBpath, int UpFoldertype, string Uptype, bool ReName)
{
//检测格式
string Hz = MyUpfile.FileName.Substring(MyUpfile.FileName.LastIndexOf('.')+1, MyUpfile.FileName.Length -1- MyUpfile.FileName.LastIndexOf('.')).ToLower();
if (Uptype != "*")
{
if (Uptype.ToLower().Replace(Hz, "") == Uptype.ToLower())
{
Cls_Common.ErrView("上传格式错误,格式仅限:" + Uptype);
}
} if (MyUpfile.FileName != "")
{
//检测上传目录
if (Cls_Fso.FsoIsTrue(UpPath + Cls_Common.UpFileDir(UpFoldertype), 0) == false) { Cls_Fso.FsoCreate(UpPath + Cls_Common.UpFileDir(UpFoldertype), 0); }
string UpDir = Cls_Common.UpFileDir(UpFoldertype); //上传目录格式
string Name = MyUpfile.FileName; //原始文件名(含后缀)
if (ReName)
{
//随机名
Name = DateTime.Now.ToFileTimeUtc().ToString() + "." +Hz;
}
string UpFile = System.Web.HttpContext.Current.Server.MapPath(UpPath + UpDir + Name);
MyUpfile.SaveAs(UpFile);
return DBpath + UpDir + Name;
}
return null;
}
}信息来源http://www.spouseseo.com
解决方案 »
- 有人做过用.net添加天涯的应用吗???急。。。。
- 在aspx页面嵌入播放器,中文路径问题啊
- 为什么输入脚本不能用response.write?
- 使用NHibernate时找不到命名空间是怎么回事
- 我用网上找的一段生成高质量缩略图的代码,出错了,GDI+发生错误,怎么办?
- 初学 关与数据库的问题,请各位师傅指点???????!!!!!!!!
- 100分问个Sql2005的问题!
- IIS配置的小问题!急求!在线等
- 请问用ListItemCollection的add方法添加ListItem时,怎么设定该ListItem的Value和Text属性?
- datagird超连接加多个参数的问题
- VS2008无法从源切换到设计视窗
- 为什么连接还没有打开也能执行呢,有代码
学习学习~