C # using the overlap of original artwork and watermark to realize watermark simply _c# tutorial

Source: Internet
Author: User

The example of this article is about C # using the original image and watermark map to realize the simple watermark method. Share to everyone for your reference, specific as follows:

Picture Action Class

<summary>///Gets the size of a picture scaled down by equal proportions. </summary>///<param name= "maxwidth" > need to be narrowed to the width </param>///<param name= "MaxHeight" > The height needed to be narrowed </param>///<param name= "Imageoriginalwidth" > The original width of the picture </param>///<param name= " Imageoriginalheight "> Original height </param>///<returns> returns the actual size of the picture by equal proportions </returns> public static
  Size getnewsize (int maxwidth, int maxheight, int imageoriginalwidth, int imageoriginalheight) {Double w = 0.0;
  Double h = 0.0;
  Double SW = convert.todouble (imageoriginalwidth);
  Double sh = convert.todouble (imageoriginalheight);
  Double MW = convert.todouble (maxwidth);
  Double MH = convert.todouble (maxheight);
    if (SW < MW && SH < MH) {w = SW;
  h = sh;
    else if ((sw/sh) > (MW/MH)) {w = maxwidth;
  H = (W * sh)/SW;
    else {h = maxheight;
  W = (H * sw)/sh;
return new Size (Convert.ToInt32 (w), Convert.ToInt32 (h)); }///<summary>///for a givenA picture (Image object) generates a thumbnail of the specified size. </summary>///<param name= "Originalimage" > Original picture </param>///<param name= "Thummaxwidth" > Thumbnail width </param>///<param name= "thummaxheight" > Thumbnail height </param>///<returns> Returns the image object of the thumbnail </returns> public static System.Drawing.Image getthumbnailimage (System.Drawing.Image originalimage, int
  thummaxwidth, int thummaxheight) {Size thumrealsize = Size.empty;
  System.Drawing.Image newimage = originalimage;
  Graphics Graphics = null;
    try {thumrealsize = getnewsize (Thummaxwidth, Thummaxheight, Originalimage.width, originalimage.height);
    NewImage = new Bitmap (thumrealsize.width, thumrealsize.height);
    Graphics = Graphics.fromimage (newimage);
    graphics.compositingquality = compositingquality.highquality; Graphics.
    Interpolationmode = Interpolationmode.highqualitybicubic; Graphics.
    SmoothingMode = smoothingmode.highquality; Graphics.
    Clear (color.transparent); Graphics. DrawImage (OriginaliMage, New Rectangle (0, 0, Thumrealsize.width, thumrealsize.height), new Rectangle (0, 0, Originalimage.width,
  originalimage.height), GraphicsUnit.Pixel); The catch {} finally {if (graphics!= null) {graphics.
      Dispose ();
    graphics = null;
} return newimage;
///<summary>///generates a thumbnail of the specified size for a given picture file. </summary>///<param name= "originalimage" > Physical file Address of the picture </param>///<param name= "Thummaxwidth" > Thumbnail width </param>///<param name= "thummaxheight" > Thumbnail height </param>///<returns> Returns the image object </returns> public static System.Drawing.Image getthumbnailimage of the thumbnail (string imagefile, int thummaxwidth
  , int thummaxheight) {System.Drawing.Image originalimage = null;
  System.Drawing.Image newimage = null;
    try {originalimage = System.Drawing.Image.FromFile (ImageFile);
  NewImage = Getthumbnailimage (Originalimage, Thummaxwidth, thummaxheight);
  The catch {} finally {if (originalimage!= null)  {Originalimage.dispose ();
    Originalimage = null;
} return newimage;
///<summary>///generates a thumbnail of the specified size for a given picture file and saves the thumbnail to the specified location. </summary>///<param name= "Originalimagefile" > Physical file Address of the picture </param>///<param name= " Thumbnailimagefile "> Thumbnail physical file address </param>///<param name=" thummaxwidth "> Thumbnail width </param>///< param name= "thummaxheight" > Thumbnail height </param> public static void Makethumbnail (String originalimagefile, String thumbnailimagefile, int thummaxwidth, int thummaxheight) {System.Drawing.Image newimage = getthumbnailimage (Originalim
  Agefile, Thummaxwidth, thummaxheight);
  try {newimage.save (thumbnailimagefile, imageformat.jpeg);
    Catch {} finally {newimage.dispose ();
  NewImage = null;
}///<summary>///adjusts the memory flow of a picture to the specified size and returns the adjusted memory stream. </summary>///<param name= "Originalimagestream" > The memory stream of the original picture </param>///<param name= "Newwidth" > Width of new picture </param>///<param name= "Newheight" > New picture height </param>///<returns> return adjusted picture's memory stream </returns> public static
  MemoryStream resizeimage (Stream originalimagestream, int newwidth, int newheight) {MemoryStream = null; System.Drawing.Image newimage = Globals.getthumbnailimage (System.Drawing.Image.FromStream (Originalimagestream),
  Newwidth, Newheight);
    if (newimage!= null) {Newimagestream = new MemoryStream ();
  Newimage.save (Newimagestream, imageformat.jpeg);
return newimagestream;
///<summary>///saves a memory stream as a disk file. </summary>///<param name= "stream" > Memory stream </param>///<param name= "newFile" > Destination disk File Address </ param> public static void Savestreamtofile (Stream stream, string newFile) {if (stream = null | |) stream. Length = = 0 | | String.
  IsNullOrEmpty (NewFile)) {return; } byte[] buffer = new Byte[stream.
  Length]; Stream.
  Position = 0; Stream. Read (buffer, 0, buffer.)
  Length); FileStream FileStream = new FilestrEAM (NewFile, FileMode.OpenOrCreate, FileAccess.Write); FileStream.Write (buffer, 0, buffer.
  Length);
  Filestream.flush ();
  Filestream.close ();
Filestream.dispose ();
///<summary>///Adds a picture watermark effect to a specified picture. </summary>///<param name= "imagefile" > Picture file Address </param>///<param name= "Waterimage" > Watermark Picture (Image object) </param> public static void Createimagewatermark (String imagefile, System.Drawing.Image waterimage ) {if (string. IsNullOrEmpty (imagefile) | | ! File.exists (imagefile) | |
  Waterimage = = null) {return;
  } System.Drawing.Image originalimage = System.Drawing.Image.FromFile (ImageFile);
  if (originalimage.width-10 < Waterimage.width | | | originalimage.height-10 < waterimage.height) {return;
  } Graphics Graphics = Graphics.fromimage (originalimage);
  int x = originalimage.width-waterimage.width-10;
  int y = originalimage.height-waterimage.height-10;
  int width = waterimage.width;
  int height = waterimage.height; GraPhics.
  DrawImage (Waterimage, New Rectangle (x, y, width, height), 0, 0, width, height, graphicsunit.pixel); Graphics.
  Dispose ();
  MemoryStream stream = new MemoryStream ();
  Originalimage.save (stream, imageformat.jpeg);
  Originalimage.dispose ();
  System.Drawing.Image imagewithwater = System.Drawing.Image.FromStream (stream);
  Imagewithwater.save (ImageFile);
Imagewithwater.dispose ();
///<summary>///A text watermark effect on a specified picture. </summary>///<param name= "imagefile" > Picture file Address </param>///<param name= "Watertext" > Watermark text content </param> public static void Createtextwatermark (String imagefile, String watertext) {if (string). IsNullOrEmpty (imagefile) | | String. IsNullOrEmpty (watertext) | | !
  File.exists (ImageFile)) {return;
  } System.Drawing.Image originalimage = System.Drawing.Image.FromFile (ImageFile);
  Graphics Graphics = Graphics.fromimage (originalimage); Graphics.
  SmoothingMode = smoothingmode.highquality; Graphics. TextRenderingHint = TextrenderinghInt.
  Cleartypegridfit;
  graphics.compositingquality = compositingquality.highquality; Graphics.
  Interpolationmode = Interpolationmode.highqualitybicubic;
  SolidBrush brush = new SolidBrush (Color.FromArgb (153, 255, 255, 255));
  Font watertextfont = new Font ("Arial", fontstyle.regular); SizeF watertextsize = Graphics.
  MeasureString (Watertext, Watertextfont);
  float x = (float) originalimage.width-watertextsize.width-10f;
  Float y = (float) originalimage.height-watertextsize.height-10f; Graphics.
  DrawString (Watertext, Watertextfont, brush, x, y); Graphics.
  Dispose (); Brush.
  Dispose ();
  MemoryStream stream = new MemoryStream ();
  Originalimage.save (stream, imageformat.jpeg);
  Originalimage.dispose ();
  System.Drawing.Image imagewithwater = System.Drawing.Image.FromStream (stream);
  Imagewithwater.save (ImageFile);
Imagewithwater.dispose ();
///<summary>///Determine if the upload component contains content. </summary>///<param name= "FileUpload" >asp.net 2.0 standard Upload components </param>///<returns> Returns True if the data is valid, otherwise returns false</returns> public static bool Isattachmentvalid (FileUpload fileupload {if (fileupload!= null && fileupload.postedfile!= null &&!string.) IsNullOrEmpty (fileUpload.PostedFile.FileName) && fileUpload.PostedFile.ContentLength > 0) {return TR
  Ue
return false;

 }
public class Imagehelper {#region "watermark stored relative path" public static string Getlogopath () {return "/images/logo.png"  ; Watermark Map Path} #endregion #region "picture watermark"//<summary>//Image watermark on the picture, this method does not support GIF-type pictures//</summary>/ /<param name= "path" > Original server Picture path </param>//<param name= "PATH_SYP" > generated photo watermark with picture path </param>//< param name= "PATH_SYPF" > Watermark picture path </param> public static void Markimage (Stream inuploadimagepath, string Inlogoimagepath, String insavepath) {System.Drawing.Image Image = System.Drawing.Image.FromStream (inuploadimagepath
    );
    System.Drawing.Image newimage = Image.FromFile (Current.Server.MapPath (Inlogoimagepath));
    Graphics g = graphics.fromimage (Image); G.drawimage (NewImage, New Rectangle (Image.width-newimage). Width, Image.height-newimage. Height, NewImage. Width, NewImage. Height), 0, 0, NewImage. Width, NewImage.
    Height, GraphicsUnit.Pixel); try {image.save (Current.Server.MapPath (Insavepath));
    The catch (Exception ex) {} finally {g.dispose ();
      Image.dispose (); NewImage.
    Dispose ();

 }} #endregion}

Read more about C # Interested readers can view the site topics: "C # Picture Operation Tips Summary", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # Common control usage Tutorial", "C # object-oriented Program design Introductory Course" and "C # A summary of thread usage tips for programming

I hope this article will help you with C # programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.