Home>
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 description
///</summary>
public class imgwater
{
 public imgwater ()
 {
  //
  //todo:add constructor logic here
  //
 }
 ///<summary>
 ///picture watermark
 ///</summary>
 ///<param name="imgfile">Original image file address</param>
 ///<param name="waterimg">watermark picture address</param>
 ///<param name="simgpath">watermark image save address</param>
 ///<param name="alpha">Watermark transparency setting</param>
 ///<param name="iscale">display ratio of watermark picture on original image</param>
 ///<param name="intdistance">the margin of the watermark picture on the original image is determined,Based on the right and bottom of the picture,When the set margin exceeds a certain size, the parameter will automatically fail</param>
 public bool zzsimgwater (
   string imgfile
  , string waterimg
  , string simgpath
  , float alpha
  , float iscale
  , int intdistance
  )
  {
  try
  {
   //Load the picture
   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;
   //Calculate watermark image size
   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);
   }
   //Export the original picture
   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);
   //Specify high-quality display watermark picture quality
   gwatermark.interpolationmode=system.drawing.drawing2d.interpolationmode.high;
   gwatermark.smoothingmode=system.drawing.drawing2d.smoothingmode.highquality;
    imageattributes imageattributes=new imageattributes ();
   //Set two colors,Achieve synthesis effect
   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);
     //Set the watermark image transparency with a matrix
   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);
   //Determine watermark margins
   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;
   //Save the picture in jpg format
   imgphoto.save (simgpath, system.drawing.imaging.imageformat.jpeg);
   //Destroy the object
   gbmphoto.dispose ();
   gwatermark.dispose ();
   imgphoto.dispose ();
   imgwatermark.dispose ();
   return true;
  }
  catch
  {
   return false;
  }
 }
 /** ////<summary>
 ///text watermark
 ///</summary>
 ///<param name="imgfile">Original image file address</param>
 ///<param name="textfont">watermark text</param>
 ///<param name="simgpath">text watermark picture save address</param>
 ///<param name="hscale">height position</param>
 ///¶m name="widthfont"
 ///<param name="alpha">text transparency, whose value ranges from 0 to 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
    );
   //Create an array of font sizes,Cycle through watermark fonts
   //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 ("Microsoft Yahei", sizes [i], fontstyle.bold);
    crsize=gbmphoto.measurestring (textfont, crfont);
    if ((ushort) crsize.width<(ushort) imgphotowidth * widthfont)
     break;
   }
   //Set the position of the watermark font
   //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>
 ///text and logo image watermark
 ///</summary>
 ///<param name="imgfile">Original image file address</param>
 ///<param name="waterimg">watermark picture address</param>
 ///<param name="textfont">watermark text information</param>
 ///<param name="simgpath">save address after surviving watermark picture</param>
 ///<param name="imgalpha">transparency of watermark image</param>
 ///<param name="imgiscale">display ratio of watermark picture on original image</param>
 ///<param name="intimgdistance">the margin of the watermark picture on the original image is determined,Based on the right and bottom of the picture,When the set margin exceeds a certain size, the parameter will automatically fail</param>
 ///<param name="texthscale">watermark text height position,Counting from the bottom of the picture,0-1</param>
 ///¶m name="textwidthfont">width ratio of text block in image 0-1</param>
 ///<param name="textalpha">text transparency, whose value ranges from 0 to 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
    );
   //Create an array of font sizes,Cycle through watermark fonts
   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;
   }
   //Set the position of the watermark font
   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;
   //Calculate watermark image size
   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);
   }
   //Export the original picture
   bitmap bmwatermark=new bitmap (bmphoto);
   bmwatermark.setresolution (imgphoto.horizontalresolution, imgphoto.verticalresolution);
   graphics gwatermark=graphics.fromimage (bmwatermark);
   //Specify high-quality display watermark picture quality
   gwatermark.interpolationmode=system.drawing.drawing2d.interpolationmode.high;
   gwatermark.smoothingmode=system.drawing.drawing2d.smoothingmode.highquality;
   system.drawing.imaging.imageattributes imageattributes=new system.drawing.imaging.imageattributes ();
   //Set two colors,Achieve synthesis effect
   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);
   //Set the watermark image transparency with a matrix
   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);
   //Determine watermark margins
   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;
   //Save the picture in jpg format
   imgphoto.save (simgpath, system.drawing.imaging.imageformat.jpeg);
   //Destroy the object
   gbmphoto.dispose ();
   gwatermark.dispose ();
   bmphoto.dispose ();
   imgphoto.dispose ();
   imgwatermark.dispose ();
  }
  catch
  {
  }
 }
 /** ////<summary>
 ///thumbnail
 ///</summary>
 ///<param name="imgfile">Original image file address</param>
 ///<param name="simgpath">thumbnail save address</param>
 ///<param name="resizewidth">thumbnail width</param>
 ///<param name="resizeheight">thumbnail height</param>
 ///<param name="bgcolor">thumbnail background color,Note that the background color can only specify values ​​in knowncolor, such as blue, red, green, etc.</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;
   }
   //Calculate the scaled image size
   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>
 ///picture cut
 ///</summary>
 ///<param name="imgfile">Original image file address</param>
 ///<param name="simgpath">thumbnail save address</param>
 ///<param name="pointx">clipping start point x coordinate</param>
 ///<param name="pointy">clipping start point y coordinate</param>
 ///<param name="cutwidth">cut width</param>
 ///<param name="cutheight">cut height</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);
   //You can only use filestream here. Using system.drawing.image will report more errors than processes accessing the file.
Will lock the file
   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
  • Previous Talking about onbeforeunload and onunload events in javascript
  • Next Detailed PHP method to obtain image information