private
void
GetThumbnailImage(
string
originalPath,
string
destPath,
int
thumbnailWidth,
int
thumbnailHeight,
string
mode,
int
left,
int
top)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalPath);
int originalWidth = originalImage.Width;
int originalHeight = originalImage.Height;
switch (mode)
{
case " WH " : // 指定宽高缩放(可能变形)
break ;
case " W " : // 指定宽,高按比例
thumbnailHeight = Convert.ToInt32(originalHeight * (( double )thumbnailWidth / originalWidth));
break ;
case " H " : // 指定高,宽按比例
thumbnailWidth = Convert.ToInt32(originalWidth * (( double )thumbnailHeight / originalHeight));
break ;
case " Cut " : // 指定宽高剪切,不变形
originalWidth = thumbnailWidth;
originalHeight = thumbnailHeight;
break ;
}
// 新建一个bmp图片
System.Drawing.Bitmap bitmapX = new Bitmap(thumbnailWidth, thumbnailHeight);
// 新建一个画板
System.Drawing.Graphics graphicsX = System.Drawing.Graphics.FromImage(bitmapX);
// 设置高质量插值法
graphicsX.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// 设置高质量,低速度呈现平滑程度
graphicsX.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// 清空画布并以透明背景色填充
graphicsX.Clear(System.Drawing.Color.Transparent);
// 在指定位置并且按指定大小绘制原图片的指定部分
graphicsX.DrawImage(originalImage, new System.Drawing.Rectangle( 0 , 0 , thumbnailWidth, thumbnailHeight), new System.Drawing.Rectangle(left, top, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel);
try
{
bitmapX.Save(destPath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
originalImage.Dispose();
bitmapX.Dispose();
graphicsX.Dispose();
}
}
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalPath);
int originalWidth = originalImage.Width;
int originalHeight = originalImage.Height;
switch (mode)
{
case " WH " : // 指定宽高缩放(可能变形)
break ;
case " W " : // 指定宽,高按比例
thumbnailHeight = Convert.ToInt32(originalHeight * (( double )thumbnailWidth / originalWidth));
break ;
case " H " : // 指定高,宽按比例
thumbnailWidth = Convert.ToInt32(originalWidth * (( double )thumbnailHeight / originalHeight));
break ;
case " Cut " : // 指定宽高剪切,不变形
originalWidth = thumbnailWidth;
originalHeight = thumbnailHeight;
break ;
}
// 新建一个bmp图片
System.Drawing.Bitmap bitmapX = new Bitmap(thumbnailWidth, thumbnailHeight);
// 新建一个画板
System.Drawing.Graphics graphicsX = System.Drawing.Graphics.FromImage(bitmapX);
// 设置高质量插值法
graphicsX.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
// 设置高质量,低速度呈现平滑程度
graphicsX.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
// 清空画布并以透明背景色填充
graphicsX.Clear(System.Drawing.Color.Transparent);
// 在指定位置并且按指定大小绘制原图片的指定部分
graphicsX.DrawImage(originalImage, new System.Drawing.Rectangle( 0 , 0 , thumbnailWidth, thumbnailHeight), new System.Drawing.Rectangle(left, top, originalWidth, originalHeight), System.Drawing.GraphicsUnit.Pixel);
try
{
bitmapX.Save(destPath,System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
originalImage.Dispose();
bitmapX.Dispose();
graphicsX.Dispose();
}
}
要得到Image不必保存到文件
uploadFile.PostedFile.SaveAs(path);
System.Drawing.Image imageX = System.Drawing.Image.FromFile(path);
只要这样
imageX = Image.FromStream(uploadFile.PostedFile.InputStream);
要得到Image同样不必保存到文件
bitmapX.Save(destPath, ImageFormat.Jpeg);
imageX = System.Drawing.Image.FromFile(destPath);
只要这样
imageX = (Image)bitmapX;
Bitmap是继承自Image的