我自己写的图片缩放的类库using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using ImageManipulation;namespace Zz.Common { public class ImageHelper { /// <summary> /// 给图片加水印 /// </summary> /// <param name="originalImg"></param> /// <param name="waterImg"></param> /// <param name="trbl"></param> /// <returns></returns> public static Image WaterImage(Image originalImg, Image waterImg, WaterTRBL trbl) { Image image = (Image)originalImg.Clone(); using (Graphics g = Graphics.FromImage(image)) { Rectangle r = new Rectangle(); r.Height = waterImg.Height; r.Width = waterImg.Width; if (trbl.Top != null) { r.Y = (int)trbl.Top; } if (trbl.Right != null) { r.X = originalImg.Width - waterImg.Width - (int)trbl.Right; } if (trbl.Bottom != null) { r.Y = originalImg.Height - waterImg.Height - (int)trbl.Bottom; } if (trbl.Left != null) { r.X = (int)trbl.Left; } g.DrawImage(waterImg, r, 0, 0, waterImg.Width, waterImg.Height, GraphicsUnit.Pixel); } return image; } /// <summary> /// 等比缩放图片 /// </summary> /// <param name="imgPhoto"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width, int height) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; //int x, y; //位置 int w, h; //缩放后的宽和高 if (sourceWidth > width || sourceHeight > height) { decimal wPercent = (decimal)sourceWidth / width; decimal hPercent = (decimal)sourceHeight / height; decimal nPercent; if (hPercent > wPercent) { nPercent = hPercent; } else { nPercent = wPercent; } w = (int)Math.Round(sourceWidth / nPercent, 0); h = (int)Math.Round(sourceHeight / nPercent, 0); } else { w = sourceWidth; h = sourceHeight; } try { Bitmap bmPhoto = new Bitmap(w, h, PixelFormat.Format24bppRgb); //bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); //grPhoto.Clear(Color.White); //grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量插值法 grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 grPhoto.Clear(System.Drawing.Color.Transparent); grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。 new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。 GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。 grPhoto.Dispose(); if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid) { OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); using (Bitmap quantized = quantizer.Quantize(imgPhoto)) { return quantized; } } else { return bmPhoto; } } catch { return null; } } public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; //int x, y; //位置 int w, h; //缩放后的宽和高 if (sourceWidth > width) { decimal nPercent = (decimal)sourceWidth / width; w = (int)Math.Round(sourceWidth / nPercent, 0); h = (int)Math.Round(sourceHeight / nPercent, 0); } else { w = sourceWidth; h = sourceHeight; } try { Bitmap bmPhoto = new Bitmap(w, h); //bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); //grPhoto.Clear(Color.White); //grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量插值法 grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 grPhoto.Clear(System.Drawing.Color.Transparent); grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。 new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。 GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。 grPhoto.Dispose(); if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid) { OctreeQuantizer quantizer = new OctreeQuantizer(255, 8); return quantizer.Quantize(bmPhoto); } else { return bmPhoto; } } catch { return null; } } public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int height, int flag) { Image thumbnail = GetImageThumbnail(imgPhoto, width, height); return ZipImage(thumbnail, flag); } public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int flag) { Image thumbnail = GetImageThumbnail(imgPhoto, width); return ZipImage(thumbnail, flag); } /// <summary> /// 压缩图片 /// </summary> /// <param name="imgPhoto"></param> /// <param name="flag"></param> /// <returns></returns> public static System.Drawing.Image ZipImage(System.Drawing.Image imgPhoto, int flag) { EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag;//设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } MemoryStream ms = new MemoryStream(); if (jpegICIinfo != null) { imgPhoto.Save(ms, jpegICIinfo, ep);//dFile是压缩后的新路径 } else { imgPhoto.Save(ms, imgPhoto.RawFormat); } System.Drawing.Image zipImage = System.Drawing.Image.FromStream(ms); return zipImage; } catch { return null; } } } }
public class WaterTRBL { public WaterTRBL() { } int? top = null, right = null, bottom = null, left = null; public WaterTRBL(int? top, int? right, int? bottom, int? left) { this.top = top; this.right = right; this.bottom = bottom; this.left = left; } public int? Top { get { return top; } set { top = value; } } public int? Right { get { return right; } set { right = value; } } public int? Bottom { get { return bottom; } set { bottom = value; } } public int? Left { get { return left; } set { left = value; } } }
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using ImageManipulation;namespace Zz.Common
{
public class ImageHelper
{
/// <summary>
/// 给图片加水印
/// </summary>
/// <param name="originalImg"></param>
/// <param name="waterImg"></param>
/// <param name="trbl"></param>
/// <returns></returns>
public static Image WaterImage(Image originalImg, Image waterImg, WaterTRBL trbl)
{ Image image = (Image)originalImg.Clone();
using (Graphics g = Graphics.FromImage(image))
{
Rectangle r = new Rectangle();
r.Height = waterImg.Height;
r.Width = waterImg.Width;
if (trbl.Top != null)
{
r.Y = (int)trbl.Top;
} if (trbl.Right != null)
{
r.X = originalImg.Width - waterImg.Width - (int)trbl.Right;
} if (trbl.Bottom != null)
{
r.Y = originalImg.Height - waterImg.Height - (int)trbl.Bottom;
} if (trbl.Left != null)
{
r.X = (int)trbl.Left;
} g.DrawImage(waterImg, r, 0, 0, waterImg.Width, waterImg.Height, GraphicsUnit.Pixel);
} return image;
} /// <summary>
/// 等比缩放图片
/// </summary>
/// <param name="imgPhoto"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width, int height)
{ int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
//int x, y; //位置
int w, h; //缩放后的宽和高 if (sourceWidth > width || sourceHeight > height)
{
decimal wPercent = (decimal)sourceWidth / width;
decimal hPercent = (decimal)sourceHeight / height;
decimal nPercent;
if (hPercent > wPercent)
{
nPercent = hPercent;
}
else
{
nPercent = wPercent;
} w = (int)Math.Round(sourceWidth / nPercent, 0);
h = (int)Math.Round(sourceHeight / nPercent, 0); }
else
{
w = sourceWidth;
h = sourceHeight;
}
try
{ Bitmap bmPhoto = new Bitmap(w, h, PixelFormat.Format24bppRgb);
//bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//grPhoto.Clear(Color.White);
//grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
//设置高质量插值法
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
grPhoto.Clear(System.Drawing.Color.Transparent);
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。
new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。
GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。 grPhoto.Dispose(); if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid)
{
OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
using (Bitmap quantized = quantizer.Quantize(imgPhoto))
{
return quantized;
}
}
else
{
return bmPhoto;
} }
catch
{ return null;
}
} public static System.Drawing.Image GetImageThumbnail(System.Drawing.Image imgPhoto, int width)
{ int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
//int x, y; //位置
int w, h; //缩放后的宽和高 if (sourceWidth > width)
{
decimal nPercent = (decimal)sourceWidth / width;
w = (int)Math.Round(sourceWidth / nPercent, 0);
h = (int)Math.Round(sourceHeight / nPercent, 0);
}
else
{
w = sourceWidth;
h = sourceHeight;
}
try
{
Bitmap bmPhoto = new Bitmap(w, h);
//bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
//grPhoto.Clear(Color.White);
//grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
//设置高质量插值法
grPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
grPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
grPhoto.Clear(System.Drawing.Color.Transparent);
grPhoto.DrawImage(imgPhoto,
new Rectangle(0, 0, w, h),///它指定所绘制图像的位置和大小。
new Rectangle(0, 0, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。
GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。 grPhoto.Dispose(); if (imgPhoto.RawFormat == ImageFormat.Gif || imgPhoto.RawFormat.Guid == ImageFormat.Gif.Guid)
{
OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
return quantizer.Quantize(bmPhoto);
}
else
{
return bmPhoto;
} }
catch
{ return null;
}
} public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int height, int flag)
{
Image thumbnail = GetImageThumbnail(imgPhoto, width, height);
return ZipImage(thumbnail, flag);
}
public static System.Drawing.Image GetZipImageThumbnail(System.Drawing.Image imgPhoto, int width, int flag)
{
Image thumbnail = GetImageThumbnail(imgPhoto, width);
return ZipImage(thumbnail, flag); }
/// <summary>
/// 压缩图片
/// </summary>
/// <param name="imgPhoto"></param>
/// <param name="flag"></param>
/// <returns></returns>
public static System.Drawing.Image ZipImage(System.Drawing.Image imgPhoto, int flag)
{
EncoderParameters ep = new EncoderParameters();
long[] qy = new long[1];
qy[0] = flag;//设置压缩的比例1-100
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam; try
{
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
} MemoryStream ms = new MemoryStream();
if (jpegICIinfo != null)
{ imgPhoto.Save(ms, jpegICIinfo, ep);//dFile是压缩后的新路径
}
else
{
imgPhoto.Save(ms, imgPhoto.RawFormat);
} System.Drawing.Image zipImage = System.Drawing.Image.FromStream(ms);
return zipImage;
}
catch
{
return null;
}
}
}
}
public class WaterTRBL
{
public WaterTRBL()
{ }
int? top = null, right = null, bottom = null, left = null;
public WaterTRBL(int? top, int? right, int? bottom, int? left)
{
this.top = top;
this.right = right;
this.bottom = bottom;
this.left = left;
} public int? Top
{
get { return top; }
set { top = value; }
}
public int? Right
{
get { return right; }
set { right = value; }
} public int? Bottom
{
get { return bottom; }
set { bottom = value; }
} public int? Left
{
get { return left; }
set { left = value; }
}
}
我想问下我该使用哪种模式,最起码图片质量要和原图一样的?
InterpolationMode.Default;???
InterpolationMode.Bicubic??