图片操作:生成缩略图、添加水印、截取图片等

来源:互联网 发布:linux入门教程视频 编辑:程序博客网 时间:2024/05/01 14:55
  1. using System;  
  2. using System.Drawing;  
  3. using System.Drawing.Imaging;  
  4. using System.IO;  
  5. using System.Web;  
  6.   
  7. namespace XXXX.Common  
  8. {  
  9.     /// <summary>  
  10.     /// 图片操作:生成缩略图、添加水印、截取图片等  
  11.     /// </summary>  
  12.     public class ImagesHelper  
  13.     {  
  14.   
  15.          
  16.   
  17.         /// <summary>  
  18.         /// 根据文件流获得图片宽度  
  19.         /// </summary>  
  20.         /// <param name="file"></param>  
  21.         /// <returns></returns>  
  22.         public static int getImgWidth(Stream stream)  
  23.         {             
  24.             Image img = Image.FromStream(stream);  
  25.             int result = img.Width;  
  26.             img.Dispose();  
  27.             stream.Dispose();  
  28.             return result;  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// 根据图片路径获得图片宽度  
  33.         /// </summary>  
  34.         /// <param name="filePath"></param>  
  35.         /// <returns></returns>  
  36.         public static int getImgWidth(string filePath)  
  37.         {              
  38.             Image img = Image.FromFile(filePath);  
  39.             int result = img.Width;  
  40.             img.Dispose();  
  41.             return result;  
  42.         }  
  43.         
  44.  
  45.         #region 从文件流生成缩略图  
  46.         /// <summary>  
  47.         /// 从文件流生成缩略图  
  48.         /// </summary>  
  49.         /// <param name="stream">数据IO流</param>  
  50.         /// <param name="savePath"></param>  
  51.         /// <param name="width"></param>  
  52.         /// <param name="height"></param>  
  53.         /// <param name="scale"></param>  
  54.         /// <returns></returns>  
  55.         public static bool GetThumbNail(Stream stream, string savePath, int width, int height, ThumbNailScale scale)  
  56.         {  
  57.             //缩略图  
  58.             Image img = Image.FromStream(stream);  
  59.             stream.Dispose();  
  60.             int towidth = width;  
  61.             int toheight = height;  
  62.             int x = 0;  
  63.             int y = 0;  
  64.             int ow = img.Width;  
  65.             int oh = img.Height;  
  66.             //如果图片小于指定宽度  
  67.             if (ow < width)  
  68.                 width = ow;  
  69.   
  70.             if (oh < height)  
  71.                 height = oh;  
  72.   
  73.   
  74.             switch (scale)  
  75.             {  
  76.                 case ThumbNailScale.Appointed:  
  77.                     break;  
  78.                 case ThumbNailScale.ScaleWidth:  
  79.                     toheight = img.Height * width / img.Width;  
  80.                     break;  
  81.                 case ThumbNailScale.ScaleHeight:  
  82.                     towidth = img.Width * height / img.Height;  
  83.                     break;  
  84.                 case ThumbNailScale.Cut:  
  85.                     if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)  
  86.                     {  
  87.                         oh = img.Height;  
  88.                         ow = img.Height * towidth / toheight;  
  89.                         y = 0;  
  90.                         x = (img.Width - ow) / 2;  
  91.                     }  
  92.                     else  
  93.                     {  
  94.                         ow = img.Width;  
  95.                         oh = img.Width * height / towidth;  
  96.                         x = 0;  
  97.                         y = (img.Height - oh) / 2;  
  98.                     }  
  99.                     break;  
  100.                 case ThumbNailScale.ScaleDown:  
  101.                     double Tw, Th;  
  102.                     Tw = width;  
  103.                     Th = height * (Convert.ToDouble(oh) / Convert.ToDouble(ow));  
  104.                     if (Th > height)  
  105.                     {  
  106.                         Th = height;  
  107.                         Tw = width * (Convert.ToDouble(ow) / Convert.ToDouble(oh));  
  108.                     }  
  109.                     towidth = Convert.ToInt32(Tw);  
  110.                     toheight = Convert.ToInt32(Th);  
  111.                     break;  
  112.                 default:  
  113.                     break;  
  114.             }  
  115.   
  116.             //新建一个bmp图片  
  117.             Image bitmap = new Bitmap(towidth, toheight);  
  118.   
  119.             //新建一个画板  
  120.             Graphics g = Graphics.FromImage(bitmap);  
  121.   
  122.             //设置高质量插值法  
  123.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;  
  124.   
  125.             //设置高质量,低速度呈现平滑程度  
  126.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
  127.   
  128.             //清空画布并以透明背景色填充  
  129.             g.Clear(Color.Transparent);  
  130.   
  131.   
  132.             //在指定位置并且按指定大小绘制原图片的指定部分  
  133.             g.DrawImage(img, new Rectangle(0, 0, towidth, toheight),  
  134.                 new Rectangle(x, y, ow, oh),  
  135.                 GraphicsUnit.Pixel);  
  136.   
  137.             try  
  138.             {  
  139.                 //以jpg格式保存缩略图  
  140.                 bitmap.Save(savePath, ImageFormat.Jpeg);  
  141.             }  
  142.             catch (Exception ex)  
  143.             {  
  144.                 Logger.Write(string.Format("从文件流生成缩略图:{0}", ex.Message), Logger.MsgType.Error);  
  145.                 return false;  
  146.             }  
  147.             finally  
  148.             {  
  149.                 img.Dispose();  
  150.                 bitmap.Dispose();  
  151.                 g.Dispose();  
  152.             }  
  153.   
  154.             return true;  
  155.         }  
  156.         #endregion  
  157.  
  158.         #region 从文件路径生成缩略图  
  159.         /// <summary>  
  160.         /// 从图片路径生成缩略图  
  161.         /// </summary>  
  162.         /// <param name="originalImagePath">图片路径</param>  
  163.         /// <param name="savePath">保存路径</param>  
  164.         /// <param name="width">缩略图宽度</param>  
  165.         /// <param name="height">缩略图高度</param>  
  166.         /// <param name="mode">HW:指定高宽缩放(可能变形) W://指定宽,高按比例 H://指定高,宽按比例 Cut://指定高宽裁减(不变形) </param>  
  167.         /// <returns></returns>  
  168.         public static bool GetThumbNail(string originalImagePath, string savePath, int width, int height, ThumbNailScale scale)  
  169.         {  
  170.             //缩略图  
  171.             Image img = Image.FromFile(originalImagePath);  
  172.   
  173.             int towidth = width;  
  174.             int toheight = height;  
  175.   
  176.             int x = 0;  
  177.             int y = 0;  
  178.             int ow = img.Width;  
  179.             int oh = img.Height;  
  180.   
  181.             //如果图片小于指定宽度  
  182.             if (ow < width)  
  183.                 width = ow;  
  184.   
  185.             switch (scale)  
  186.             {  
  187.                 case ThumbNailScale.Appointed:  
  188.                     break;  
  189.                 case ThumbNailScale.ScaleWidth:  
  190.                     toheight = img.Height * width / img.Width;  
  191.                     break;  
  192.                 case ThumbNailScale.ScaleHeight:  
  193.                     towidth = img.Width * height / img.Height;  
  194.                     break;  
  195.                 case ThumbNailScale.Cut:  
  196.                     if ((double)img.Width / (double)img.Height > (double)towidth / (double)toheight)  
  197.                     {  
  198.                         oh = img.Height;  
  199.                         ow = img.Height * towidth / toheight;  
  200.                         y = 0;  
  201.                         x = (img.Width - ow) / 2;  
  202.                     }  
  203.                     else  
  204.                     {  
  205.                         ow = img.Width;  
  206.                         oh = img.Width * height / towidth;  
  207.                         x = 0;  
  208.                         y = (img.Height - oh) / 2;  
  209.                     }  
  210.                     break;  
  211.                 case ThumbNailScale.ScaleDown:  
  212.                     double Tw, Th;  
  213.                     Tw = width;  
  214.                     Th = height * (Convert.ToDouble(oh) / Convert.ToDouble(ow));  
  215.                     if (Th > height)  
  216.                     {  
  217.                         Th = height;  
  218.                         Tw = width * (Convert.ToDouble(ow) / Convert.ToDouble(oh));  
  219.                     }  
  220.                     towidth = Convert.ToInt32(Tw);  
  221.                     toheight = Convert.ToInt32(Th);  
  222.                     break;  
  223.                 default:  
  224.                     break;  
  225.             }  
  226.   
  227.             //新建一个bmp图片  
  228.             Image bitmap = new Bitmap(towidth, toheight);  
  229.   
  230.             //新建一个画板  
  231.             Graphics g = Graphics.FromImage(bitmap);  
  232.   
  233.             //设置高质量插值法  
  234.             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;  
  235.   
  236.             //设置高质量,低速度呈现平滑程度  
  237.             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
  238.   
  239.             //清空画布并以透明背景色填充  
  240.             g.Clear(Color.White);  
  241.   
  242.             //在指定位置并且按指定大小绘制原图片的指定部分  
  243.             g.DrawImage(img, new Rectangle(0, 0, towidth, toheight),  
  244.                 new Rectangle(x, y, ow, oh),  
  245.                 GraphicsUnit.Pixel);  
  246.   
  247.             try  
  248.             {  
  249.                 //以jpg格式保存缩略图  
  250.                 bitmap.Save(savePath, ImageFormat.Jpeg);  
  251.             }  
  252.             catch (Exception e)  
  253.             {  
  254.                 throw e;  
  255.             }  
  256.             finally  
  257.             {  
  258.                 img.Dispose();  
  259.                 bitmap.Dispose();  
  260.                 g.Dispose();  
  261.             }  
  262.   
  263.             return true;  
  264.   
  265.         }  
  266.         #endregion  
  267.  
  268.         #region 获取图片格式  
  269.         /// <summary>  
  270.         /// 获取图片格式  
  271.         /// </summary>  
  272.         /// <param name="strContentType"></param>  
  273.         /// <returns>返回图片格式</returns>  
  274.         public static ImageFormat GetImageType(object strContentType)  
  275.         {  
  276.             if ((strContentType.ToString().ToLower()) == "image/pjpeg")  
  277.             {  
  278.                 return ImageFormat.Jpeg;  
  279.             }  
  280.             else if ((strContentType.ToString().ToLower()) == "image/gif")  
  281.             {  
  282.                 return ImageFormat.Gif;  
  283.             }  
  284.             else if ((strContentType.ToString().ToLower()) == "image/bmp")  
  285.             {  
  286.                 return ImageFormat.Bmp;  
  287.             }  
  288.             else if ((strContentType.ToString().ToLower()) == "image/tiff")  
  289.             {  
  290.                 return ImageFormat.Tiff;  
  291.             }  
  292.             else if ((strContentType.ToString().ToLower()) == "image/x-icon")  
  293.             {  
  294.                 return ImageFormat.Icon;  
  295.             }  
  296.             else if ((strContentType.ToString().ToLower()) == "image/x-png")  
  297.             {  
  298.                 return ImageFormat.Png;  
  299.             }  
  300.             else if ((strContentType.ToString().ToLower()) == "image/x-emf")  
  301.             {  
  302.                 return ImageFormat.Emf;  
  303.             }  
  304.             else if ((strContentType.ToString().ToLower()) == "image/x-exif")  
  305.             {  
  306.                 return ImageFormat.Exif;  
  307.             }  
  308.             else if ((strContentType.ToString().ToLower()) == "image/x-wmf")  
  309.             {  
  310.                 return ImageFormat.Wmf;  
  311.             }  
  312.             else  
  313.             {  
  314.                 return ImageFormat.MemoryBmp;  
  315.             }  
  316.         }  
  317.         #endregion  
  318.   
  319.         /// <summary>  
  320.         /// 生成水印图片  
  321.         /// </summary>  
  322.         /// <param name="sourceFile"></param>  
  323.         /// <param name="saveFile">保存文件路径</param>  
  324.         /// <returns></returns>  
  325.         public static bool MakeWaterImage(Stream sourceFile, string saveFile)  
  326.         {  
  327.             bool result = false;  
  328.             //水印图片  
  329.             try  
  330.             {  
  331.                 Image imgPhoto = Image.FromStream(sourceFile);  
  332.                 sourceFile.Close();  
  333.                 sourceFile.Dispose();  
  334.   
  335.                 int phWidth = imgPhoto.Width;  
  336.                 int phHeight = imgPhoto.Height;  
  337.   
  338.                 Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);  
  339.                 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);  
  340.   
  341.                 Image imgWatermark = new Bitmap(System.Web.HttpContext.Current.Server.MapPath("/images/watermark.png"));  
  342.                 int wmWidth = imgWatermark.Width;  
  343.                 int wmHeight = imgWatermark.Height;  
  344.   
  345.                 if (phWidth > (wmWidth + 100) && phHeight > (wmHeight + 100))  
  346.                 {  
  347.                     Graphics grPhoto = Graphics.FromImage(bmPhoto);  
  348.                     grPhoto.Clear(Color.White);  
  349.                     grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);  
  350.   
  351.                     grPhoto.Dispose();  
  352.   
  353.                     //添加水印图片  
  354.   
  355.                     using (Bitmap bmWatermark = new Bitmap(bmPhoto))  
  356.                     {  
  357.                         bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);  
  358.                         Graphics grWatermark = Graphics.FromImage(bmWatermark);  
  359.                         using (ImageAttributes imageAttributes = new ImageAttributes())  
  360.                         {  
  361.                             //ColorMap colorMap = new ColorMap();  
  362.                             //colorMap.OldColor = Color.FromArgb(255, 255, 255,255);  
  363.                             //colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);  
  364.                             //ColorMap[] remapTable = { colorMap };  
  365.                             //imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);  
  366.                             float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } };  
  367.                             ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);  
  368.                             imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
  369.                             int xPosOfWm = ((phWidth - wmWidth) - 2);  
  370.                             int yPosOfWm = ((phHeight - wmHeight) - 2);  
  371.                             grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);  
  372.                         }  
  373.                         imgPhoto = bmWatermark;  
  374.                         grWatermark.Dispose();  
  375.                         imgPhoto.Save(saveFile, ImageFormat.Jpeg);  
  376.                     }  
  377.   
  378.                     result = true;  
  379.                 }  
  380.                 else  
  381.                 {  
  382.                     result = false;  
  383.                 }  
  384.                 imgWatermark.Dispose();  
  385.                 bmPhoto.Dispose();  
  386.                 imgPhoto.Dispose();  
  387.             }  
  388.             catch(Exception ex)  
  389.             {  
  390.                 Logger.Write(string.Format("生成水印图片错误:{0}",ex.Message), Logger.MsgType.Information);  
  391.   
  392.                 try  
  393.                 {  
  394.                     Image imgPhoto2 = Image.FromStream(sourceFile);  
  395.                     imgPhoto2.Save(saveFile, ImageFormat.Jpeg);  
  396.                     imgPhoto2.Dispose();  
  397.                     result = true;  
  398.                 }  
  399.                 catch  
  400.                 {  
  401.                     result = false;  
  402.                 }  
  403.             }  
  404.   
  405.             return result;  
  406.   
  407.         }  
  408.   
  409.         /// <summary>  
  410.         /// 生成水印图片  
  411.         /// </summary>  
  412.         /// <param name="sourceFile"></param>  
  413.         /// <param name="saveFile">保存文件路径</param>  
  414.         /// <param name="Location">位置 0 - 右下角 1 -  居中 2 - 右上角 3 - 左下角</param>  
  415.         /// <returns></returns>  
  416.         public static bool MakeWaterImage(Stream sourceFile, string saveFile, ImagePosition Position)  
  417.         {  
  418.             bool result = false;  
  419.             //水印图片  
  420.             try  
  421.             {  
  422.                 Image imgPhoto = Image.FromStream(sourceFile);  
  423.   
  424.   
  425.                 int phWidth = imgPhoto.Width;  
  426.                 int phHeight = imgPhoto.Height;  
  427.   
  428.                 Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);  
  429.                 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);  
  430.   
  431.                 Image imgWatermark = new Bitmap(System.Web.HttpContext.Current.Server.MapPath("/images/watermark.png"));  
  432.                 int wmWidth = imgWatermark.Width;  
  433.                 int wmHeight = imgWatermark.Height;  
  434.   
  435.                 if (phWidth > (wmWidth + 100) && phHeight > (wmHeight + 100))  
  436.                 {  
  437.                     Graphics grPhoto = Graphics.FromImage(bmPhoto);  
  438.                     grPhoto.Clear(Color.White);  
  439.                     grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);  
  440.   
  441.                     grPhoto.Dispose();  
  442.   
  443.                     //添加水印图片  
  444.   
  445.                     using (Bitmap bmWatermark = new Bitmap(bmPhoto))  
  446.                     {  
  447.                         bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);  
  448.                         Graphics grWatermark = Graphics.FromImage(bmWatermark);  
  449.                         using (ImageAttributes imageAttributes = new ImageAttributes())  
  450.                         {  
  451.                             //ColorMap colorMap = new ColorMap();  
  452.                             //colorMap.OldColor = Color.FromArgb(255, 255, 255,255);  
  453.                             //colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);  
  454.                             //ColorMap[] remapTable = { colorMap };  
  455.                             //imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);  
  456.                             float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } };  
  457.                             ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);  
  458.                             imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);  
  459.                             int xPosOfWm = 0;  
  460.                             int yPosOfWm = 0;  
  461.                             switch (Position)  
  462.                             {  
  463.                                 case ImagePosition.BottomRight:  
  464.                                     xPosOfWm = ((phWidth - wmWidth) - 2);  
  465.                                     yPosOfWm = ((phHeight - wmHeight) - 2);  
  466.                                     break;  
  467.                                 case ImagePosition.TopLeft:  
  468.                                     xPosOfWm = 2;  
  469.                                     yPosOfWm = 2;  
  470.                                     break;  
  471.                                 case ImagePosition.TopRigth:  
  472.                                     xPosOfWm = ((phWidth - wmWidth) - 2);  
  473.                                     yPosOfWm = 2;  
  474.                                     break;  
  475.                                 case ImagePosition.BottomLeft:  
  476.                                     xPosOfWm = 2;  
  477.                                     yPosOfWm = ((phHeight - wmHeight) - 2);  
  478.                                     break;  
  479.                                 case ImagePosition.Center:  
  480.                                     xPosOfWm = ((phWidth / 2) - (wmWidth / 2));  
  481.                                     yPosOfWm = ((phHeight / 2) - (wmHeight / 2));  
  482.                                     break;  
  483.                             }  
  484.                             grWatermark.DrawImage(imgWatermark, new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), 0, 0, wmWidth, wmHeight, GraphicsUnit.Pixel, imageAttributes);  
  485.                         }  
  486.                         imgPhoto = bmWatermark;  
  487.                         grWatermark.Dispose();  
  488.                         imgPhoto.Save(saveFile, ImageFormat.Jpeg);  
  489.                     }  
  490.   
  491.                     result = true;  
  492.                 }  
  493.                 else  
  494.                 {  
  495.                     Image imgPhoto2 = Image.FromStream(sourceFile);  
  496.                     imgPhoto2.Save(saveFile, ImageFormat.Jpeg);  
  497.                     imgPhoto2.Dispose();  
  498.                     result = true;  
  499.                 }  
  500.                 imgWatermark.Dispose();  
  501.                 bmPhoto.Dispose();  
  502.                 imgPhoto.Dispose();  
  503.             }  
  504.             catch  
  505.             {  
  506.   
  507.                 try  
  508.                 {  
  509.                     Image imgPhoto2 = Image.FromStream(sourceFile);  
  510.                     imgPhoto2.Save(saveFile, ImageFormat.Jpeg);  
  511.                     imgPhoto2.Dispose();  
  512.                     result = true;  
  513.                 }  
  514.                 catch  
  515.                 {  
  516.                     result = false;  
  517.                 }  
  518.             }  
  519.   
  520.             sourceFile.Close();  
  521.             sourceFile.Dispose();  
  522.   
  523.             return result;  
  524.   
  525.         }  
  526.  
  527.         #region 从图片中截取一张指定大小的图片  
  528.         /// <summary>  
  529.         /// 从图片中截取部分生成新图  
  530.         /// </summary>  
  531.         /// <param name="sFromFilePath">原始图片</param>  
  532.         /// <param name="saveFilePath">生成新图</param>  
  533.         /// <param name="width">截取图片宽度</param>  
  534.         /// <param name="height">截取图片高度</param>  
  535.         /// <param name="spaceX">截图图片X坐标</param>  
  536.         /// <param name="spaceY">截取图片Y坐标</param>  
  537.         public static void CaptureImage(string sFromFilePath, string saveFilePath, int width, int height, int spaceX, int spaceY)  
  538.         {  
  539.             //载入底图  
  540.             Image fromImage = Image.FromFile(sFromFilePath);  
  541.             int x = 0; //截取X坐标  
  542.             int y = 0; //截取Y坐标  
  543.             //原图宽与生成图片宽 之差  
  544.             //当小于0(即原图宽小于要生成的图)时,新图宽度为较小者 即原图宽度 X坐标则为0  
  545.             //当大于0(即原图宽大于要生成的图)时,新图宽度为设置值 即width X坐标则为 sX与spaceX之间较小者  
  546.             //Y方向同理  
  547.             int sX = fromImage.Width - width;  
  548.             int sY = fromImage.Height - height;  
  549.             if (sX > 0)  
  550.             {  
  551.                 x = sX > spaceX ? spaceX : sX;  
  552.             }  
  553.             else  
  554.             {  
  555.                 width = fromImage.Width;  
  556.             }  
  557.             if (sY > 0)  
  558.             {  
  559.                 y = sY > spaceY ? spaceY : sY;  
  560.             }  
  561.             else  
  562.             {  
  563.                 height = fromImage.Height;  
  564.             }  
  565.   
  566.             //创建新图位图  
  567.             Bitmap bitmap = new Bitmap(width, height);  
  568.             //创建作图区域  
  569.             Graphics graphic = Graphics.FromImage(bitmap);  
  570.             //截取原图相应区域写入作图区  
  571.             graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);  
  572.             //从作图区生成新图  
  573.             Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());  
  574.             //保存图象  
  575.             saveImage.Save(saveFilePath, ImageFormat.Jpeg);  
  576.             //释放资源  
  577.             saveImage.Dispose();  
  578.             bitmap.Dispose();  
  579.             graphic.Dispose();  
  580.         }  
  581.         #endregion  
  582.   
  583.         public enum ImagePosition  
  584.         {  
  585.             /// <summary>  
  586.             /// 居中  
  587.             /// </summary>  
  588.             Center,  
  589.             /// <summary>  
  590.             /// 左上角  
  591.             /// </summary>  
  592.             TopLeft,  
  593.             /// <summary>  
  594.             /// 左下角  
  595.             /// </summary>  
  596.             BottomLeft,  
  597.             /// <summary>  
  598.             /// 右下角  
  599.             /// </summary>  
  600.             BottomRight,  
  601.             /// <summary>  
  602.             /// 右上角  
  603.             /// </summary>  
  604.             TopRigth  
  605.         }  
  606.   
  607.         /// <summary>  
  608.         /// 图片  
  609.         /// </summary>  
  610.         public enum ThumbNailScale  
  611.         {  
  612.             /// <summary>  
  613.             /// 指定高宽缩放,图片长宽不一致会变形  
  614.             /// </summary>  
  615.             Appointed,  
  616.             /// <summary>  
  617.             /// 指定宽,高按比例      
  618.             /// </summary>  
  619.             ScaleWidth,  
  620.             /// <summary>  
  621.             /// 指定高,宽按比例  
  622.             /// </summary>  
  623.             ScaleHeight,  
  624.             /// <summary>  
  625.             /// 指定高宽裁减,可能只显示部分图片  
  626.             /// </summary>  
  627.             Cut,  
  628.             /// <summary>  
  629.             /// 按图片比例缩放,不变形,显示全部图片(推荐)  
  630.             /// </summary>  
  631.             ScaleDown  
  632.         }  
  633.     }  
  634. }