生成缩略图 and 压缩
来源:互联网 发布:微信怎么发布淘宝链接 编辑:程序博客网 时间:2024/04/30 13:02
//生成缩略图函数
//顺序参数:源图文件流、缩略图存放地址、模版宽、模版高
//注:缩略图大小控制在模版区域内
public static void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight)
{
//从文件取得图片对象,并使用流中嵌入的颜色管理信息
System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream, true);
//缩略图宽、高
System.Double newWidth = myImage.Width, newHeight = myImage.Height;
//宽大于模版的横图
if (myImage.Width > myImage.Height || myImage.Width == myImage.Height)
{
if (myImage.Width > templateWidth)
{
//宽按模版,高按比例缩放
newWidth = templateWidth;
newHeight = myImage.Height * (newWidth / myImage.Width);
}
}
//高大于模版的竖图
else
{
if (myImage.Height > templateHeight)
{
//高按模版,宽按比例缩放
newHeight = templateHeight;
newWidth = myImage.Width * (newHeight / myImage.Height);
}
}
//取得图片大小
System.Drawing.Size mySize = new Size((int)newWidth, (int)newHeight);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width, mySize.Height);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.White);
//在指定位置画图
g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
System.Drawing.GraphicsUnit.Pixel);
///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋体",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();
///图片水印
//System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a = Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存缩略图
bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
myImage.Dispose();
bitmap.Dispose();
}
复制代码 代码如下:
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "选择图片文件";
// fileDialog.Filter = "excel files (*.xls)|*.xls";
fileDialog.FilterIndex = 1;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
System.IO.FileStream file =System.IO.File.Open(fileDialog.FileName,System.IO.FileMode.Open);
System.IO.Stream strea = file;
file.Close();
MakeSmallImg(strea, "缩略图.jpg", 150, 150);
// file.Close();
}
}
详细出处参考:http://www.jb51.net/article/22515.htm
- 生成缩略图 and 压缩
- 图像压缩、生成缩略图类
- .NET图片压缩和生成缩略图
- 使用epeg生成无压缩的缩略图
- android 图片压缩 生成缩略图方法
- Android图片质量压缩以及缩略图生成
- C#图片切割、图片压缩、缩略图生成
- Android图片质量压缩以及缩略图生成
- Android 图片压缩也即生成缩略图方法
- 用于图片切割,图片压缩,缩略图的生成
- C#中图片切割,图片压缩,缩略图的生成
- C#中图片切割,图片压缩,缩略图的生成
- Android 图片压缩也即生成缩略图方法
- Android 图片压缩也即生成缩略图方法
- c#生成图片缩略图或按比例压缩图片
- Thumbnailator java图片压缩,加水印,批量生成缩略图
- C#中图片切割,图片压缩,缩略图的生成
- Android 图片压缩也即生成缩略图方法
- 怎么查看linux库是使用哪个版本的编译器编译的
- Matlab的for循环优化
- 最好的flex学习网站,各种控件元素详解,含源码、在线查看事例效
- ubuntun 下面lamp 环境的搭建
- git commit 取消
- 生成缩略图 and 压缩
- 触发器使用教程和命名规范
- The terminal server has exceeded the maximum number of allowed clients
- Hadoop技术内幕:深入解析MapReduce架构设计与实现原理
- Android开发---从零搞起2
- 关于工作与生活的转帖
- 返回函数指针的指针函数
- COM中使用的数组-SafeArray
- ICE Manual(Documentation for Ice 3.5)---Connection Management(Connection Establishment)