压缩图片C#算法
转载自 http://www.open-open.com/lib/view/open1391348644910.html
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System;
namespace Bll
{
/// <summary>
/// 图片处理类
/// 1、生成缩略图片或按照比例改变图片的大小和画质
/// 2、将生成的缩略图放到指定的目录下
/// </summary>
public class ImageHepler
{
public Image ResourceImage, ReducedImage;
private int ImageWidth;
private int ImageHeight;
public string ErrMessage; /// <summary>
/// 类的构造函数
/// </summary>
/// <param name="ImageFileName">图片文件的全路径名称</param>
public ImageHepler(string ImageFileName)
{
ResourceImage = Image.FromFile(ImageFileName);
ErrMessage = "";
} public bool ThumbnailCallback()
{
return false;
} /// <summary>
/// 生成缩略图,返回缩略图的Image对象
/// </summary>
/// <param name="Width">缩略图的宽度</param>
/// <param name="Height">缩略图的高度</param>
/// <returns>缩略图的Image对象</returns>
public Image GetReducedImage(int Width, int Height)
{
double LengthLong; //存储(长和宽中)较短的长度
int widthOK, heightOK; //存储实际要生成的图片的长宽
if (Width < Height) //判断输入的长和宽那个较短
{
LengthLong = Width; //把较短的存储在 LengthLonh 用于计算
}
else
{
LengthLong = Height;
}
try
{
//判断原图片 长和宽
//原图比较长的一个边要和缩略图的边相等
if (ResourceImage.Width > ResourceImage.Height)
{
widthOK = (int)LengthLong;
heightOK = (int)(LengthLong / ResourceImage.Width * ResourceImage.Height);
}
else
{
heightOK = (int)LengthLong;
widthOK = (int)LengthLong / ResourceImage.Height * ResourceImage.Width; }
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage = ResourceImage.GetThumbnailImage(widthOK, heightOK, callb, IntPtr.Zero);
return ReducedImage;
}
catch (Exception e)
{
ErrMessage = e.Message;
return null;
}
} /// <summary>
/// 生成缩略图,将缩略图文件保存到指定的路径
/// </summary>
/// <param name="Width">缩略图的宽度</param>
/// <param name="Height">缩略图的高度</param>
/// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>
/// <returns>成功返回true,否则返回false</returns>
public bool GetReducedImage(int Width, int Height, string targetFilePath)
{
double LengthLong; //存储(长和宽中)较短的长度
int widthOK, heightOK; //存储实际要生成的图片的长宽
if (Width < Height) //判断输入的长和宽那个较短
{
LengthLong = Width; //把较短的存储在 LengthLonh 用于计算
}
else
{
LengthLong = Height;
}
try
{
//判断原图片 长和宽
//原图比较长的一个边要和缩略图的边相等
if (ResourceImage.Width > ResourceImage.Height)
{
widthOK = (int)LengthLong;
heightOK = (int)(LengthLong / ResourceImage.Width * ResourceImage.Height);
}
else
{
heightOK = (int)LengthLong;
widthOK = (int)LengthLong / ResourceImage.Height * ResourceImage.Width;
}
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ReducedImage = ResourceImage.GetThumbnailImage(widthOK, heightOK, callb, IntPtr.Zero);
ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
//ReducedImage.Dispose();
return true;
}
catch (Exception e)
{
ErrMessage = e.Message;
return false;
}
} /// <summary>
/// 生成缩略图,返回缩略图的Image对象
/// </summary>
/// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>
/// <returns>缩略图的Image对象</returns>
public Image GetReducedImage(double Percent)
{
try
{
Image ReducedImage;
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent);
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
return ReducedImage;
}
catch (Exception e)
{
ErrMessage = e.Message;
return null;
}
} /// <summary>
/// 生成缩略图,返回缩略图的Image对象
/// </summary>
/// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>
/// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:\Images\filename.jpg</param>
/// <returns>成功返回true,否则返回false</returns>
public bool GetReducedImage(double Percent, string targetFilePath)
{
try
{
Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);
ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent);
ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
//ReducedImage.Dispose();
return true;
}
catch (Exception e)
{
ErrMessage = e.Message;
return false;
}
}
}
}
压缩图片C#算法的更多相关文章
- Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image获取图像像素点image.getRGB(i, lineIndex); 图片剪辑/AtiPlatf_cms/src/com/attilax/img/imgx.javacutImage图片处理titit 判断判断一张图片是否包含另一张小图片 atitit 图片去噪算法的原理与
Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image 获取图像像素点 image.getRGB(i, lineIndex); ...
- 压缩感知重构算法之IRLS算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之OLS算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之CoSaMP算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之IHT算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之SP算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- 压缩感知重构算法之OMP算法python实现
压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...
- JVM探究 面试题 JVM的位置 三种JVM:HotSpot 新生区 Young/ New 养老区 Old 永久区 Perm 堆内存调优GC的算法有哪些?标记清除法,标记压缩,复制算法,引用计数法
JVM探究 面试题: 请你弹弹你对JVM的理解?Java8虚拟机和之前的变化更新? 什么是OOM?什么是栈溢出StackOverFlowError?怎么分析 JVM的常用调优参数有哪些? 内存快照如何 ...
- iOS学习-压缩图片(改变图片的宽高)
压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...
随机推荐
- jquery中$each()
$.each():可用于遍历任何的集合(无论是数组或对象) $(selector).each():专用于jquery对象的遍历, 如果是数组,回调函数每次传入数组的索引和对应的值(值亦可以通过this ...
- Oracle PL/SQL开发基础(第三十三弹:EXCEPTION_INIT)
如果有一些异常并没有异常名称,比如一些ORA-开头的异常并没有一个友好的预定义的异常定义,此时在WHEN子句中无法使用具体的异常名称,必须要使用OTHERS异常处理器进行捕捉.通过EXCEPTION_ ...
- Django中ORM之查询表记录
查询相关API from django.db import models # Create your models here. class Book(models.Model): title = mo ...
- 关于<marquee>、<form>、input中的<text>、<password>、<hidden>、<wenbenkuang>、<reset>、<image>、<submit>、<radio>、<checkbox>以及<select><iframe src>的用法
<html> <head> <meta charset="UTF-8"> <title></ ...
- Codeforces Round #493 (Div. 1) B. Roman Digits 打表找规律
题意: 我们在研究罗马数字.罗马数字只有4个字符,I,V,X,L分别代表1,5,10,100.一个罗马数字的值为该数字包含的字符代表数字的和,而与字符的顺序无关.例如XXXV=35,IXI=12. 现 ...
- 【BZOJ1396】识别子串 - 后缀自动机+线段树
题意: Description Input 一行,一个由小写字母组成的字符串S,长度不超过10^5 Output L行,每行一个整数,第i行的数据表示关于S的第i个元素的最短识别子串有多长. 题解: ...
- BZOJ 1016 最小生成树计数(矩阵树定理)
我们把边从小到大排序,然后依次插入一种权值的边,然后把每一个联通块合并. 然后当一次插入的边不止一条时做矩阵树定理就行了.算出有多少种生成树就行了. 剩下的交给乘法原理. 实现一不小心就会让程序变得很 ...
- code-reading-notes--libyang-1
API struct lyd_node * lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options, ...) ...
- NOIP2018提高组金牌训练营——搜索专题
NOIP2018提高组金牌训练营——搜索专题 1416 两点 福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不 ...
- Golang-import-introduce
本文主要讲解golang中import关键字的用法 import( "fmt" ) //然后在代码里面可以通过如下的方式调用 fmt.Println("hello wor ...