using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace ImageTool
{
/// <summary>
/// ImageTool 图片的压缩,加水印等功能
/// </summary>
public class CompressImage
{
#region //返回按钮 压缩图片
public static void SetCompressImage(int bitint, string bitstr, string path, string fileName)
{
System.Drawing.Image aNewImage;
System.Drawing.Image image = System.Drawing.Image.FromFile(path + fileName);
fileName = fileName.ToLower().Replace("big_", "");
decimal width = image.Width;
decimal height = image.Height;
int newwidth, newheight;
if (width > height)
{
newwidth = bitint;
newheight = (int)(height / width * bitint);
}
else
{
newheight = bitint;
newwidth = (int)(width / height * bitint);
}
aNewImage = image.GetThumbnailImage(newwidth, newheight, null, IntPtr.Zero);
Bitmap output = new Bitmap(aNewImage);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, ((long)80));
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/jpeg");
output.Save(path + bitstr + fileName, myImageCodecInfo, parameters);
output.Dispose();
aNewImage.Dispose();
image.Dispose();
}
#endregion
#region //返回高清缩略图
//返回高清缩略图
public static void SetGoodImage(string fileName, string newFile, int maxHeight, int maxWidth)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);
System.Drawing.Imaging.ImageFormat
thisFormat = img.RawFormat;
Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height);
Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
Graphics g = Graphics.FromImage(outBmp);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height),
0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams = new EncoderParameters();
long[] quality = new long[1];
quality[0] = 80;
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICI = null;
for (int x = 0;
x < arrayICI.Length;
x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICI = arrayICI[x];
//设置JPEG编码
break;
}
}
if (jpegICI != null)
{
outBmp.Save(newFile, jpegICI, encoderParams);
}
else
{
outBmp.Save(newFile, thisFormat);
}
img.Dispose();
outBmp.Dispose();
}
// 得到到按比例最佳尺寸
private static Size NewSize(int maxWidth, int maxHeight, int width, int height)
{
double w = 0.0;
double h = 0.0;
double sw = Convert.ToDouble(width);
double sh = Convert.ToDouble(height);
double mw = Convert.ToDouble(maxWidth);
double mh = Convert.ToDouble(maxHeight);
if (sw < mw && sh < mh)
{
w = sw;
h = sh;
}
else if ((sw / sh) > (mw / mh))
{
w = maxWidth;
h = (w * sh) / sw;
}
else
{
h = maxHeight;
w = (h * sw) / sh;
}
return new Size(Convert.ToInt32(w), Convert.ToInt32(h));
}
// 得到图片类型
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
#endregion
}
/// <summary>
///E动力修改, 图片修改类,主要是用来保护图片版权的,版权归原作者所有
/// </summary>
public class ImageModification
{
#region "member fields"
private string modifyImagePath=null;
private string drawedImagePath=null;
private int rightSpace;
private int bottoamSpace;
private int lucencyPercent=70;
private string outPath=null;
#endregion
public ImageModification()
{
}
#region "propertys"
/// <summary>
/// 获取或设置要修改的图像路径
/// </summary>
public string ModifyImagePath
{
get{return this.modifyImagePath;}
set{this.modifyImagePath=value;}
}
/// <summary>
/// 获取或设置在画的图片路径(水印图片)
/// </summary>
public string DrawedImagePath
{
get{return this.drawedImagePath;}
set{this.drawedImagePath=value;}
}
/// <summary>
/// 获取或设置水印在修改图片中的右边距
/// </summary>
public int RightSpace
{
get{return this.rightSpace;}
set{this.rightSpace=value;}
}
//获取或设置水印在修改图片中距底部的高度
public int BottoamSpace
{
get{return this.bottoamSpace;}
set{this.bottoamSpace=value;}
}
/// <summary>
/// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
/// </summary>
public int LucencyPercent
{
get{return this.lucencyPercent;}
set
{
if(value>=0&&value<=100)
this.lucencyPercent=value;
}
}
/// <summary>
/// 获取或设置要输出图像的路径
/// </summary>
public string OutPath
{
get{return this.outPath;}
set{this.outPath=value;}
}
#endregion
#region "methods"
/// <summary>
/// 开始绘制水印
/// </summary>
public void DrawImage()
{
Image modifyImage=null;
Image drawedImage=null;
Graphics g=null;
try
{
//建立图形对象
modifyImage=Image.FromFile(this.ModifyImagePath);
drawedImage=Image.FromFile(this.DrawedImagePath);
g=Graphics.FromImage(modifyImage);
//获取要绘制图形坐标
int x=modifyImage.Width-this.rightSpace;
int y=modifyImage.Height-this.BottoamSpace;
//设置颜色矩阵
float[][] matrixItems ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, (float)this.LucencyPercent/100f, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgAttr=new ImageAttributes();
imgAttr.SetColorMatrix(colorMatrix,ColorMatrixFlag.Default,ColorAdjustType.Bitmap);
//绘制阴影图像
g.DrawImage(
drawedImage,
new Rectangle(x,y,drawedImage.Width,drawedImage.Height),
0,0,drawedImage.Width,drawedImage.Height,
GraphicsUnit.Pixel,imgAttr);
//保存文件
string[] allowImageType={".jpg",".gif",".png",".bmp",".tiff",".wmf",".ico"};
FileInfo file=new FileInfo(this.ModifyImagePath);
ImageFormat imageType=ImageFormat.Gif;
switch(file.Extension.ToLower())
{
case ".jpg":
imageType=ImageFormat.Jpeg;
break;
case ".gif":
imageType=ImageFormat.Gif;
break;
case ".png":
imageType=ImageFormat.Png;
break;
case ".bmp":
imageType=ImageFormat.Bmp;
break;
case ".tif":
imageType=ImageFormat.Tiff;
break;
case ".wmf":
imageType=ImageFormat.Wmf;
break;
case ".ico":
imageType=ImageFormat.Icon;
break;
default:
break;
}
MemoryStream ms=new MemoryStream();
modifyImage.Save(ms,imageType);
byte[] imgData=ms.ToArray();
modifyImage.Dispose();
drawedImage.Dispose();
g.Dispose();
FileStream fs=null;
if(this.OutPath==null || this.OutPath=="")
{
File.Delete(this.ModifyImagePath);
fs=new FileStream(this.ModifyImagePath,FileMode.Create,FileAccess.Write);
}
else
{
fs=new FileStream(this.OutPath,FileMode.Create,FileAccess.Write);
}
if(fs!=null)
{
fs.Write(imgData,0,imgData.Length);
fs.Close();
}
}
finally
{
try
{
drawedImage.Dispose();
modifyImage.Dispose();
g.Dispose();
}
catch{;}
}
}
#endregion
}
/// <summary>
/// 图片水印
/// </summary>
public class WaterMark
{
public static void SetWaterMark(System.Web.UI.Page page, string down_path, string fileName, string typeys)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(down_path + fileName);
ImageModification wm = new ImageModification();
wm.DrawedImagePath = page.Server.MapPath(".") + "/upfile/" + "backlogo.jpg"; //水印图片
wm.ModifyImagePath = down_path + "/" + fileName; //图片的路径
wm.RightSpace = 130; //水印位置
wm.BottoamSpace = image.Height - 12; //水银位置
wm.LucencyPercent = 50; //透明度
wm.OutPath = down_path + typeys + fileName; //生成的文件名
wm.DrawImage();
image.Dispose();
}
}
}