公司做了个图片网站,但是我用了XInfo.Common.Tools.MyImage组件却发现图片缩放后严重的丢真
[@more@]
徐哥也很不满意,强烈要求我必须解决这个问题 所以我就在网站查了很多东东,不过好象都不好用
最后我找到了一个不丢真的缩放代码,但不是按照比例缩放的,我又找了个一个比例缩放的代码
两者结合一下,从新改了函数,居然成功了,哈哈 我突然发现我太天才了
我把代码发上来吧 以便网友们使用 嘿嘿
//使用方法调用GenerateHighThumbnail()方法即可
//参数oldImagePath表示要被缩放的图片路径
//参数newImagePath表示缩放后保存的图片路径
//参数width和height分别是缩放范围宽和高
public static void GenerateHighThumbnail(string oldImagePath, string newImagePath, int width, int height)
{
System.Drawing.Image oldImage = System.Drawing.Image.FromFile(oldImagePath);
int newWidth = AdjustSize(width, height, oldImage.Width, oldImage.Height).Width;
int newHeight =AdjustSize(width, height, oldImage.Width, oldImage.Height).Height;
{
System.Drawing.Image oldImage = System.Drawing.Image.FromFile(oldImagePath);
int newWidth = AdjustSize(width, height, oldImage.Width, oldImage.Height).Width;
int newHeight =AdjustSize(width, height, oldImage.Width, oldImage.Height).Height;
//。。。。。。。。。。。
System.Drawing.Image thumbnailImage = oldImage.GetThumbnailImage(newWidth, newHeight,new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(thumbnailImage);
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(thumbnailImage);
//处理JPG质量的函数
System.Drawing.Imaging.ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
if (ici != null)
{
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
bm.Save(newImagePath, ici, ep);
System.Drawing.Imaging.ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
if (ici != null)
{
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(1);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);
bm.Save(newImagePath, ici, ep);
//释放所有资源,不释放,可能会出错误。
ep.Dispose();
ep = null;
}
ici = null;
ep.Dispose();
ep = null;
}
ici = null;
bm.Dispose();
bm = null;
bm = null;
thumbnailImage.Dispose();
thumbnailImage = null;
oldImage.Dispose();
oldImage = null;
}
thumbnailImage = null;
oldImage.Dispose();
oldImage = null;
}
private static bool ThumbnailCallback()
{
return false;
}
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;
}
public struct PicSize
{
public int Width;
public int Height;
}
public static PicSize AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
{
PicSize size = new PicSize();
// 原始宽高在指定宽高范围内,不作任何处理
if (orgWidth <= spcWidth && orgHeight <= spcHeight)
{
size.Width = orgWidth;
size.Height = orgHeight;
}
else
{
// 取得比例系数
float w = orgWidth / (float)spcWidth;
float h = orgHeight / (float)spcHeight;
// 宽度比大于高度比
if (w > h)
{
size.Width = spcWidth;
size.Height = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
}
// 宽度比小于高度比
else if (w < h)
{
size.Height = spcHeight;
size.Width = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
}
// 宽度比等于高度比
else
{
size.Width = spcWidth;
size.Height = spcHeight;
}
}
return size;
}
{
return false;
}
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;
}
public struct PicSize
{
public int Width;
public int Height;
}
public static PicSize AdjustSize(int spcWidth, int spcHeight, int orgWidth, int orgHeight)
{
PicSize size = new PicSize();
// 原始宽高在指定宽高范围内,不作任何处理
if (orgWidth <= spcWidth && orgHeight <= spcHeight)
{
size.Width = orgWidth;
size.Height = orgHeight;
}
else
{
// 取得比例系数
float w = orgWidth / (float)spcWidth;
float h = orgHeight / (float)spcHeight;
// 宽度比大于高度比
if (w > h)
{
size.Width = spcWidth;
size.Height = (int)(w >= 1 ? Math.Round(orgHeight / w) : Math.Round(orgHeight * w));
}
// 宽度比小于高度比
else if (w < h)
{
size.Height = spcHeight;
size.Width = (int)(h >= 1 ? Math.Round(orgWidth / h) : Math.Round(orgWidth * h));
}
// 宽度比等于高度比
else
{
size.Width = spcWidth;
size.Height = spcHeight;
}
}
return size;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/631872/viewspace-1049782/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/631872/viewspace-1049782/