private void text_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="strOraginalPath"></param>
///
public void GetThumb(string strOraginalPath)
{
int _iThumbMaxWidth = 400;//缩略图的最大宽度
int _iThumbMaxHeight = 300;//缩略图的最大高度
System.Drawing.Image imgOraginal = System.Drawing.Image.FromFile(strOraginalPath);
int iOraginalWidth = imgOraginal.Width;
int iOraginalHeight = imgOraginal.Height;
int toWidth;
int toHeight;
//原图的宽高比
float fWbH = (float)iOraginalWidth / (float)iOraginalHeight;
//原图的高宽比
float fHbW = (float)iOraginalHeight / (float)iOraginalWidth;
//如果宽高比大于4:3,则按照最大宽度来生成缩略图
if (fWbH >= (4.0f / 3.0f))
{
if (_iThumbMaxWidth < iOraginalWidth)
{
toWidth = _iThumbMaxWidth;
toHeight = (int)(toWidth * fHbW);
}
else//如果指定的缩略图宽度大于原图的宽度,则不生成缩略图
{
toWidth = iOraginalWidth;
toHeight = iOraginalHeight;
}
}
else//如果宽高比小于4:3,则按照最大高度来生成缩略图
{
if (_iThumbMaxWidth < iOraginalWidth)
{
toHeight = _iThumbMaxHeight;
toWidth = (int)(toHeight * fWbH);
}
else//如果指定的缩略图高度小于原图的宽度,则不生成缩略图
{
toWidth = iOraginalWidth;
toHeight = iOraginalHeight;
}
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(toWidth, toHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设定缩略图的生成质量
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(System.Drawing.Color.Transparent);
g.DrawImage(imgOraginal, new System.Drawing.Rectangle(0, 0, toWidth, toHeight), new System.Drawing.Rectangle(0, 0, iOraginalWidth, iOraginalHeight), System.Drawing.GraphicsUnit.Pixel);
//string thumbServerPath = HttpContext.Current.Server.MapPath("缩略图路径");
string thumbServerPath = @"VideosSL\";
if (!Directory.Exists(thumbServerPath))
{
Directory.CreateDirectory(thumbServerPath);
}
try
{
bitmap.Save(thumbServerPath + "高达1(缩略图名称).jpg", System.Drawing.Imaging.ImageFormat.Jpeg); }
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
imgOraginal.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
string strOraginalPath = @"Pictures\1.jpg";
GetThumb(strOraginalPath);
}