using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Drawing;
using System.Drawing.Imaging;
public partial class test_ImageTest : System.Web.UI.Page
{
static Hashtable htmimes = new Hashtable();
internal readonly string ALLowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
protected override void OnInit(EventArgs e)
{
htmimes[".jpe"] = "image/jpeg";
htmimes[".jpeg"] = "image/jpeg";
htmimes[".jpg"] = "image/jpg";
htmimes[".png"] = "image/png";
htmimes[".tif"] = "image/tif";
htmimes[".tiff"] = "image/tiff";
htmimes[".bmp"] = "image/bmp";
}
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 获取图像解码器的所有相关信息
/// </summary>
/// <param name="mimnType"></param>
/// <returns></returns>
static ImageCodecInfo GetCodecInfo(string mimnType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ic in CodecInfo)
{
if (ic.MimeType == mimnType)
return ic;
}
return null;
}
/// <summary>
/// 检测扩展名的有效性
/// </summary>
/// <param name="Sext"></param>
/// <returns></returns>
bool CheckValidExt(string Sext)
{
bool flage = false;
string[] aExt = ALLowExt.Split('|');
foreach (string filetype in aExt)
{
if (filetype.ToLower() == Sext)
{
flage = true;
break;
}
}
return flage;
}
/// <summary>
/// 保存图片
/// </summary>
/// <param name="image"></param>
/// <param name="SavePath"></param>
/// <param name="ici"></param>
void SaveImage(System.Drawing.Image image, string SavePath, ImageCodecInfo ici)
{
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, ((long)90));
image.Save(SavePath, ici, parameters);
parameters.Dispose();
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="sourceImagePath"></param>
/// <param name="thumbnailImagePath"></param>
/// <param name="thumbnailImageWidth"></param>
public void ToThumbnailImages(string sourceImagePath, string thumbnailImagePath, int thumbnailImageWidth)
{
string sExt = sourceImagePath.Substring(sourceImagePath.LastIndexOf('.')).ToLower();
if (sourceImagePath.ToString() == System.String.Empty)
throw new NullReferenceException("SourceImagePath is null !");
if (!CheckValidExt(sExt))
{
throw new ArgumentException("原图片格式不正确,支持的格式有["+ALLowExt+"]","SourceImagePath");
}
System.Drawing.Image image=System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath(sourceImagePath));
//num,缩小图片的图片高度
int numHeiht=(thumbnailImageWidth/4*3);
int width=image.Width;
int height=image.Height;
if ((((double)width) / ((double)height)) >= 1.33333333333333f)
{
numHeiht = ((height * thumbnailImageWidth) / width);
}
else
{
thumbnailImageWidth = (width * numHeiht) / height;
}
if ((thumbnailImageWidth < 1) || (numHeiht < 1))
return ;
Bitmap bitmap = new Bitmap(thumbnailImageWidth, numHeiht, PixelFormat.Format32bppRgb);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.Clear(Color.Transparent);
graphics.DrawImage(image, new Rectangle(0, 0, thumbnailImageWidth, numHeiht));
image.Dispose();
try
{
string savepath = (thumbnailImagePath == null ? sourceImagePath : thumbnailImagePath);
SaveImage(bitmap, HttpContext.Current.Server.MapPath(savepath), GetCodecInfo((string)htmimes[sExt]));
}
catch (System.Exception ee)
{
throw ee;
}
finally
{
bitmap.Dispose();
graphics.Dispose();
}
}
/// <summary>
/// 裁剪出指定的部分
/// </summary>
/// <param name="b"></param>
/// <param name="startX"></param>
/// <param name="startY"></param>
/// <param name="iWidth"></param>
/// <param name="iHeight"></param>
/// <returns>bmpout</returns>
public static Bitmap KiCut(Bitmap b, int startX, int startY, int iWidth, int iHeight)
{
if (b == null)
return null;
int w = b.Width;
int h = b.Height;
if (startX >= w || startY >= h)
return null;
if (startX + iWidth > w)
iWidth = w - startX;
if (startY + iHeight > h)
iHeight = h - startY;
try
{
Bitmap bmpout = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpout);
g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(startX, startY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpout;
}
catch
{
return null;
}
}
public void ImageReduceCutOut(int int_Width, int int_Height, string Inpu_ImageUrl, string out_ImageUrl)
{
// ===上传标准图大小===
int int_Standard_Width = 160;
int int_Standard_Height = 160;
int Reduce_Width = 0; // 缩小的宽度
int Reduce_Height = 0; // 缩小的高度
int CutOut_Width = 0; // 裁剪的宽度
int CutOut_Height = 0; // 裁剪的高度
int level = 100; //缩略图的质量 1-100的范围
if (int_Standard_Height * int_Width / int_Standard_Width > int_Height)
{
Reduce_Width = int_Width;
Reduce_Height = int_Standard_Height * int_Width / int_Standard_Width;
CutOut_Width = int_Width;
CutOut_Height = int_Height;
}
if (int_Standard_Width * int_Width / int_Standard_Height > int_Width)
{
Reduce_Width = int_Standard_Width * int_Height / int_Standard_Height;
Reduce_Height = int_Height;
CutOut_Width = int_Width;
CutOut_Height = int_Height;
}
else
{
Reduce_Width = int_Width;
Reduce_Height = int_Height;
CutOut_Width = int_Width;
CutOut_Height = int_Height;
}
System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(Inpu_ImageUrl));
System.Drawing.Image thumbNailImage = oldimage.GetThumbnailImage(Reduce_Width, Reduce_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbNailCallBack), IntPtr.Zero);
Bitmap bm = new Bitmap(thumbNailImage);
// ===处理JPG质量的函数===
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null;
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
ici = codec;
}
EncoderParameters ep = new EncoderParameters();
ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);
bm.Save(Server.MapPath("2.jpg"), ici, ep);
// ===裁剪图片===
Rectangle cloneRect = new Rectangle(0, 0, CutOut_Width, CutOut_Height);
PixelFormat format = bm.PixelFormat;
Bitmap cloneBitmap = bm.Clone(cloneRect, format);
// ===保存图片===
cloneBitmap.Save(Server.MapPath(out_ImageUrl), ici, ep);
}
public bool ThumbNailCallBack()
{
return false;
}
}