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;
namespace My_Function
{
/// <summary>
/// 我的function函数库
/// </summary>
public class function
{
//过滤单引号'
public string cleanStr(string str)
{
StringBuilder bs = new StringBuilder(str);
bs.Replace("'", "");
return bs.ToString();
}
//判断是否是数字,如果是则返回该值,否则返回0
public int IsNumeric(string str)
{
try
{
int num = Int32.Parse(str);
return num;
}
catch
{
return 0;
}
}
//md5加密
public string MD5(string str)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(str.Trim(), "md5");
}
//生成缩略图,参数说明:target_path为源图片URL,target_path为目标缩略图的路径(包括图片名称),max_w为宽的最大值,max_h为高的最大值
public bool Create_small_pic(string source_path, string target_path, int max_w, int max_h)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(source_path);
int img_width = img.Width;
int img_height = img.Height;
int img_w = 0;
int img_h = 0;
if (img_width > max_w)
{
img_w = max_w;
img_h = (int)Math.Floor(Convert.ToDouble(img_w) / Convert.ToDouble(img_width) * Convert.ToDouble(img_height));
if ( img_h > max_h)
{
img_h = max_h;
img_w = (int)Math.Floor(Convert.ToDouble(img_h) / Convert.ToDouble(img_height) * Convert.ToDouble(img_width));
}
}
else
{
if (img_height > max_h)
{
img_h = max_h;
img_w = (int)Math.Floor(Convert.ToDouble(img_h) / Convert.ToDouble(img_height) * Convert.ToDouble(img_width));
if (img_w > max_w)
{
img_w = max_w;
img_h = (int)Math.Floor(Convert.ToDouble(img_w) / Convert.ToDouble(img_width) * Convert.ToDouble(img_height));
}
}
else
{
img_w = (int)Math.Floor(Convert.ToDouble(img_width));
img_h = (int)Math.Floor(Convert.ToDouble(img_height));
}
}
System.Drawing.Bitmap new_img = new System.Drawing.Bitmap(img_w, img_h);
System.Drawing.Graphics gs = System.Drawing.Graphics.FromImage(new_img);
gs.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
gs.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
gs.Clear(System.Drawing.Color.Transparent);//清空画布并以透明背景色填充
gs.DrawImage(img, new System.Drawing.Rectangle(0, 0, img_w, img_h), new System.Drawing.Rectangle(0, 0, img_width, img_height), System.Drawing.GraphicsUnit.Pixel);
try
{
string file_ext = target_path.Substring(target_path.Length - 3, 3);//取得图片的后缀
if (file_ext.Trim().ToLower() == "gif")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Gif);//生成Gif缩略图
}
if (file_ext.Trim().ToLower() == "jpg")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Jpeg);//生成Jpeg缩略图
}
if (file_ext.Trim().ToLower() == "bmp")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Bmp);//生成Bmp缩略图
}
if (file_ext.Trim().ToLower() == "png")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Png);//生成Png缩略图
}
return true;
}
catch (Exception ex)
{
return false;
throw ex;
}
finally
{
new_img.Dispose();
gs.Dispose();
img.Dispose();
}
}
}
}
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;
namespace My_Function
{
/// <summary>
/// 我的function函数库
/// </summary>
public class function
{
//过滤单引号'
public string cleanStr(string str)
{
StringBuilder bs = new StringBuilder(str);
bs.Replace("'", "");
return bs.ToString();
}
//判断是否是数字,如果是则返回该值,否则返回0
public int IsNumeric(string str)
{
try
{
int num = Int32.Parse(str);
return num;
}
catch
{
return 0;
}
}
//md5加密
public string MD5(string str)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(str.Trim(), "md5");
}
//生成缩略图,参数说明:target_path为源图片URL,target_path为目标缩略图的路径(包括图片名称),max_w为宽的最大值,max_h为高的最大值
public bool Create_small_pic(string source_path, string target_path, int max_w, int max_h)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(source_path);
int img_width = img.Width;
int img_height = img.Height;
int img_w = 0;
int img_h = 0;
if (img_width > max_w)
{
img_w = max_w;
img_h = (int)Math.Floor(Convert.ToDouble(img_w) / Convert.ToDouble(img_width) * Convert.ToDouble(img_height));
if ( img_h > max_h)
{
img_h = max_h;
img_w = (int)Math.Floor(Convert.ToDouble(img_h) / Convert.ToDouble(img_height) * Convert.ToDouble(img_width));
}
}
else
{
if (img_height > max_h)
{
img_h = max_h;
img_w = (int)Math.Floor(Convert.ToDouble(img_h) / Convert.ToDouble(img_height) * Convert.ToDouble(img_width));
if (img_w > max_w)
{
img_w = max_w;
img_h = (int)Math.Floor(Convert.ToDouble(img_w) / Convert.ToDouble(img_width) * Convert.ToDouble(img_height));
}
}
else
{
img_w = (int)Math.Floor(Convert.ToDouble(img_width));
img_h = (int)Math.Floor(Convert.ToDouble(img_height));
}
}
System.Drawing.Bitmap new_img = new System.Drawing.Bitmap(img_w, img_h);
System.Drawing.Graphics gs = System.Drawing.Graphics.FromImage(new_img);
gs.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
gs.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
gs.Clear(System.Drawing.Color.Transparent);//清空画布并以透明背景色填充
gs.DrawImage(img, new System.Drawing.Rectangle(0, 0, img_w, img_h), new System.Drawing.Rectangle(0, 0, img_width, img_height), System.Drawing.GraphicsUnit.Pixel);
try
{
string file_ext = target_path.Substring(target_path.Length - 3, 3);//取得图片的后缀
if (file_ext.Trim().ToLower() == "gif")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Gif);//生成Gif缩略图
}
if (file_ext.Trim().ToLower() == "jpg")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Jpeg);//生成Jpeg缩略图
}
if (file_ext.Trim().ToLower() == "bmp")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Bmp);//生成Bmp缩略图
}
if (file_ext.Trim().ToLower() == "png")
{
new_img.Save(target_path, System.Drawing.Imaging.ImageFormat.Png);//生成Png缩略图
}
return true;
}
catch (Exception ex)
{
return false;
throw ex;
}
finally
{
new_img.Dispose();
gs.Dispose();
img.Dispose();
}
}
}
}
文章来源:桂林唯创网络