加入收藏 | 设为首页 | 会员中心 | 我要投稿 应用网_常德站长网 (https://www.0736zz.com/)- 媒体处理、CDN、边缘计算、网络安全、物联网!
当前位置: 首页 > 综合聚焦 > 编程要点 > 语言 > 正文

C# 添加图片水印类的方法介绍

发布时间:2023-06-08 08:00:45 所属栏目:语言 来源:互联网
导读:   C# 添加图片水印类的方法,它举例分析了图像中添加水印的方法,同时也分析了C#的相关技术,有这方面需要的朋友可以关注错新技术频道发表出的文章,希望对你学习有帮助!



  复制
  C# 添加图片水印类的方法,它举例分析了图像中添加水印的方法,同时也分析了C#的相关技术,有这方面需要的朋友可以关注错新技术频道发表出的文章,希望对你学习有帮助!
 
  复制代码 代码如下:
 
  using System;
 
  using System.Collections.Generic;
 
  using System.Text;
 
  using System.Drawing;
 
  using System.IO;
 
  using System.Drawing.Imaging;
 
  using System.Web;
 
  using System.Drawing.Drawing2D;
 
  using System.Reflection;
 
  namespace Chen
 
  {
 
  public class warterPic
 
  {
 
  ///
 
  /// 给图片上水印
 
  ///
 
  ///原图片地址
 
  ///水印图片地址
 
  ///
 
  public void markwater(string filepath, string waterfile)
 
  {
 
  //gif不水印
 
  int i = filepath.LastIndexOf(".");
 
  string ex = filepath.Substring(i, filepath.Length - i);
 
  if (string.Compare(ex, ".gif", true) == 0)
 
  {
 
  return;
 
  }
 
  string modifyimagepath = filepath;//修改的图像路径
 
  int lucencypercent = 25;
 
  Image modifyimage = null;
 
  Image drawedimage = null;
 
  Graphics g = null;
 
  try
 
  {
 
  //建立图形对象
 
  modifyimage = Image.FromFile(modifyimagepath, true);
 
  drawedimage = Image.FromFile(waterfile, true);
 
  g = Graphics.FromImage(modifyimage);
 
  //获取要绘制图形坐标
 
  int x = modifyimage.Width - drawedimage.Width;
 
  int y = modifyimage.Height - drawedimage.Height; //设置颜色矩阵
 
  float[][] matrixitems ={ new float[] { 1, 0, 0, 0, 0 }, new float[] { 0, 1, 0, 0, 0 }, new float[] { 0, 0, 1, 0, 0 }, new float[] { 0, 0, 0, (float)lucencypercent / 100f, 0 }, new float[] { 0, 0, 0, 0, 1 } };
 
  ColorMatrix colormatrix = new ColorMatrix(matrixitems);
 
  ImageAttributes imgattr = new ImageAttributes();
 
  imgattr.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //绘制阴影图像
 
  g.DrawImage(drawedimage, new Rectangle(x, y, drawedimage.Width, drawedimage.Height), 10, 10, drawedimage.Width, drawedimage.Height, GraphicsUnit.Pixel, imgattr); //保存文件
 
  string[] allowimagetype ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
 
  FileInfo fi = new FileInfo(modifyimagepath);
 
  ImageFormat imagetype = ImageFormat.Gif;
 
  switch (fi.Extension.ToLower())
 
  {
 
  case ".jpg":
 
  imagetype = ImageFormat.Jpeg;
 
  break;
 
  case ".gif":
 
  imagetype = ImageFormat.Gif;
 
  break;
 
  case ".png":
 
  imagetype = ImageFormat.Png;
 
  break;
 
  case ".bmp":
 
  imagetype = ImageFormat.Bmp;
 
  break;
 
  case ".tif":
 
  imagetype = ImageFormat.Tiff;
 
  break;
 
  case ".wmf":
 
  imagetype = ImageFormat.Wmf;
 
  break;
 
  case ".ico":
 
  imagetype = ImageFormat.Icon;
 
  break;
 
  default: break;
 
  }
 
  MemoryStream ms = new MemoryStream();
 
  modifyimage.Save(ms, imagetype);
 
  byte[] imgdata = ms.ToArray();
 
  modifyimage.Dispose();
 
  drawedimage.Dispose();
 
  g.Dispose();
 
  FileStream fs = null;
 
  //File.Delete(modifyimagepath);
 
  fs = new FileStream(modifyimagepath, FileMode.Create, FileAccess.Write);
 
  if (fs != null)
 
  {
 
  fs.Write(imgdata, 0, imgdata.Length);
 
  fs.Close();
 
  }
 
  }
 
  finally
 
  {
 
  try
 
  {
 
  drawedimage.Dispose();
 
  modifyimage.Dispose();
 
  g.Dispose();
 
  }
 
  catch
 
  { }
 
  }
 
  }
 
  }
 
  }
 

(编辑:应用网_常德站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!