做一个按比例压缩图像的方法
来源:互联网 发布:软件著作权免税 编辑:程序博客网 时间:2024/04/29 08:56
此方法为网上抄袭而来,并非本人原创。
public static void ResizeImageFile(string pathFilename, byte[] imageFile, int targetSize)
...{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
...{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
...{
using (Graphics canvas = Graphics.FromImage(newImage))
...{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
if (newSize.Width >= 400)
...{
canvas.TextRenderingHint = TextRenderingHint.AntiAlias;
string imageCopyright = ConfigurationManager.AppSettings["ImageCopyright"].ToString();
Font font = new Font("Verdana", 10);
SolidBrush solidBrushBlack = new SolidBrush(Color.Black);
SolidBrush solidBrushWhite = new SolidBrush(Color.White);
//左上角
canvas.DrawString(imageCopyright, font, solidBrushBlack, 5, 5);
canvas.DrawString(imageCopyright, font, solidBrushWhite, 6, 6);
//右下角
SizeF sizeF = new SizeF();
sizeF = canvas.MeasureString(imageCopyright, font);
canvas.DrawString(imageCopyright, font, solidBrushWhite, newSize.Width - sizeF.Width - 5, newSize.Height - sizeF.Height);
canvas.DrawString(imageCopyright, font, solidBrushBlack, newSize.Width - sizeF.Width - 6, newSize.Height - sizeF.Height);
}
newImage.Save(pathFilename, ImageFormat.Jpeg);
}
}
}
}
private static Size CalculateDimensions(Size oldSize, int targetSize)
...{
Size newSize = new Size();
if (oldSize.Height > oldSize.Width)
...{
if (oldSize.Height < targetSize)
...{
newSize.Width = oldSize.Width;
newSize.Height = oldSize.Height;
}
else
...{
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height) * 0.75);
newSize.Height = (int)(targetSize * 0.75);
}
}
else
...{
if (oldSize.Width < targetSize)
...{
newSize.Width = oldSize.Width;
newSize.Height = oldSize.Height;
}
else
...{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
}
}
return newSize;
}
...{
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
...{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
...{
using (Graphics canvas = Graphics.FromImage(newImage))
...{
canvas.SmoothingMode = SmoothingMode.AntiAlias;
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
if (newSize.Width >= 400)
...{
canvas.TextRenderingHint = TextRenderingHint.AntiAlias;
string imageCopyright = ConfigurationManager.AppSettings["ImageCopyright"].ToString();
Font font = new Font("Verdana", 10);
SolidBrush solidBrushBlack = new SolidBrush(Color.Black);
SolidBrush solidBrushWhite = new SolidBrush(Color.White);
//左上角
canvas.DrawString(imageCopyright, font, solidBrushBlack, 5, 5);
canvas.DrawString(imageCopyright, font, solidBrushWhite, 6, 6);
//右下角
SizeF sizeF = new SizeF();
sizeF = canvas.MeasureString(imageCopyright, font);
canvas.DrawString(imageCopyright, font, solidBrushWhite, newSize.Width - sizeF.Width - 5, newSize.Height - sizeF.Height);
canvas.DrawString(imageCopyright, font, solidBrushBlack, newSize.Width - sizeF.Width - 6, newSize.Height - sizeF.Height);
}
newImage.Save(pathFilename, ImageFormat.Jpeg);
}
}
}
}
private static Size CalculateDimensions(Size oldSize, int targetSize)
...{
Size newSize = new Size();
if (oldSize.Height > oldSize.Width)
...{
if (oldSize.Height < targetSize)
...{
newSize.Width = oldSize.Width;
newSize.Height = oldSize.Height;
}
else
...{
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height) * 0.75);
newSize.Height = (int)(targetSize * 0.75);
}
}
else
...{
if (oldSize.Width < targetSize)
...{
newSize.Width = oldSize.Width;
newSize.Height = oldSize.Height;
}
else
...{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
}
}
return newSize;
}
- 做一个按比例压缩图像的方法
- 图片压缩按比例压缩
- 一个真实图像的JPEG2000压缩过程
- 常用图像压缩方法的对比
- 压缩JS的一个方法
- 常见图像压缩方法
- bitmap 图像压缩方法
- 上传图片按比例压缩
- 图片按比例压缩
- iOS中按比例剪切图片的方法
- Android Bitmap压缩图像的正确方法(compress的误区)
- Android Bitmap压缩图像的正确方法(compress的误区)
- 基于预测编码的图像压缩方法的研究
- 一个C++及Delphi做的压缩程序
- 图像压缩(SVD方法)
- CvSaveImage()保存jpeg时压缩图像的方法
- 图像压缩的奥秘
- php图片缩放代码-按比例缩放或截取指定大小的缩略图 非常好用的一个方法
- 集结号?
- 通过注册表更改winxp系统信息
- 微软(Microsoft)和雅虎(Yahoo)联姻会有什么结果?
- 雪灾词集:瘫痪,停止,疏散
- vb+access
- 做一个按比例压缩图像的方法
- Windows-自启动方式完全总结!
- AppLocale引起乱码之解决
- 全国计算机等级考试调整方案(07年5月发布)
- 类unix操作系统内核研究各阶段学习导引
- 2008年分销商八大发展趋势
- Linux内核研究推荐书目
- 我们期待自己成为一个优秀的软件模型设计者(转载)
- Using the DirectX Sample Framework