我有 jpg 图像格式,大小为 3300K。
我尝试压缩图像=>所以我改变了图像大小,但我的文件仍然很大(〜800K)
改变尺寸方法代码:
internal static Image resizeImage( Image imgToResize )
{
Bitmap b = new Bitmap( 300, 300 );
using( Graphics g = Graphics.FromImage( ( Image )b ) )
{
//g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.InterpolationMode = InterpolationMode.Low;
g.DrawImage( imgToResize, 0, 0, 300, 300 );
imgToResize.Dispose();
}
return ( Image )b;
}
我必须压缩图像 - 即使图像的质量会低于原始图像。
我该怎么做 ?
毫克=图像
新闻大小 = 高度和宽度
使用以下代码调用 Resize 函数
Bitmap mg = new Bitmap(strUploadPath);
Size newSize = new Size(Convert.ToInt32(DispMaxWidth), Convert.ToInt32(DispMaxHeight));
Bitmap bp = ResizeImage(mg, newSize);
if (bp != null)
bp.Save(strUploadPath, System.Drawing.Imaging.ImageFormat.Jpeg);
private Bitmap ResizeImage(Bitmap mg, Size newSize)
{
double ratio = 0d;
double myThumbWidth = 0d;
double myThumbHeight = 0d;
int x = 0;
int y = 0;
Bitmap bp;
if ((mg.Width / Convert.ToDouble(newSize.Width)) > (mg.Height /
Convert.ToDouble(newSize.Height)))
ratio = Convert.ToDouble(mg.Width) / Convert.ToDouble(newSize.Width);
else
ratio = Convert.ToDouble(mg.Height) / Convert.ToDouble(newSize.Height);
myThumbHeight = Math.Ceiling(mg.Height / ratio);
myThumbWidth = Math.Ceiling(mg.Width / ratio);
//Size thumbSize = new Size((int)myThumbWidth, (int)myThumbHeight);
Size thumbSize = new Size((int)newSize.Width, (int)newSize.Height);
bp = new Bitmap(newSize.Width, newSize.Height);
x = (newSize.Width - thumbSize.Width) / 2;
y = (newSize.Height - thumbSize.Height);
// Had to add System.Drawing class in front of Graphics ---
System.Drawing.Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Rectangle rect = new Rectangle(x, y, thumbSize.Width, thumbSize.Height);
g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel);
return bp;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)