欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#利用原图和水印图的重叠简单实现水印的方法

程序员文章站 2023-10-31 15:55:46
本文实例讲述了c#利用原图和水印图的重叠简单实现水印的方法。分享给大家供大家参考,具体如下: 图片操作类 /// /// 获取...

本文实例讲述了c#利用原图和水印图的重叠简单实现水印的方法。分享给大家供大家参考,具体如下:

图片操作类

/// <summary>
/// 获取一个图片按等比例缩小后的大小。
/// </summary>
/// <param name="maxwidth">需要缩小到的宽度</param>
/// <param name="maxheight">需要缩小到的高度</param>
/// <param name="imageoriginalwidth">图片的原始宽度</param>
/// <param name="imageoriginalheight">图片的原始高度</param>
/// <returns>返回图片按等比例缩小后的实际大小</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>
/// 对给定的一个图片(image对象)生成一个指定大小的缩略图。
/// </summary>
/// <param name="originalimage">原始图片</param>
/// <param name="thummaxwidth">缩略图的宽度</param>
/// <param name="thummaxheight">缩略图的高度</param>
/// <returns>返回缩略图的image对象</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);
  }
  catch { }
  finally
  {
    if (graphics != null)
    {
      graphics.dispose();
      graphics = null;
    }
  }
  return newimage;
}
/// <summary>
/// 对给定的一个图片文件生成一个指定大小的缩略图。
/// </summary>
/// <param name="originalimage">图片的物理文件地址</param>
/// <param name="thummaxwidth">缩略图的宽度</param>
/// <param name="thummaxheight">缩略图的高度</param>
/// <returns>返回缩略图的image对象</returns>
public static system.drawing.image getthumbnailimage(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);
  }
  catch { }
  finally
  {
    if (originalimage != null)
    {
      originalimage.dispose();
      originalimage = null;
    }
  }
  return newimage;
}
/// <summary>
/// 对给定的一个图片文件生成一个指定大小的缩略图,并将缩略图保存到指定位置。
/// </summary>
/// <param name="originalimagefile">图片的物理文件地址</param>
/// <param name="thumbnailimagefile">缩略图的物理文件地址</param>
/// <param name="thummaxwidth">缩略图的宽度</param>
/// <param name="thummaxheight">缩略图的高度</param>
public static void makethumbnail(string originalimagefile, string thumbnailimagefile, int thummaxwidth, int thummaxheight)
{
  system.drawing.image newimage = getthumbnailimage(originalimagefile, thummaxwidth, thummaxheight);
  try
  {
    newimage.save(thumbnailimagefile, imageformat.jpeg);
  }
  catch
  { }
  finally
  {
    newimage.dispose();
    newimage = null;
  }
}
/// <summary>
/// 将一个图片的内存流调整为指定大小,并返回调整后的内存流。
/// </summary>
/// <param name="originalimagestream">原始图片的内存流</param>
/// <param name="newwidth">新图片的宽度</param>
/// <param name="newheight">新图片的高度</param>
/// <returns>返回调整后的图片的内存流</returns>
public static memorystream resizeimage(stream originalimagestream, int newwidth, int newheight)
{
  memorystream newimagestream = 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>
/// 将一个内存流保存为磁盘文件。
/// </summary>
/// <param name="stream">内存流</param>
/// <param name="newfile">目标磁盘文件地址</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>
/// 对一个指定的图片加上图片水印效果。
/// </summary>
/// <param name="imagefile">图片文件地址</param>
/// <param name="waterimage">水印图片(image对象)</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>
/// 对一个指定的图片加上文字水印效果。
/// </summary>
/// <param name="imagefile">图片文件地址</param>
/// <param name="watertext">水印文字内容</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", 16, 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>
/// 判断上传组件是否包含内容。
/// </summary>
/// <param name="fileupload">asp.net 2.0标准上传组件</param>
/// <returns>如果数据有效,则返回true,否则返回false</returns>
public static bool isattachmentvalid(fileupload fileupload)
{
  if (fileupload != null &&
    fileupload.postedfile != null &&
    !string.isnullorempty(fileupload.postedfile.filename) &&
    fileupload.postedfile.contentlength > 0)
  {
    return true;
  }
  return false;
}

public class imagehelper
{
  #region " 水印存放的相对路径 "
  public static string getlogopath()
  {
    return "/images/logo.png";  ///水印图路径
  }
  #endregion
  #region " 图片水印 "
  // <summary>
  // 在图片上生成图片水印,此方法不支持gif类型的图片
  // </summary>
  // <param name="path">原服务器图片路径</param>
  // <param name="path_syp">生成的带图片水印的图片路径</param>
  // <param name="path_sypf">水印图片路径</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));
    }
    catch (exception ex) {
    }
    finally {
      g.dispose();
      image.dispose();
      newimage.dispose();
    }
  }
  #endregion
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#图片操作技巧汇总》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#常见控件用法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

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