iextensionunit类_.net 操作图片类

public class ImgHelp

{

//public static string gif = "image/gif";

//public static string jpg = "image/pjpeg";

//public static string png = "image/x-png";

Page pag = new Page();

string[] Imgtype = { "image/gif", "image/pjpeg", "image/x-png" };

public static string thisYear = DateTime.Now.Year.ToString();

public static string thisMonth = DateTime.Now.Month.ToString();

public static string thisDay = DateTime.Now.Day.ToString();

string strDate = thisYear + thisMonth + thisDay + DateTime.Now.Hour.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();

///

/// 保存上传的文件返回true为保存成功Massage为文件路径,返回false为保存失败Massage错误信息

///

///

/// 允许上传的最大值(以字节为单位)

/// 存储路径

/// 存储信息

///

public bool UpImg(FileUpload ProducFile, int size, string addurl, out string Massage)

{

string mag;

bool su = false;

int i = ThrowImg(ProducFile, size, out mag);

if (i == 1)

{

//新文件名

string NewFileName = strDate + ProducFile.FileName.Replace("~", "").Replace("|", "").Replace("&", "");

string url = addurl;

string PddUrl = pag.Server.MapPath(url);

try

{

if (!Directory.Exists(PddUrl))

{

Directory.CreateDirectory(PddUrl);

}

if (File.Exists(pag.Server.MapPath(url + NewFileName)) == false)

{

ProducFile.SaveAs(pag.Server.MapPath(url + NewFileName));

Massage = url + NewFileName;

su = true;

}

else

{

Massage = "上传文件名已经存在,请重新上传!";

}

}

catch

{

Massage = "文件上传时程序出现异常,请重新上传!";

}

}

else

{

Massage = mag;

}

return su;

}

///

/// 上传图片关生成 缩略图

///

/// 控件

/// 允许上传的最大值(以字节为单位)

/// 存储路径

/// 保存文件前缀名集合

/// ,宽度集合

/// 高度集合

/// 返回上传信息

///

public bool UpImgAndBreviary(FileUpload ProducFile, int size, string addurl,string[] frontName,int[] ImgWid,int[] ImgHid,out string Massage)

{

string mag;

bool key = false;

int i = ThrowImg(ProducFile, size, out mag);

if (i != 1)

{

Massage = mag;

key = false;

}

else if (frontName.Length <= 0 || frontName.Length != ImgWid.Length || frontName.Length != ImgHid.Length)

{

Massage = "缩略图信息不相符";

key = false;

}

else

{

//新文件名

string NewFileName = "primary_" + strDate + ProducFile.FileName.Replace("primary_", "");

//存储路径

string url = addurl + thisYear + "/" + thisMonth + "/" + thisDay;

//转为服务器的绝对路径

string PddUrl = pag.Server.MapPath(url);

try

{

//判断路径目录是否存在

if (!Directory.Exists(PddUrl))

{

//创建目录

Directory.CreateDirectory(PddUrl);

if (File.Exists(pag.Server.MapPath(url + "/" + NewFileName)) == false)

{

//保存文件

ProducFile.SaveAs(pag.Server.MapPath(url + "/" + NewFileName));

//返回文件地址

Massage = url + "/" + NewFileName;

//生成缩略图

for (int j = 0; j < frontName.Length; j++)

{

BreviaryImg(Massage, Massage.Replace("primary_", frontName[j]), ImgWid[j], ImgHid[j]);

}

key = true;

//生成两张缩略图,保存名是在原图名前加了一个middle_,small_。

//newwidth1 = 200;

//newheight1 = 200;

//newwidth2 = 60;

//newheight2 = 60;

//BreviaryImg(Massage, Massage.Replace("primary_", "middle_"), newwidth1, newheight1);

//BreviaryImg(Massage, Massage.Replace("primary_", "small_"), newwidth2, newheight2);

}

else

{

Massage = "上传文件重名,请重新上传!";

key = false;

}

}

else

{

if (File.Exists(pag.Server.MapPath(url + "/" + NewFileName)) == false)

{

//保存文件

ProducFile.SaveAs(pag.Server.MapPath(url + "/" + NewFileName));

//返回保存地址

Massage = url + "/" + NewFileName;

//生成缩略图。

for (int j = 0; j < frontName.Length; j++)

{

BreviaryImg(Massage, Massage.Replace("primary_", frontName[j]), ImgWid[j], ImgHid[j]);

}

key = true;

}

else

{

Massage = "上传文件名已经存在,请重新上传!";

key = false;

}

}

}

catch

{

Massage = "文件上传时出现程序错误,请重新上传!";

key = false;

}

}

return key;

}

///

/// 检测上传文件

///

///

/// 允许上传最大值

/// 检测信息

///

private int ThrowImg(FileUpload ProducFile, int Size, out string Massage)

{

int i = 0;

Boolean ImgOK = false;

if (ProducFile.HasFile)

{

//图片类型集

string[] ImgExtension = { ".gif", ".jpg", ".jpeg", ".png" };

//文件类型

string FileType = ProducFile.PostedFile.ContentType;

//文件扩展名

string FileExType = Path.GetExtension(ProducFile.FileName).ToLower();

//匹配类型

for (int k = 0; k < ImgExtension.Length; k++)

{

if (FileExType == ImgExtension[k])

{

foreach (string type in Imgtype)

{

if (FileType == type)

{

ImgOK = true;

break;

}

}

}

}

if (ImgOK == true)

{

//获取文件大小

int FileSize = ProducFile.PostedFile.ContentLength;

if (FileSize < Size)

{

i = 1;

Massage = "";

}

else

{

Massage = "上传文件过大!";

i = 0;

}

}

else

{

Massage = "上传的文件的格式不合法!";

i = 0;

}

}

else

{

Massage = "请选择正确的文件路径!";

i = 0;

}

return i;

}

///

/// 生成缩缩略图

///

/// 原图路径

/// 缩略图存储路径

/// 缩略图宽

/// 缩略图高

private void BreviaryImg(string imagePath, string savePath, int wid, int hid)

{

string originalImagePath = pag.Server.MapPath(imagePath);

string thumbnailPath = pag.Server.MapPath(savePath);

System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

//int towidth = wid;

//int toheight = hid;

int x = 0;

int y = 0;

int ow = originalImage.Width;

int oh = originalImage.Height;

//新建一个bmp图片

System.Drawing.Image bitmap = new System.Drawing.Bitmap(wid, hid);

//新建一个画板

System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

//设置高质量插值法

g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度

g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充

g.Clear(System.Drawing.Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分

g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, wid, hid), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);

try

{

//以原格式保存缩略图

bitmap.Save(thumbnailPath, originalImage.RawFormat);

}

catch (System.Exception e)

{

throw e;

}

finally

{

originalImage.Dispose();

bitmap.Dispose();

g.Dispose();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值