现在的位置: 首页 > 综合 > 正文

【优化】C#图片水印和缩略图片

2012年11月23日 ⁄ 综合 ⁄ 共 11360字 ⁄ 字号 评论关闭

前段时间写的在实际运用中,发现了很多不足之处,所以优化了一下,当然还是有问题的(不过基本使用没什么问题):

考虑到有些图片是放置在本站内,增加了一个临时文件处理方式FileCache(Delete,Save),对于节省空间来说还是可以用的

//使用方式及参数

A、缩略图片

 

View Code

string a = skeletonize.Resizepic(savepath, commonfun.ResizeType.XY, "/images/", ImgClassLib.commonfun.ImageType.JPEG, 124, 151, commonfun.FileCache.Save, out warning);

B、水印图片

View Code

string c = watermark.makewatermark(savepath, "/images/w.png", commonfun.WaterType.Random, "/images/", commonfun.ImageType.JPEG, commonfun.FileCache.Delete, out warning2);

一下是code commonfun类

 

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.IO;

namespace ImgClassLib
{
public class commonfun
{
/// <summary>
/// 根据文件路径判断文件是否存在
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="model">返回模式,m:返回map地址不检查文件是否存在,c:检测文件是否存在,并返回map地址</param>
/// <param name="mappath">map路径</param>
/// <returns></returns>
public static bool FileExistMapPath(string filepath, FileCheckModel model, out string mappath)
{
bool checkresult = false;
switch (model)
{
case FileCheckModel.M:
mappath = HttpContext.Current.Server.MapPath(filepath);
checkresult = true;
break;
case FileCheckModel.C:
if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(filepath)))
{
mappath = HttpContext.Current.Server.MapPath(filepath);
checkresult = true;
}
else
{
mappath = null;
checkresult = false;
}
break;
default:
mappath = "";
checkresult = false;
break;
}
return checkresult;
}

/// <summary>
/// 图片保存类型
/// JPEG:.jpg格式;
/// GIF:.gif格式;
/// PNG:.png格式;
/// </summary>
public enum ImageType
{
JPEG,
GIF,
PNG
}

/// <summary>
/// 水印模式
/// Center:中间;
/// CenterUp:中上;
/// CenterDown:中下;
/// LeftUp:左上;
/// LeftDown:左下;
/// RightUp:右上;
/// RightDown:右下;
/// Random:随机;
/// </summary>
public enum WaterType
{
Center,
CenterUp,
CenterDown,
LeftUp,
LeftDown,
RightUp,
RightDown,
Random
}

/// <summary>
/// 缩略模式
/// X--按宽度缩放,高着宽比例;
/// Y--按高度缩放,宽着宽比例;
/// XY--按给定mwidth,mheight(此模式mwidth,mheight为必须值)进行缩略;
/// </summary>
public enum ResizeType
{
X,
Y,
XY
}

/// <summary>
/// 文件检测模式
/// M:不检测文件是否存在,返回ServerMapPath;
/// C:检测文件是否存在,返回ServerMapPath;
/// </summary>
public enum FileCheckModel
{
M,
C
}

/// <summary>
/// 原图文件是否保存
/// Delete:保存
/// Save:不保存,删除
/// </summary>
public enum FileCache
{
Save,
Delete
}
}
}

 

 

以下是code skeletonize类

 

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.Drawing;
using System.IO;


namespace ImgClassLib
{
/// <summary>
/// 图片处理:缩略图片
/// </summary>
public class skeletonize
{

/// <summary>
/// 根据指定:缩略宽、高,缩略图片并保存
/// 返回图片虚拟路径,和一个警告信息,可根据此信息获取图片合成信息
/// </summary>
/// <param name="picpath">原图路径</param>
/// <param name="model">缩略模式[X,Y,XY](默认XY模式)</param>
/// <param name="spath">文件保存路径(默认跟路径)</param>
/// <param name="imgtype">图片保存类型</param>
/// <param name="mwidth">缩略宽度(默认原图高度)</param>
/// <param name="mheight">缩略高度(默认原图高度)</param>
/// <param name="filecache">原文件处理方式</param>
/// <param name="warning">处理警告信息</param>
/// <returns>错误,返回错误信息;成功,返回图片路径</returns>

public static string Resizepic(string picpath, commonfun.ResizeType model, string spath, commonfun.ImageType imgtype, double? mwidth, double? mheight, commonfun.FileCache filecache, out string warning)
{
//反馈信息
System.Text.StringBuilder checkmessage = new System.Text.StringBuilder();

//文件保存路径
spath = string.IsNullOrEmpty(spath) ? "/" : spath;

//缩略宽度
double swidth = mwidth.HasValue ? double.Parse(mwidth.ToString()) : 0;

//缩略高度
double sheight = mheight.HasValue ? double.Parse(mheight.ToString()) : 0;

//从指定源图片,创建image对象
string _sourceimg_common_mappath = "";

//检测源文件
bool checkfile = false;
checkfile=commonfun.FileExistMapPath(picpath,commonfun.FileCheckModel.C, out _sourceimg_common_mappath);

System.Drawing.Image _sourceimg_common=null;
System.Drawing.Bitmap _currimg_common = null;
System.Drawing.Graphics _g_common = null;

if (checkfile == true)
{
//从源文件创建imgage
_sourceimg_common = System.Drawing.Image.FromFile(_sourceimg_common_mappath);

#region 缩略模式
//缩略模式
switch (model)
{
case commonfun.ResizeType.X:

#region X模式

//根据给定尺寸,获取绘制比例
double _width_scale = swidth / _sourceimg_common.Width;
//高着比例
sheight = _sourceimg_common.Height * _width_scale;

#endregion
; break;
case commonfun.ResizeType.Y:
#region Y模式

//根据给定尺寸,获取绘制比例
double _height_scale = sheight / _sourceimg_common.Height;
//宽着比例
swidth = _sourceimg_common.Width * _height_scale;

#endregion
; break;
case commonfun.ResizeType.XY:
#region XY模式

//当选择XY模式时,mwidth,mheight为必须值
if (swidth < 0 || sheight < 0)
{
checkmessage.Append("error:XY模式,mwidth,mheight为必须值;");
}

#endregion
; break;
default:

#region 默认XY模式

//当默认XY模式时,mwidth,mheight为必须值
if (swidth < 0 || sheight < 0)
{
checkmessage.Append("error:你当前未选择缩略模式,系统默认XY模式,mwidth,mheight为必须值;");
}

; break;
#endregion
}
#endregion
}
else
{
checkmessage.Append("error:未能找到缩略原图片," + picpath + ";");
}

if (string.IsNullOrEmpty(checkmessage.ToString()))
{
//创建bitmap对象
_currimg_common = new System.Drawing.Bitmap((int)swidth, (int)sheight);

_g_common = Graphics.FromImage(_currimg_common);

//设置画笔
_g_common.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
_g_common.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
_g_common.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;

//绘制图片
_g_common.DrawImage(_sourceimg_common, new Rectangle(0, 0, (int)swidth, (int)sheight), new Rectangle(0, 0, _sourceimg_common.Width, _sourceimg_common.Height), GraphicsUnit.Pixel);

//保存图片
string _spath_common_mappath = "";

//获取图片类型的hashcode值,生成图片后缀名
int extro = imgtype.GetHashCode();

string extend = extro == 0 ? ".jpg" : (extro == 1 ? ".gif" : (extro == 2 ? ".png" : ".jpg"));

//全局文件名
spath = spath + Guid.NewGuid().ToString() + extend;

commonfun.FileExistMapPath(spath,commonfun.FileCheckModel.M, out _spath_common_mappath);

switch (imgtype)
{
case commonfun.ImageType.JPEG: _currimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
case commonfun.ImageType.GIF: _currimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Gif); break;
case commonfun.ImageType.PNG: _currimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Png); break;
}

//释放
_sourceimg_common.Dispose();
_currimg_common.Dispose();
_g_common.Dispose();

//处理原文件
int filecachecode = filecache.GetHashCode();

//文件缓存方式:Delete,删除原文件
if (filecachecode == 1)
{
System.IO.File.Delete(_sourceimg_common_mappath);
}

//返回相对虚拟路径
warning = "";
return spath;
}

//释放
if (_sourceimg_common != null)
{
_sourceimg_common.Dispose();
}
if (_currimg_common!=null)
{
_currimg_common.Dispose();
}
if (_g_common != null)
{
_g_common.Dispose();
}

warning = checkmessage.ToString().TrimEnd(';');

return "";
}

}
}

 

 

以下是code watermark类

 

View Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.IO;
using System.Drawing;

namespace ImgClassLib
{
/// <summary>
/// 图片处理:水印图片
/// </summary>
public class watermark
{
/// <summary>
/// 水印图片
/// 【如果图片需要缩略,请使用skeletonize.Resizepic()方法对图片进行缩略】
/// 返回图片虚拟路径,和一个警告信息,可根据此信息获取图片合成信息
/// </summary>
/// <param name="picpath">需要水印的图片路径</param>
/// <param name="waterspath">水印图片路径</param>
/// <param name="watermodel">水印模式</param>
/// <param name="spath">文件保存路径</param>
/// <param name="imgtype">保存文件类型</param>
/// <param name="filecache">原文件处理方式</param>
/// <param name="warning">处理警告信息</param>
/// <returns>错误,返回错误信息;成功,返回图片路径</returns>

public static string makewatermark(string picpath, string waterspath, commonfun.WaterType watermodel, string spath, commonfun.ImageType imgtype, commonfun.FileCache filecache, out string warning)
{
#region
//反馈信息
System.Text.StringBuilder checkmessage = new System.Text.StringBuilder();

//检测源文件
string _sourceimg_common_mappath = "";
bool checkfile = false;

//检测水印源文件
string _sourceimg_water_mappath = "";
bool checkfilewater = false;

checkfile = commonfun.FileExistMapPath(picpath,commonfun.FileCheckModel.C, out _sourceimg_common_mappath);
checkfilewater = commonfun.FileExistMapPath(waterspath, commonfun.FileCheckModel.C, out _sourceimg_water_mappath);

System.Drawing.Image _sourceimg_common = null;
System.Drawing.Image _sourceimg_water = null;

if (checkfile == true)
{
//从指定源文件,创建image对象
_sourceimg_common = System.Drawing.Image.FromFile(_sourceimg_common_mappath);
}
else
{
checkmessage.Append("error:找不到需要的水印图片!" + picpath + ";");
}
if (checkfilewater == true)
{
//从指定源文件,创建image对象
_sourceimg_water = System.Drawing.Image.FromFile(_sourceimg_water_mappath);
}
else
{
checkmessage.Append("error:找不到需要水印图片!" + waterspath + ";");
}
#endregion

#region
if (string.IsNullOrEmpty(checkmessage.ToString()))
{
//源图宽、高
int _sourceimg_common_width =_sourceimg_common.Width;
int _sourceimg_common_height = _sourceimg_common.Height;

//水印图片宽、高
int _sourceimg_water_width = _sourceimg_water.Width;
int _sourceimg_water_height =_sourceimg_water.Height;

#region 水印坐标
//水印坐标
int _sourceimg_water_point_x = 0;
int _sourceimg_water_point_y = 0;

switch (watermodel)
{
case commonfun.WaterType.Center:
_sourceimg_water_point_x = (_sourceimg_common_width - _sourceimg_water_width) / 2;
_sourceimg_water_point_y = (_sourceimg_common_height - _sourceimg_water_height) / 2;
; break;
case commonfun.WaterType.CenterDown:
_sourceimg_water_point_x = (_sourceimg_common_width - _sourceimg_water_width) / 2;
_sourceimg_water_point_y = _sourceimg_common_height - _sourceimg_water_height;
; break;
case commonfun.WaterType.CenterUp:
_sourceimg_water_point_x = (_sourceimg_common_width - _sourceimg_water_width) / 2;
_sourceimg_water_point_y = 0;
; break;
case commonfun.WaterType.LeftDown:
_sourceimg_water_point_x = 0;
_sourceimg_water_point_y = _sourceimg_common_height - _sourceimg_water_height;
; break;
case commonfun.WaterType.LeftUp:
; break;
case commonfun.WaterType.Random:
Random r = new Random();
int x_random = r.Next(0, _sourceimg_common_width);
int y_random = r.Next(0, _sourceimg_common_height);

_sourceimg_water_point_x = x_random > (_sourceimg_common_width - _sourceimg_water_width)
? _sourceimg_common_width - _sourceimg_water_width : x_random;

_sourceimg_water_point_y = y_random > (_sourceimg_common_height - _sourceimg_water_height)
? _sourceimg_common_height - _sourceimg_water_height : y_random;

; break;
case commonfun.WaterType.RightDown:
_sourceimg_water_point_x = _sourceimg_common_width - _sourceimg_water_width;
_sourceimg_water_point_y = _sourceimg_common_height - _sourceimg_water_height;
; break;
case commonfun.WaterType.RightUp:
_sourceimg_water_point_x = _sourceimg_common_width - _sourceimg_water_width;
_sourceimg_water_point_y = 0;
; break;
}
#endregion

//从源图创建画板
System.Drawing.Graphics _g_common = Graphics.FromImage(_sourceimg_common);

//设置画笔
_g_common.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
_g_common.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
_g_common.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;

//绘制水印图片
_g_common.DrawImage(_sourceimg_water, new Rectangle(_sourceimg_water_point_x, _sourceimg_water_point_y, _sourceimg_water_width, _sourceimg_water_height), new Rectangle(0, 0, _sourceimg_water_width, _sourceimg_water_height), GraphicsUnit.Pixel);

//保存图片
string _spath_common_mappath = "";
//全局文件名

//获取图片类型的hashcode值,生成图片后缀名
int extro = imgtype.GetHashCode();
string extend = extro == 0 ? ".jpg" : (extro == 1 ? ".gif" : (extro == 2 ? ".png" : ".jpg"));

spath = spath + Guid.NewGuid().ToString() + extend;

commonfun.FileExistMapPath(spath,commonfun.FileCheckModel.M, out _spath_common_mappath);

switch (imgtype)
{
case commonfun.ImageType.JPEG: _sourceimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
case commonfun.ImageType.GIF: _sourceimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Gif); break;
case commonfun.ImageType.PNG: _sourceimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Png); break;
}


//释放
_sourceimg_common.Dispose();
_sourceimg_water.Dispose();
_g_common.Dispose();

//处理原文件
int filecachecode = filecache.GetHashCode();
//删除原文件
if (filecachecode == 1)
{
System.IO.File.Delete(_sourceimg_common_mappath);
}

warning = "";
return spath;

}
#endregion

//释放
if (_sourceimg_common != null)
{
_sourceimg_common.Dispose();
}
if (_sourceimg_water!=null)
{
_sourceimg_water.Dispose();
}

warning = checkmessage.ToString().TrimEnd(';');
return "";
}
}
}

 

 

           

抱歉!评论已关闭.