using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Baolee.GeneralMethod
{
/// <summary>
/// ImageClass 的摘要说明。
/// </summary>
public class ImageClass
{
// Fields
public string ErrMessage;
private int ImageHeight;
private int ImageWidth;
private Image ResourceImage;
/// <summary>
///
/// </summary>
/// <param name="ImageFileName"></param>
public ImageClass(string ImageFileName)
{
this.ResourceImage = Image.FromFile(ImageFileName);
this.ErrMessage = "";
}
/// <summary>
///
/// </summary>
/// <param name="Percent"></param>
/// <returns></returns>
public Image GetReducedImage(double Percent)
{
try
{
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.ThumbnailCallback);
this.ImageWidth = Convert.ToInt32((double) (this.ResourceImage.Width * Percent));
this.ImageHeight = Convert.ToInt32((double) (this.ResourceImage.Width * Percent));
return this.ResourceImage.GetThumbnailImage(this.ImageWidth, this.ImageHeight, callback, IntPtr.Zero);
}
catch (Exception exception)
{
this.ErrMessage = exception.Message;
return null;
}
}
/// <summary>
///
/// </summary>
/// <param name="Percent"></param>
/// <param name="targetFilePath"></param>
/// <returns></returns>
public bool GetReducedImage(double Percent, string targetFilePath)
{
try
{
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.ThumbnailCallback);
this.ImageWidth = Convert.ToInt32((double) (this.ResourceImage.Width * Percent));
this.ImageHeight = Convert.ToInt32((double) (this.ResourceImage.Width * Percent));
Image image = this.ResourceImage.GetThumbnailImage(this.ImageWidth, this.ImageHeight, callback, IntPtr.Zero);
image.Save(targetFilePath, ImageFormat.Jpeg);
image.Dispose();
return true;
}
catch (Exception exception)
{
this.ErrMessage = exception.Message;
return false;
}
}
/// <summary>
///
/// </summary>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <returns></returns>
public Image GetReducedImage(int Width, int Height)
{
try
{
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.ThumbnailCallback);
return this.ResourceImage.GetThumbnailImage(Width, Height, callback, IntPtr.Zero);
}
catch (Exception exception)
{
this.ErrMessage = exception.Message;
return null;
}
}
/// <summary>
///
/// </summary>
/// <param name="Width"></param>
/// <param name="Height"></param>
/// <param name="targetFilePath"></param>
/// <returns></returns>
public bool GetReducedImage(int Width, int Height, string targetFilePath)
{
try
{
Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(this.ThumbnailCallback);
Image image = this.ResourceImage.GetThumbnailImage(Width, Height, callback, IntPtr.Zero);
image.Save(targetFilePath);
image.Dispose();
return true;
}
catch (Exception exception)
{
this.ErrMessage = exception.Message;
return false;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool ThumbnailCallback()
{
return false;
}
}
}
本文介绍了一个图片压缩类ImageClass,该类提供了多种压缩图片的方法,包括按比例压缩并返回新的图片对象或保存到指定路径,以及直接指定宽度和高度进行压缩。
443

被折叠的 条评论
为什么被折叠?



