zoukankan      html  css  js  c++  java
  • asp.net生成缩略图、文字图片水印

      1        /// <summary>
      2         /// 会产生graphics异常的PixelFormat
      3         /// </summary>
      4         private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
      5 PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
      6 PixelFormat.Format8bppIndexed
      7     };
      8 
      9 
     10         /// <summary>
     11         /// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
     12         /// </summary>
     13         /// <param name="imgPixelFormat">原图片的PixelFormat</param>
     14         /// <returns></returns>
     15         private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
     16         {
     17             foreach (PixelFormat pf in indexedPixelFormats)
     18             {
     19                 if (pf.Equals(imgPixelFormat)) return true;
     20             }
     21 
     22             return false;
     23         }
     24 
     25        /// <summary>
     26         /// 在图片上生成图片水印
     27         /// </summary>
     28         /// <param name="Path">原服务器图片路径</param>
     29         /// <param name="Path_syp">生成的带图片水印的图片路径</param>
     30         /// <param name="Path_sypf">水印图片路径</param>
     31         public static void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
     32         {
     33             System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
     34             System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
     35             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
     36             g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
     37             g.Dispose();
     38 
     39             image.Save(Path_syp);
     40             image.Dispose();
     41         }
     42         /// <summary>
     43         /// 在图片上增加文字水印
     44         /// </summary>
     45         /// <param name="Path">原服务器图片路径</param>
     46         /// <param name="Path_sy">生成的带文字水印的图片路径</param>
     47         public static void AddShuiYinWord(string Path, string Path_sy)
     48         {
     49 
     50 
     51             using (System.Drawing.Image img = System.Drawing.Image.FromFile(Path))
     52             {
     53                 //如果原图片是索引像素格式之列的,则需要转换
     54                 if (IsPixelFormatIndexed(img.PixelFormat))
     55                 {
     56                     Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
     57                     using (Graphics g = Graphics.FromImage(bmp))
     58                     {
     59                         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
     60                         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     61                         g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
     62                         g.DrawImage(img, 0, 0);
     63 
     64                         System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
     65                         System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
     66                         g.DrawString("爱智旮旯", f, b, 15, 15);
     67                      
     68 
     69                     }
     70                     bmp.Save(Path_sy);
     71                     bmp.Dispose();
     72                     
     73                 }
     74                 else //否则直接操作
     75                 {
     76                     string addText = "爱智旮旯";
     77                     System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
     78                     System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
     79                     g.DrawImage(image, 0, 0, image.Width, image.Height);
     80                     System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
     81                     System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
     82 
     83                     g.DrawString(addText, f, b, 15, 15);
     84                     g.Dispose();
     85 
     86                     image.Save(Path_sy);
     87                     image.Dispose();
     88                 }
     89             } 
     90 
     91 
     92 
     93 
     94 
     95 
     96          
     97         }
     98 
     99         /// <summary>
    100         /// 生成缩略图
    101         /// </summary>
    102         /// <param name="originalImagePath">源图路径(物理路径)</param>
    103         /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
    104         /// <param name="width">缩略图宽度</param>
    105         /// <param name="height">缩略图高度</param>
    106         /// <param name="mode">生成缩略图的方式</param> 
    107         public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    108         {
    109             System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
    110 
    111             int towidth = width;
    112             int toheight = height;
    113 
    114             int x = 0;
    115             int y = 0;
    116             int ow = originalImage.Width;
    117             int oh = originalImage.Height;
    118 
    119             switch (mode)
    120             {
    121                 case "HW"://指定高宽缩放(可能变形) 
    122                     break;
    123                 case "W"://指定宽,高按比例 
    124                     toheight = originalImage.Height * width / originalImage.Width;
    125                     break;
    126                 case "H"://指定高,宽按比例
    127                     towidth = originalImage.Width * height / originalImage.Height;
    128                     break;
    129                 case "Cut"://指定高宽裁减(不变形) 
    130                     if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
    131                     {
    132                         oh = originalImage.Height;
    133                         ow = originalImage.Height * towidth / toheight;
    134                         y = 0;
    135                         x = (originalImage.Width - ow) / 2;
    136                     }
    137                     else
    138                     {
    139                         ow = originalImage.Width;
    140                         oh = originalImage.Width * height / towidth;
    141                         x = 0;
    142                         y = (originalImage.Height - oh) / 2;
    143                     }
    144                     break;
    145                 default:
    146                     break;
    147             }
    148 
    149             //新建一个bmp图片
    150             System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
    151 
    152             //新建一个画板
    153             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    154 
    155             //设置高质量插值法
    156             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    157 
    158             //设置高质量,低速度呈现平滑程度
    159             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    160 
    161             //清空画布并以透明背景色填充
    162             g.Clear(System.Drawing.Color.Transparent);
    163 
    164             //在指定位置并且按指定大小绘制原图片的指定部分
    165             g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
    166             new System.Drawing.Rectangle(x, y, ow, oh),
    167             System.Drawing.GraphicsUnit.Pixel);
    168 
    169             try
    170             {
    171                 //以jpg格式保存缩略图
    172                 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
    173             }
    174             catch (System.Exception e)
    175             {
    176                 throw e;
    177             }
    178             finally
    179             {
    180                 originalImage.Dispose();
    181                 bitmap.Dispose();
    182                 g.Dispose();
    183             }
    184         }
    185 
    186       protected void Button1_Click(object sender, EventArgs e)
    187         {
    188             if (FileUpload1.HasFile)
    189             {
    190                 string fileContentType = FileUpload1.PostedFile.ContentType;
    191                 if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/jpg")
    192                 {
    193                     string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
    194 
    195                     FileInfo file = new FileInfo(name);
    196                     string fileName = file.Name; // 文件名称
    197                     string fileName_s = "s_" + file.Name; // 缩略图文件名称
    198                     string fileName_sy = "sy_" + file.Name; // 水印图文件名称(文字)
    199                     string fileName_syp = "syp_" + file.Name; // 水印图文件名称(图片)
    200                     //以下路径可以根据情况进行修改
    201                     string webFilePath = Server.MapPath("file/" + fileName); // 服务器端文件路径
    202                     string webFilePath_s = Server.MapPath("file/" + fileName_s);  // 服务器端缩略图路径
    203                     string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // 服务器端带水印图路径(文字)
    204                     string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // 服务器端带水印图路径(图片)
    205                     //以下为水印图片的路径一定要先准备一张水印图片!
    206                     string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // 服务器端水印图路径(图片)
    207 
    208                     if (!File.Exists(webFilePath))
    209                     {
    210                         try
    211                         {
    212                             FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
    213                             AddShuiYinWord(webFilePath, webFilePath_sy);
    214                             //AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
    215                             MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut"); // 生成缩略图方法
    216                             Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
    217                         }
    218                         catch (Exception ex)
    219                         {
    220                             Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
    221                         }
    222                     }
    223                     else
    224                     {
    225                         Label1.Text = "提示:文件已经存在,请重命名后上传";
    226                     }
    227                 }
    228                 else
    229                 {
    230                     Label1.Text = "提示:文件类型不符";
    231                 }
    232 
    233             }
    234         }
    View Code
  • 相关阅读:
    umeng社交分享最新版5.0的跨进程使用崩溃的问题及解法-Android
    AlertDialog禁止返回键
    一个男人想经商,不读 100本商人自传,怎么会了解商人的思维状态
    Android中使用Gson解析JSON数据的两种方法
    DevExpress gridControl控件动态绑定列 zt
    获得WCF Client端的本地端口 z
    log4net.dll配置以及在项目中应用 zt
    系统交易策略 hylt
    判斷作業系統為 64bit 或 32bit z
    路徑 z
  • 原文地址:https://www.cnblogs.com/keyyang/p/3808473.html
Copyright © 2011-2022 走看看