62,046
社区成员
发帖
与我相关
我的任务
分享
public interface IThumbMode //计算缩略图接口
{
Image _originaImage { get; set; } //原始图片
int width { get; } //缩略图的高
int height { get; } //缩略图的宽
void makeWH(); //计算缩略图的宽高
}
public class imgTools
{
public static Image makeThumbImg(IThumbMode mode)//类似抽象工厂,按情况也可以考虑模板或策略模式
{
var oimg = mode._originaImage;
var res = oimg.GetThumbnailImage(mode.width, mode.height, null, new IntPtr());
return res;
}
}
public class ThumbBySize:IThumbMode //按文件size缩略
{
private long filesize;
public LockSize(string path,long filesize)
{
_originaImage = Image.FromFile(path);
this.filesize = filesize;
makeWH();
}
#region IThumbMode 成员
public Image _originaImage
{
get;
set;
}
private int m_width;
public int width
{
get { return m_width; }
}
private int m_height;
public int height
{
get { return m_height; }
}
public void makeWH()
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
_originaImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
var osize = ms.Length;
if (osize <= filesize)
return;
else
{
m_width = Convert.ToInt32(_originaImage.Width * filesize / osize);
m_height = Convert.ToInt32(_originaImage.Height * filesize / osize);
}
}
#endregion