转载自 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#算法的更多相关文章

  1. 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); ...

  2. 压缩感知重构算法之IRLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  3. 压缩感知重构算法之OLS算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  4. 压缩感知重构算法之CoSaMP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  5. 压缩感知重构算法之IHT算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  6. 压缩感知重构算法之SP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  7. 压缩感知重构算法之OMP算法python实现

    压缩感知重构算法之OMP算法python实现 压缩感知重构算法之CoSaMP算法python实现 压缩感知重构算法之SP算法python实现 压缩感知重构算法之IHT算法python实现 压缩感知重构 ...

  8. JVM探究 面试题 JVM的位置 三种JVM:HotSpot 新生区 Young/ New 养老区 Old 永久区 Perm 堆内存调优GC的算法有哪些?标记清除法,标记压缩,复制算法,引用计数法

    JVM探究 面试题: 请你弹弹你对JVM的理解?Java8虚拟机和之前的变化更新? 什么是OOM?什么是栈溢出StackOverFlowError?怎么分析 JVM的常用调优参数有哪些? 内存快照如何 ...

  9. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

随机推荐

  1. MS SQL 获取数据字典的经典sql语句

    select [表名]=c.Name, [表说明]=isnull(f.[value],''), [列名]=a.Name, [列序号]=a.Column_id, [标识]=case when is_id ...

  2. JavaScript中闭包的理解

    1.什么是闭包 我个人理解闭包就是函数中嵌套函数,但是嵌套的那个函数必须是返回值,才构成闭包: <!DOCTYPE html> <html> <head> < ...

  3. DOM基础知识(Node对象、Element对象)

    5.Node对象 u  遍历节点 u 父节点 .parentNode - 获取父节点—> 元素节点或文档节点 .parentElement - 获取父元素节点—> 元素节点 u    子节 ...

  4. 使用DWR实现JS调用服务端Java代码

    DWR简介 DWR全称Direct Web Remoting,是一款非常优秀的远程过程调用(Remote Procedure Call)框架,通过浏览器提供的Ajax引擎实现在前端页面的JS代码中调用 ...

  5. C语言-实现整数倒序输出

    Action() { //实现一个3位数的倒序输出(123输出321) int n; int m=321; n=fun_mod(m,n); lr_output_message("%d&quo ...

  6. jsp+jdbc实现用户登录

    1.1 创建数据库表 表名:user 字段: userid   保存用户的登录id name     用户名 password 密码 1.2 实现思路 a. 用户登录,则需要有个一个表单页,此页面可输 ...

  7. Python测试(二)

    # 1.计算1-300之间所有能被3和7整除的所有数之和# num = 0# for i in range(1,300):# if i%3 ==0 and i%7 ==0:# num += i# pr ...

  8. js原生api之String的slice方法

    我们在工作中可能会很少进行这样的思考,对于一些常用的原生api它是如何实现的呢,如果让我们去用js实现一个与原生api功能相同的函数我们该如何设计算法去实现呢? 为了巩固自己的编程技术和提高自己的编程 ...

  9. Linux下实时查看GPU状况

    1. 显示当前GPU使用情况 Nvidia自带了一个nvidia-smi的命令行工具,会显示显存使用情况: $ nvidia-smi 输出如下: 2. 周期性输出GPU使用情况 但是有时我们希望不仅知 ...

  10. V4L2驱动程序架构

    1 V4L2简介 video4linux2(V4L2)是Linux内核中关于视频设备的内核驱动,它为Linux中视频设备访问提供了通用接口,在Linux系统中,V4L2驱动的Video设备节点路径通常 ...