asp.net生成缩略图,添加图片水印的类

 
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. namespace BLL
  6. {
  7.     public class ImageUtil
  8.     {
  9.         public static void addWatermarkImage(Graphics picture, string WaterMarkPicPath, string _watermarkPosition, int _width, int _height)
  10.         {
  11.             Image image = new Bitmap(WaterMarkPicPath);
  12.             int x = 0;
  13.             int y = 0;
  14.             int width = image.Width;
  15.             int height = image.Height;
  16.             string str = _watermarkPosition;
  17.             if (str != null)
  18.             {
  19.                 if (!(str == "WM_TOP_LEFT"))
  20.                 {
  21.                     if (str == "WM_TOP_RIGHT")
  22.                     {
  23.                         x = (_width - width) - 10;
  24.                         y = 10;
  25.                     }
  26.                     else if (str == "WM_BOTTOM_RIGHT")
  27.                     {
  28.                         x = (_width - width) - 10;
  29.                         y = (_height - height) - 10;
  30.                     }
  31.                     else if (str == "WM_BOTTOM_LEFT")
  32.                     {
  33.                         x = 10;
  34.                         y = (_height - height) - 10;
  35.                     }
  36.                     else if (str == "WM_BOTTOM_MIDDLE")
  37.                     {
  38.                         x = (_width / 2) - (width / 2);
  39.                         y = (_height - height) - 10;
  40.                     }
  41.                     else if (str == "WM_TOP_MIDDLE")
  42.                     {
  43.                         x = (_width / 2) - (width / 2);
  44.                         y = 10;
  45.                     }
  46.                 }
  47.                 else
  48.                 {
  49.                     x = 10;
  50.                     y = 10;
  51.                 }
  52.             }
  53.             picture.DrawImage(image, x, y, width, height);
  54.             image.Dispose();
  55.         }
  56.         public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  57.         {
  58.             string str;
  59.             Image image = Image.FromFile(originalImagePath);
  60.             int num = width;
  61.             int num2 = height;
  62.             int x = 0;
  63.             int y = 0;
  64.             int num5 = image.Width;
  65.             int num6 = image.Height;
  66.             if (((str = mode) != null) && (str != "HW"))
  67.             {
  68.                 if (!(str == "W"))
  69.                 {
  70.                     if (str == "H")
  71.                     {
  72.                         num = (image.Width * height) / image.Height;
  73.                     }
  74.                     else if (str == "Cut")
  75.                     {
  76.                         if ((((double)image.Width) / ((double)image.Height)) > (((double)num) / ((double)num2)))
  77.                         {
  78.                             num6 = image.Height;
  79.                             num5 = (image.Height * num) / num2;
  80.                             y = 0;
  81.                             x = (image.Width - num5) / 2;
  82.                         }
  83.                         else
  84.                         {
  85.                             num5 = image.Width;
  86.                             num6 = (image.Width * height) / num;
  87.                             x = 0;
  88.                             y = (image.Height - num6) / 2;
  89.                         }
  90.                     }
  91.                 }
  92.                 else
  93.                 {
  94.                     num2 = (image.Height * width) / image.Width;
  95.                 }
  96.             }
  97.             Image image2 = new Bitmap(num, num2);
  98.             Graphics graphics = Graphics.FromImage(image2);
  99.             graphics.InterpolationMode = InterpolationMode.High;
  100.             graphics.SmoothingMode = SmoothingMode.HighQuality;
  101.             graphics.Clear(Color.Transparent);
  102.             graphics.DrawImage(image, new Rectangle(0, 0, num, num2), new Rectangle(x, y, num5, num6), GraphicsUnit.Pixel);
  103.             try
  104.             {
  105.                 image2.Save(thumbnailPath, ImageFormat.Jpeg);
  106.             }
  107.             catch (Exception exception)
  108.             {
  109.                 throw exception;
  110.             }
  111.             finally
  112.             {
  113.                 image.Dispose();
  114.                 image2.Dispose();
  115.                 graphics.Dispose();
  116.             }
  117.         }
  118.     }
  119. }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值