/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbLocal(string originalImagePath, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { try { Image originalImage = Image.FromFile(originalImagePath); MakeThumbnail(originalImage, thumbnailPath, width, height, mode); } catch(Exception ex) { Logger.CurrentLog.Error("",ex); return; } }
public static bool Create(ImageThumbnailMode mode, Image image, Size size, ImageFormat imageFormat, System.IO.Stream output) { try { Size realSize = size; if ((mode & ImageThumbnailMode.SaveProportion) == ImageThumbnailMode.SaveProportion) { // 2007-04-24 by Oleg Rylin: the old code worked only for 1:1 size proportions realSize.Width = (int)(1.0 * image.Width * size.Height / image.Height); if (realSize.Width > size.Width) { realSize.Width = size.Width; realSize.Height = (int)(1.0 * image.Height * size.Width / image.Width); } /* double prop = image.Width / (1.0 * image.Height); * * realSize.Width = prop>1?size.Width:(int)(size.Width*prop); * realSize.Height = prop>1?(int)(size.Height/prop):size.Height; * */ } if ((mode & ImageThumbnailMode.SkipSmallImage) == ImageThumbnailMode.SkipSmallImage) { if (image.Width < size.Width && image.Height < size.Height) { realSize.Width = image.Width; realSize.Height = image.Height; } } Image thumbnailImage = image.GetThumbnailImage(realSize.Width, realSize.Height, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { thumbnailImage.Save(stream, imageFormat); stream.Position = 0; output.Write(stream.GetBuffer(), 0, (int)stream.Length); } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); return(false); } return(true); }
public static bool Create(ImageThumbnailMode mode, Image image, Size size, ImageFormat imageFormat, System.IO.Stream output) { try { Size realSize = size; if((mode & ImageThumbnailMode.SaveProportion)==ImageThumbnailMode.SaveProportion) { // 2007-04-24 by Oleg Rylin: the old code worked only for 1:1 size proportions realSize.Width = (int)(1.0 * image.Width * size.Height / image.Height); if (realSize.Width > size.Width) { realSize.Width = size.Width; realSize.Height = (int)(1.0 * image.Height * size.Width / image.Width); } /* double prop = image.Width / (1.0 * image.Height); realSize.Width = prop>1?size.Width:(int)(size.Width*prop); realSize.Height = prop>1?(int)(size.Height/prop):size.Height; * */ } if((mode & ImageThumbnailMode.SkipSmallImage)==ImageThumbnailMode.SkipSmallImage ) { if(image.Width<size.Width && image.Height< size.Height) { realSize.Width = image.Width; realSize.Height = image.Height; } } Image thumbnailImage = image.GetThumbnailImage(realSize.Width, realSize.Height, new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); using(System.IO.MemoryStream stream = new System.IO.MemoryStream()) { thumbnailImage.Save(stream, imageFormat); stream.Position = 0; output.Write(stream.GetBuffer(), 0, (int)stream.Length); } } catch(Exception ex) { System.Diagnostics.Trace.WriteLine(ex); return false; } return true; }
public static bool Create(ImageThumbnailMode mode, string ContainerKey, int FileId, Size size, ImageFormat imageFormat, System.IO.Stream output) { try { BaseIbnContainer bic = BaseIbnContainer.Create("FileLibrary", ContainerKey); FileStorage fs = (FileStorage)bic.LoadControl("FileStorage"); using(System.IO.MemoryStream stream = new System.IO.MemoryStream()) { fs.LoadFile(FileId,stream); Image image = Image.FromStream(stream); return Create(mode, image, size, imageFormat, output); } } catch(Exception ex) { System.Diagnostics.Trace.WriteLine(ex); return false; } }
public static bool Create(ImageThumbnailMode mode, string ContainerKey, int FileId, Size size, ImageFormat imageFormat, System.IO.Stream output) { try { BaseIbnContainer bic = BaseIbnContainer.Create("FileLibrary", ContainerKey); FileStorage fs = (FileStorage)bic.LoadControl("FileStorage"); using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { fs.LoadFile(FileId, stream); Image image = Image.FromStream(stream); return(Create(mode, image, size, imageFormat, output)); } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex); return(false); } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(Image originalImage, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case ImageThumbnailMode.HW: //指定高宽缩放(可能变形) break; case ImageThumbnailMode.W: //指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case ImageThumbnailMode.H: //指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case ImageThumbnailMode.Cut: //指定高宽裁减 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = originalImage.Width - ow; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh); } break; case ImageThumbnailMode.CutA: //指定高宽裁减(不变形)自定义 if (ow <= towidth && oh <= toheight) { x = -(towidth - ow) / 2; y = -(toheight - oh) / 2; ow = towidth; oh = toheight; } else { if (ow > oh) //宽大于高 { x = 0; y = -(ow - oh) / 2; oh = ow; } else //高大于宽 { y = 0; x = -(oh - ow) / 2; ow = oh; } } break; default: break; } //新建一个bmp图片 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbLocal(string originalImagePath, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { try { Image originalImage = Image.FromFile(originalImagePath); MakeThumbnail(originalImage, thumbnailPath, width, height, mode); } catch (Exception ex) { Logger.CurrentLog.Error("", ex); return; } }
public static void MakeThumbWeb(string HttpSrc, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { try { WebRequest webRequest = WebRequest.Create(HttpSrc); WebResponse webResponse = webRequest.GetResponse(); Image originalImage = Image.FromStream(webResponse.GetResponseStream()); MakeThumbnail(originalImage, thumbnailPath, width, height, mode); } catch (Exception ex) { Logger.CurrentLog.Error("", ex); return; } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(Image originalImage, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case ImageThumbnailMode.HW://指定高宽缩放(可能变形) break; case ImageThumbnailMode.W://指定宽,高按比例 toheight = originalImage.Height * width / originalImage.Width; break; case ImageThumbnailMode.H://指定高,宽按比例 towidth = originalImage.Width * height / originalImage.Height; break; case ImageThumbnailMode.Cut://指定高宽裁减 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = originalImage.Width - ow; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh); } break; case ImageThumbnailMode.CutA://指定高宽裁减(不变形)自定义 if (ow <= towidth && oh <= toheight) { x = -(towidth - ow) / 2; y = -(toheight - oh) / 2; ow = towidth; oh = toheight; } else { if (ow > oh)//宽大于高 { x = 0; y = -(ow - oh) / 2; oh = ow; } else//高大于宽 { y = 0; x = -(oh - ow) / 2; ow = oh; } } break; default: break; } //新建一个bmp图片 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbLocal(string originalImagePath, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { Image originalImage = Image.FromFile(originalImagePath); MakeThumbnail(originalImage, thumbnailPath, width, height, mode); }
public static void MakeThumbWeb(string HttpSrc, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { WebRequest webRequest = WebRequest.Create(HttpSrc); WebResponse webResponse = webRequest.GetResponse(); Image originalImage = Image.FromStream(webResponse.GetResponseStream()); MakeThumbnail(originalImage, thumbnailPath, width, height, mode); }
/// <summary> /// 生成缩略图 /// </summary> public static MemoryStream MakeThumbnail(Stream originalimage, int width, int height, ImageThumbnailMode mode) { return(MakeThumbnail(originalimage, new Point(0, 0), width, height, mode)); }
/// <summary> /// 图像处理 /// </summary> private static MemoryStream MakeThumbnail(Stream originalimage, Point point, int width, int height, ImageThumbnailMode mode) { using (Image originalImage = Image.FromStream(originalimage)) { int destrect_width, destrect_height, srcrect_width, srcrect_height, x, y; switch (mode) { case ImageThumbnailMode.FixedWidthAndHeight: //指定高宽缩放(可能变形) destrect_width = width; destrect_height = height; srcrect_width = originalImage.Width; srcrect_height = originalImage.Height; x = y = 0; break; case ImageThumbnailMode.FixedWidth: //指定宽,高按比例 destrect_width = width; destrect_height = originalImage.Height * width / originalImage.Width; srcrect_width = originalImage.Width; srcrect_height = originalImage.Height; x = y = 0; break; case ImageThumbnailMode.FixedHeight: //指定高,宽按比例 destrect_width = originalImage.Width * height / originalImage.Height; destrect_height = height; srcrect_width = originalImage.Width; srcrect_height = originalImage.Height; x = y = 0; break; case ImageThumbnailMode.Cut: //指定高宽裁减(不变比例) destrect_width = width; destrect_height = height; if ((double)originalImage.Width / (double)originalImage.Height > (double)destrect_width / (double)destrect_height) { srcrect_width = originalImage.Height; srcrect_height = originalImage.Height * destrect_width / destrect_height; y = 0; x = (originalImage.Width - srcrect_width) / 2; } else { srcrect_width = originalImage.Width; srcrect_height = originalImage.Width * height / destrect_width; x = 0; y = (originalImage.Height - srcrect_height) / 2; } break; case ImageThumbnailMode.CustomCut: //裁减 destrect_width = originalImage.Width; destrect_height = originalImage.Height; srcrect_width = width; srcrect_height = height; x = point.X; y = point.Y; break; case ImageThumbnailMode.AutoFit: //自动缩放 if (originalImage.Height > originalImage.Width) { destrect_width = originalImage.Width * height / originalImage.Height; destrect_height = height; } else { destrect_width = width; destrect_height = originalImage.Height * width / originalImage.Width; } srcrect_width = originalImage.Width; srcrect_height = originalImage.Height; x = y = 0; break; default: throw new ArgumentException("不正确的生成缩略图方式", "mode"); } var destRect = new Rectangle(0, 0, destrect_width, destrect_height); var srcRect = new Rectangle(x, y, srcrect_width, srcrect_height); //新建一个bmp图片 using (Image bitmap = new Bitmap(destRect.Width, destRect.Height)) { //新建一个画板 using (Graphics g = Graphics.FromImage(bitmap)) { //设置高质量插值法 g.InterpolationMode = InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, destRect, srcRect, GraphicsUnit.Pixel); //输出 MemoryStream output = new MemoryStream(); try { bitmap.Save(output, originalImage.RawFormat); output.Seek(0, SeekOrigin.Begin);//uploadstream.Position = 0; return(output); } catch { output.Dispose(); throw; } } } } }
/// <summary> /// 生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理路径)</param> /// <param name="width">缩略图宽度</param> /// <param name="height">缩略图高度</param> /// <param name="mode">生成缩略图的方式</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ImageThumbnailMode mode) { Image originalImage = Image.FromFile(originalImagePath); int towidth = width; int toheight = height; int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; switch (mode) { case ImageThumbnailMode.HW: break; case ImageThumbnailMode.W: toheight = originalImage.Height * width / originalImage.Width; break; case ImageThumbnailMode.H: towidth = originalImage.Width * height / originalImage.Height; break; case ImageThumbnailMode.Cut: if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * height / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } //新建一个bmp图片 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充 g.Clear(Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }