当前位置: 移动技术网 > IT编程>开发语言>c# > C#给图片添加水印完整实例

C#给图片添加水印完整实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#给图片添加水印的方法。分享给大家供大家参考,具体如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.drawing;
using system.io;
using system.drawing.imaging;
/// <summary>
///imgwater 的摘要说明
/// </summary>
public class imgwater
{
 public imgwater()
 {
  //
  //todo: 在此处添加构造函数逻辑
  //
 }
 /// <summary>
 /// 图片水印
 /// </summary>
 /// <param name="imgfile">原图文件地址</param>
 /// <param name="waterimg">水印图片地址</param>
 /// <param name="simgpath">水印图片保存地址</param>
 /// <param name="alpha">水印透明度设置</param>
 /// <param name="iscale">水印图片在原图上的显示比例</param>
 /// <param name="intdistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
 public bool zzsimgwater(
   string imgfile
  , string waterimg
  , string simgpath
  , float alpha
  , float iscale
  , int intdistance
  )
  {
  try
  {
   //装载图片
   filestream fs = new filestream(imgfile, filemode.open);
   binaryreader br = new binaryreader(fs);
   byte[] bytes = br.readbytes((int)fs.length);
   br.close();
   fs.close();
   memorystream ms = new memorystream(bytes);
   system.drawing.image imgphoto = system.drawing.image.fromstream(ms);
   //system.drawing.image imgphoto = system.drawing.image.fromfile(imgfile);
   int imgphotowidth = imgphoto.width;
   int imgphotoheight = imgphoto.height;
   system.drawing.image imgwatermark = new bitmap(waterimg);
   int imgwatermarkwidth = imgwatermark.width;
   int imgwatermarkheight = imgwatermark.height;
   //计算水印图片尺寸
   decimal ascale = convert.todecimal(iscale);
   decimal pscale = 0.05m;
   decimal minscale = ascale - pscale;
   decimal maxscale = ascale + pscale;
   int imgwatermarkwidthnew = imgwatermarkwidth;
   int imgwatermarkheightnew = imgwatermarkheight;
   if (imgphotowidth >= imgwatermarkwidth && imgphotoheight >= imgwatermarkheight && imgphotowidth >= imgphotoheight)
    if (imgwatermarkwidth > imgwatermarkheight)
     if ((minscale <= math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7)) && (math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7) <= maxscale))
      {
     }
     else
      {
      imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
      imgwatermarkheightnew = convert.toint32((imgphotowidth * ascale / imgwatermarkwidth) * imgwatermarkheight);
     }
    else
     if ((minscale <= math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7)) && (math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7) <= maxscale))
     {
     }
     else
      {
      imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
      imgwatermarkwidthnew = convert.toint32((imgphotoheight * ascale / imgwatermarkheight) * imgwatermarkwidth);
     }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgwatermarkwidth >= imgwatermarkheight)
    {
    imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
    imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
   }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight <= imgphotoheight && imgphotowidth >= imgphotoheight)
    {
    imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
    imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
   }
   if (imgwatermarkwidth <= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgphotowidth >= imgphotoheight)
    {
    imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
    imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
   }
   if (imgphotowidth >= imgwatermarkwidth && imgphotoheight >= imgwatermarkheight && imgphotowidth <= imgphotoheight)
    if (imgwatermarkwidth > imgwatermarkheight)
     if ((minscale <= math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7)) && (math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7) <= maxscale))
      {
     }
     else
      {
      imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
      imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
     }
    else
     if ((minscale <= math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7)) && (math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7) <= maxscale))
      {
     }
     else
      {
      imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
      imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
     }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgwatermarkwidth <= imgwatermarkheight)
    {
    imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
    imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
   }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight <= imgphotoheight && imgphotowidth <= imgphotoheight)
    {
    imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
    imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
   }
   if (imgwatermarkwidth <= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgphotowidth <= imgphotoheight)
    {
    imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
    imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
   }
   //将原图画出来
   bitmap bmphoto = new bitmap(imgphotowidth, imgphotoheight, system.drawing.imaging.pixelformat.format24bpprgb);
   bmphoto.setresolution(72, 72);
   graphics gbmphoto = graphics.fromimage(bmphoto);
   gbmphoto.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
   gbmphoto.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
   gbmphoto.clear(color.white);
   gbmphoto.drawimage(
     imgphoto
    , new rectangle(0, 0, imgphotowidth, imgphotoheight)
    , 0
    , 0
    , imgphotowidth
    , imgphotoheight
    , graphicsunit.pixel
    );
   bitmap bmwatermark = new bitmap(bmphoto);
   bmwatermark.setresolution(imgphoto.horizontalresolution, imgphoto.verticalresolution);
   graphics gwatermark = graphics.fromimage(bmwatermark);
   //指定高质量显示水印图片质量
   gwatermark.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
   gwatermark.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
    imageattributes imageattributes = new imageattributes();
   //设置两种颜色,达到合成效果
   colormap colormap = new colormap();
   colormap.oldcolor = color.fromargb(255, 0, 255, 0);
   colormap.newcolor = color.fromargb(0, 0, 0, 0);
   colormap[] remaptable = { colormap };
   imageattributes.setremaptable(remaptable, system.drawing.imaging.coloradjusttype.bitmap);
     //用矩阵设置水印图片透明度
   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, alpha, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
   };
   system.drawing.imaging.colormatrix wmcolormatrix = new system.drawing.imaging.colormatrix(colormatrixelements);
   imageattributes.setcolormatrix(wmcolormatrix, system.drawing.imaging.colormatrixflag.default, system.drawing.imaging.coloradjusttype.bitmap);
   //确定水印边距
   int xpos = imgphotowidth - imgwatermarkwidthnew;
   int ypos = imgphotoheight - imgwatermarkheightnew;
   int xposofwm = 0;
   int yposofwm = 0;
   if (xpos > intdistance)
    xposofwm = xpos - intdistance;
   else
    xposofwm = xpos;
   if (ypos > intdistance)
    yposofwm = ypos - intdistance;
   else
    yposofwm = ypos;
   gwatermark.drawimage(
     imgwatermark
    , new rectangle(xposofwm, yposofwm, imgwatermarkwidthnew, imgwatermarkheightnew)
    , 0
    , 0
    , imgwatermarkwidth
    , imgwatermarkheight
    , graphicsunit.pixel
    , imageattributes
    );
   imgphoto = bmwatermark;
   //以jpg格式保存图片
   imgphoto.save(simgpath, system.drawing.imaging.imageformat.jpeg);
   //销毁对象
   gbmphoto.dispose();
   gwatermark.dispose();
   imgphoto.dispose();
   imgwatermark.dispose();
   return true;
  }
  catch
  {
   return false ;
  }
 }
 /**//// <summary>
 /// 文字水印
 /// </summary>
 /// <param name="imgfile">原图文件地址</param>
 /// <param name="textfont">水印文字</param>
 /// <param name="simgpath">文字水印图片保存地址</param>
 /// <param name="hscale">高度位置</param>
 /// <param name="widthfont">文字块在图片中所占宽度比例</param>
 /// <param name="alpha">文字透明度 其数值的范围在0到255</param>
 public bool zzstextwater(
   string imgfile
  , string textfont
  , string simgpath
  , float hscale
  , float widthfont
  , int alpha
  )
  {
  try
  {
   filestream fs = new filestream(imgfile, filemode.open);
   binaryreader br = new binaryreader(fs);
   byte[] bytes = br.readbytes((int)fs.length);
   br.close();
   fs.close();
   memorystream ms = new memorystream(bytes);
   system.drawing.image imgphoto = system.drawing.image.fromstream(ms);
   //system.drawing.image imgphoto = system.drawing.image.fromfile(imgfile);
   int imgphotowidth = imgphoto.width;
   int imgphotoheight = imgphoto.height;
   bitmap bmphoto = new bitmap(imgphotowidth, imgphotoheight, system.drawing.imaging.pixelformat.format24bpprgb);
   bmphoto.setresolution(72, 72);
   graphics gbmphoto = graphics.fromimage(bmphoto);
   gbmphoto.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
   gbmphoto.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
   gbmphoto.drawimage(
     imgphoto
    , new rectangle(0, 0, imgphotowidth, imgphotoheight)
    , 0
    , 0
    , imgphotowidth
    , imgphotoheight
    , graphicsunit.pixel
    );
   //建立字体大小的数组,循环找出适合图片的水印字体
   //int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
   int[] sizes = new int[] { 28, 26, 24, 20, 16, 14, 12 };
   system.drawing.font crfont = null;
   system.drawing.sizef crsize = new sizef();
   for (int i = 0; i < 7; i++)
   {
    crfont = new font("微软雅黑", sizes[i], fontstyle.bold);
    crsize = gbmphoto.measurestring(textfont, crfont);
    if ((ushort)crsize.width < (ushort)imgphotowidth * widthfont)
     break;
   }
   //设置水印字体的位置
   //int ypixlesfrombottom = (int)(imgphotoheight * hscale);
   //float yposfrombottom = ((imgphotoheight - ypixlesfrombottom) - (crsize.height / 2));
   //float xcenterofimg = (imgphotowidth * 1 / 2);
   float yposfrombottom = imgphotoheight *0.85f;
   float xcenterofimg = imgphotowidth * 0.8f;
   //if (xcenterofimg<crsize.height)
   // xcenterofimg = (imgphotowidth * (1 - (crsize.height)/ imgphotowidth));
   system.drawing.stringformat strformat = new system.drawing.stringformat();
   strformat.alignment = system.drawing.stringalignment.center;
   //
   system.drawing.solidbrush semitransbrush2 = new system.drawing.solidbrush(color.fromargb(alpha, 0, 0, 0));
   gbmphoto.drawstring(
     textfont
    , crfont
    , semitransbrush2
    , new system.drawing.pointf(xcenterofimg + 1, yposfrombottom + 1)
    , strformat
    );
   system.drawing.solidbrush semitransbrush = new system.drawing.solidbrush(color.fromargb(alpha, 255, 255, 255));
   //gbmphoto.fillrectangle(semitransbrush2, new rectanglef(new pointf(xcenterofimg - crsize.width / 2, yposfrombottom - 4), crsize));
   gbmphoto.drawstring(
     textfont
    , crfont
    , semitransbrush
    , new system.drawing.pointf(xcenterofimg, yposfrombottom)
    , strformat
    );
   bmphoto.save(simgpath, system.drawing.imaging.imageformat.jpeg);
   gbmphoto.dispose();
   imgphoto.dispose();
   bmphoto.dispose();
   return true;
  }
  catch 
  {
   return false;
  }
 }
 /**//// <summary>
 /// 文字和logo图片水印
 /// </summary>
 /// <param name="imgfile">原图文件地址</param>
 /// <param name="waterimg">水印图片地址</param>
 /// <param name="textfont">水印文字信息</param>
 /// <param name="simgpath">生存水印图片后的保存地址</param>
 /// <param name="imgalpha">水印图片的透明度</param>
 /// <param name="imgiscale">水印图片在原图上的显示比例</param>
 /// <param name="intimgdistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>
 /// <param name="texthscale">水印文字高度位置,从图片底部开始计算,0-1</param>
 /// <param name="textwidthfont">文字块在图片中所占宽度比例 0-1</param>
 /// <param name="textalpha">文字透明度 其数值的范围在0到255</param>
 public void zzsimgtextwater(
   string imgfile
  , string waterimg
  , string textfont
  , string simgpath
  , float imgalpha
  , float imgiscale
  , int intimgdistance
  , float texthscale
  , float textwidthfont
  , int textalpha
  )
  {
  try
  {
   filestream fs = new filestream(imgfile, filemode.open);
   binaryreader br = new binaryreader(fs);
   byte[] bytes = br.readbytes((int)fs.length);
   br.close();
   fs.close();
   memorystream ms = new memorystream(bytes);
   system.drawing.image imgphoto = system.drawing.image.fromstream(ms);
   //system.drawing.image imgphoto = system.drawing.image.fromfile(imgfile);
   int imgphotowidth = imgphoto.width;
   int imgphotoheight = imgphoto.height;
   bitmap bmphoto = new bitmap(imgphotowidth, imgphotoheight, system.drawing.imaging.pixelformat.format24bpprgb);
   bmphoto.setresolution(72, 72);
   graphics gbmphoto = graphics.fromimage(bmphoto);
   gbmphoto.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
   gbmphoto.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
   gbmphoto.drawimage(
     imgphoto
    , new rectangle(0, 0, imgphotowidth, imgphotoheight)
    , 0
    , 0
    , imgphotowidth
    , imgphotoheight
    , graphicsunit.pixel
    );
   //建立字体大小的数组,循环找出适合图片的水印字体
   int[] sizes = new int[] { 1000, 800, 700, 650, 600, 560, 540, 500, 450, 400, 380, 360, 340, 320, 300, 280, 260, 240, 220, 200, 180, 160, 140, 120, 100, 80, 72, 64, 48, 32, 28, 26, 24, 20, 28, 16, 14, 12, 10, 8, 6, 4, 2 };
   system.drawing.font crfont = null;
   system.drawing.sizef crsize = new sizef();
   for (int i = 0; i < 43; i++)
   {
    crfont = new font("arial", sizes[i], fontstyle.bold);
    crsize = gbmphoto.measurestring(textfont, crfont);
    if ((ushort)crsize.width < (ushort)imgphotowidth * textwidthfont)
     break;
   }
   //设置水印字体的位置
   int ypixlesfrombottom = (int)(imgphotoheight * texthscale);
   float yposfrombottom = ((imgphotoheight - ypixlesfrombottom) - (crsize.height / 2));
   float xcenterofimg = (imgphotowidth * 1 / 2);
   system.drawing.stringformat strformat = new system.drawing.stringformat();
   strformat.alignment = system.drawing.stringalignment.center;
   //
   system.drawing.solidbrush semitransbrush2 = new system.drawing.solidbrush(color.fromargb(textalpha, 0, 0, 0));
   gbmphoto.drawstring(
     textfont
    , crfont
    , semitransbrush2
    , new system.drawing.pointf(xcenterofimg + 1, yposfrombottom + 1)
    , strformat
    );
   system.drawing.solidbrush semitransbrush = new system.drawing.solidbrush(color.fromargb(textalpha, 255, 255, 255));
   gbmphoto.drawstring(
     textfont
    , crfont
    , semitransbrush
    , new system.drawing.pointf(xcenterofimg, yposfrombottom)
    , strformat
    );
   system.drawing.image imgwatermark = new bitmap(waterimg);
   int imgwatermarkwidth = imgwatermark.width;
   int imgwatermarkheight = imgwatermark.height;
   //计算水印图片尺寸
   decimal ascale = convert.todecimal(imgiscale);
   decimal pscale = 0.05m;
   decimal minscale = ascale - pscale;
   decimal maxscale = ascale + pscale;
   int imgwatermarkwidthnew = imgwatermarkwidth;
   int imgwatermarkheightnew = imgwatermarkheight;
   if (imgphotowidth >= imgwatermarkwidth && imgphotoheight >= imgwatermarkheight && imgphotowidth >= imgphotoheight)
    if (imgwatermarkwidth > imgwatermarkheight)
     if ((minscale <= math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7)) && (math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7) <= maxscale))
     {
     }
     else
     {
      imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
      imgwatermarkheightnew = convert.toint32((imgphotowidth * ascale / imgwatermarkwidth) * imgwatermarkheight);
     }
    else
     if ((minscale <= math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7)) && (math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7) <= maxscale))
     {
     }
     else
     {
      imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
      imgwatermarkwidthnew = convert.toint32((imgphotoheight * ascale / imgwatermarkheight) * imgwatermarkwidth);
     }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgwatermarkwidth >= imgwatermarkheight)
   {
    imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
    imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
   }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight <= imgphotoheight && imgphotowidth >= imgphotoheight)
   {
    imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
    imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
   }
   if (imgwatermarkwidth <= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgphotowidth >= imgphotoheight)
   {
    imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
    imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
   }
   if (imgphotowidth >= imgwatermarkwidth && imgphotoheight >= imgwatermarkheight && imgphotowidth <= imgphotoheight)
    if (imgwatermarkwidth > imgwatermarkheight)
     if ((minscale <= math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7)) && (math.round((convert.todecimal(imgwatermarkwidth) / convert.todecimal(imgphotowidth)), 7) <= maxscale))
     {
     }
     else
     {
      imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
      imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
     }
    else
     if ((minscale <= math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7)) && (math.round((convert.todecimal(imgwatermarkheight) / convert.todecimal(imgphotoheight)), 7) <= maxscale))
     {
     }
     else
     {
      imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
      imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
     }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgwatermarkwidth <= imgwatermarkheight)
   {
    imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
    imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
   }
   if (imgwatermarkwidth >= imgphotowidth && imgwatermarkheight <= imgphotoheight && imgphotowidth <= imgphotoheight)
   {
    imgwatermarkwidthnew = convert.toint32(imgphotowidth * ascale);
    imgwatermarkheightnew = convert.toint32(((imgphotowidth * ascale) / imgwatermarkwidth) * imgwatermarkheight);
   }
   if (imgwatermarkwidth <= imgphotowidth && imgwatermarkheight >= imgphotoheight && imgphotowidth <= imgphotoheight)
   {
    imgwatermarkheightnew = convert.toint32(imgphotoheight * ascale);
    imgwatermarkwidthnew = convert.toint32(((imgphotoheight * ascale) / imgwatermarkheight) * imgwatermarkwidth);
   }
   //将原图画出来
   bitmap bmwatermark = new bitmap(bmphoto);
   bmwatermark.setresolution(imgphoto.horizontalresolution, imgphoto.verticalresolution);
   graphics gwatermark = graphics.fromimage(bmwatermark);
   //指定高质量显示水印图片质量
   gwatermark.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
   gwatermark.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
   system.drawing.imaging.imageattributes imageattributes = new system.drawing.imaging.imageattributes();
   //设置两种颜色,达到合成效果
   system.drawing.imaging.colormap colormap = new system.drawing.imaging.colormap();
   colormap.oldcolor = color.fromargb(255, 0, 255, 0);
   colormap.newcolor = color.fromargb(0, 0, 0, 0);
   system.drawing.imaging.colormap[] remaptable = { colormap };
   imageattributes.setremaptable(remaptable, system.drawing.imaging.coloradjusttype.bitmap);
   //用矩阵设置水印图片透明度
   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, imgalpha, 0.0f},
    new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
   };
   system.drawing.imaging.colormatrix wmcolormatrix = new system.drawing.imaging.colormatrix(colormatrixelements);
   imageattributes.setcolormatrix(wmcolormatrix, system.drawing.imaging.colormatrixflag.default, system.drawing.imaging.coloradjusttype.bitmap);
   //确定水印边距
   int xpos = imgphotowidth - imgwatermarkwidthnew;
   int ypos = imgphotoheight - imgwatermarkheightnew;
   int xposofwm = 0;
   int yposofwm = 0;
   if (xpos > intimgdistance)
    xposofwm = xpos - intimgdistance;
   else
    xposofwm = xpos;
   if (ypos > intimgdistance)
    yposofwm = ypos - intimgdistance;
   else
    yposofwm = ypos;
   gwatermark.drawimage(
     imgwatermark
    , new rectangle(xposofwm, yposofwm, imgwatermarkwidthnew, imgwatermarkheightnew)
    , 0
    , 0
    , imgwatermarkwidth
    , imgwatermarkheight
    , graphicsunit.pixel
    , imageattributes
    );
   imgphoto = bmwatermark;
   //以jpg格式保存图片
   imgphoto.save(simgpath, system.drawing.imaging.imageformat.jpeg);
   //销毁对象
   gbmphoto.dispose();
   gwatermark.dispose();
   bmphoto.dispose();
   imgphoto.dispose();
   imgwatermark.dispose();
  }
  catch 
  {
  }
 }
 /**//// <summary>
 /// 缩略图
 /// </summary>
 /// <param name="imgfile">原图文件地址</param>
 /// <param name="simgpath">缩略图保存地址</param>
 /// <param name="resizewidth">缩略图宽度</param>
 /// <param name="resizeheight">缩略图高度</param>
 /// <param name="bgcolor">缩略图背景颜色,注意,背景颜色只能指定knowncolor中的值,如blue,red,green等</param>
 public bool zzsresizeimg( string imgfile , string simgpath , int resizewidth , int resizeheight , string bgcolor )
  {
  try
  {
   filestream fs = new filestream(imgfile, filemode.open);
   binaryreader br = new binaryreader(fs);
   byte[] bytes = br.readbytes((int)fs.length);
   br.close();
   fs.close();
   memorystream ms = new memorystream(bytes);
   system.drawing.image imgphoto = system.drawing.image.fromstream(ms);
   //system.drawing.image imgphoto = system.drawing.image.fromfile(imgfile);
   int imgphotowidth = imgphoto.width;
   int imgphotoheight = imgphoto.height;
   int startx = 0;
   int starty = 0;
   int newwidth = 0;
   int newheight = 0;
   if (imgphotowidth >= resizewidth && imgphotoheight >= resizeheight)
   {
    newwidth = resizewidth;
    newheight = convert.toint32(imgphotoheight * math.round(convert.todecimal(resizewidth) / convert.todecimal(imgphotowidth), 10));
    startx = 0;
    starty = (resizeheight - newheight) / 2;
   }
   if (resizewidth > imgphotowidth && resizeheight < imgphotoheight)
   {
    newheight = resizeheight;
    newwidth = convert.toint32(imgphotowidth * math.round(convert.todecimal(resizeheight) / convert.todecimal(imgphotoheight), 10));
    startx = (resizewidth - newwidth) / 2;
    starty = 0;
   }
   if (resizewidth < imgphotowidth && resizeheight > imgphotoheight)
   {
    newwidth = resizewidth;
    newheight = convert.toint32(imgphotoheight * math.round(convert.todecimal(resizewidth) / convert.todecimal(imgphotowidth), 10));
    startx = 0;
    starty = (resizeheight - newheight) / 2;
   }
   if (imgphotowidth < resizewidth && imgphotoheight < resizeheight)
   {
    newwidth = imgphotowidth;
    newheight = imgphotoheight;
    startx = (resizewidth - imgphotowidth) / 2;
    starty = (resizeheight - imgphotoheight) / 2;
   }
   //计算缩放图片尺寸
   bitmap bmphoto = new bitmap(resizewidth, resizeheight, system.drawing.imaging.pixelformat.format24bpprgb);
   bmphoto.setresolution(72, 72);
   graphics gbmphoto = graphics.fromimage(bmphoto);
   gbmphoto.clear(color.fromname(bgcolor));
   gbmphoto.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
   gbmphoto.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
   gbmphoto.drawimage(
     imgphoto
    , new rectangle(startx, starty, newwidth, newheight)
    , new rectangle(0, 0, imgphotowidth, imgphotoheight)
    , graphicsunit.pixel
    );
   bmphoto.save(simgpath, system.drawing.imaging.imageformat.jpeg);
   imgphoto.dispose();
   gbmphoto.dispose();
   bmphoto.dispose();
   ms.close();
   return true;
  }
  catch 
  {
   return false;
  }
 }
 /**//// <summary>
 /// 图片剪切
 /// </summary>
 /// <param name="imgfile">原图文件地址</param>
 /// <param name="simgpath">缩略图保存地址</param>
 /// <param name="pointx">剪切起始点 x坐标</param>
 /// <param name="pointy">剪切起始点 y坐标</param>
 /// <param name="cutwidth">剪切宽度</param>
 /// <param name="cutheight">剪切高度</param>
 public bool zzscutimg(string imgfile, string simgpath, int pointx, int pointy, int cutwidth, int cutheight)
 {
  filestream fs = new filestream(imgfile, filemode.open);
  binaryreader br = new binaryreader(fs);
  try
  {
   byte[] bytes = br.readbytes((int)fs.length);
   br.close();
   fs.close();
   memorystream ms = new memorystream(bytes);
   system.drawing.image imgphoto = system.drawing.image.fromstream(ms);
   //system.drawing.image imgphoto = system.drawing.image.fromfile(imgfile);
   //此处只能用filestream,用 system.drawing.image则会报多过进程访问文件的错误,会锁定文件
   bitmap bmphoto = new bitmap(cutwidth, cutheight, system.drawing.imaging.pixelformat.format24bpprgb);
   bmphoto.setresolution(72, 72);
   graphics gbmphoto = graphics.fromimage(bmphoto);
   gbmphoto.interpolationmode = system.drawing.drawing2d.interpolationmode.high;
   gbmphoto.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
   gbmphoto.drawimage(
     imgphoto
    , new rectangle(0, 0, cutwidth, cutheight)
    , new rectangle(pointx, pointy, cutheight, cutheight)
    , graphicsunit.pixel
    );
   bmphoto.save(simgpath, system.drawing.imaging.imageformat.jpeg);
   imgphoto.dispose();
   gbmphoto.dispose();
   bmphoto.dispose();
   ms.close();
   return true;
  }
  catch
  {
   return false;
  }
  finally
  {
  }
 }
}

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网