日期:2014-05-18 浏览次数:21252 次
public static Image GenThumbnail(Image imageFrom, int width, int height, int quality)
{
// 源图宽度及高度
int imageFromWidth = imageFrom.Width;
int imageFromHeight = imageFrom.Height;
// 生成的缩略图实际宽度及高度
if (width >= imageFromWidth && height >= imageFromHeight)
{
return imageFrom;
}
else
{
// 生成的缩略图在上述"画布"上的位置
int X = 0;
int Y = 0;
decimal wpercent = (decimal)width / imageFromWidth;
decimal hpercent = (decimal)height / imageFromHeight;
if (wpercent > hpercent)
{
width = (int)(imageFromWidth * hpercent);
}
else if (wpercent < hpercent)
{
height = (int)(imageFromHeight * wpercent);
}
// 创建画布
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bmp.SetResolution(imageFrom.HorizontalResolution, imageFrom.VerticalResolution);
using (Graphics g = Graphics.FromImage(bmp))
{
// 用白色清空
g.Clear(Color.White);
// 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
// 指定高质量、低速度呈现。
g.SmoothingMode = SmoothingMode.HighQuality;
// 在指定位置并且按指定大小绘制指定的 Image 的指定部分。
g.DrawImage(imageFrom, new Rectangle(X, Y, width, height), new Rectangle(0, 0, imageFromWidth, imageFromHeight), GraphicsUnit.Pixel);
return bmp;
}
}
}
------解决方案--------------------
试试锐化方案:
/// <summary>
/// 锐化
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="val">锐化程度。取值[0,1]。值越大锐化程度越高</param>
/// <returns>锐化后的图像</returns>
public static Bitmap KiSharpen(Bitmap b, float val)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
try
{
Bitmap bmpRtn = new Bitmap(w, h, PixelFormat.Format24bppRgb);
BitmapData srcData = b.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData dstData = bmpRtn.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
unsafe
{
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
int stride = srcData.Stride;
byte* p;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{