using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace Baolee.GeneralMethod
{
/// <summary>
/// ImageClass 的摘要說(shuō)明。
/// </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;
}
}
}
聯(lián)系客服