zoukankan      html  css  js  c++  java
  • C# asp.net上传图片加水印文字(个人心得)

      今天与大家分享一下"C# asp.net上传图片加水印文字",没什么技术含量,我也没装13,但是还是希望如果转载的话请注明作者和出处,谢谢!

      图片加文字水印的文章到百度或者谷歌上去一搜一大堆,但是貌似有用的没多少,很多都是一个版本,其他的都是转载或者分享的,很多情况下都不能满足我们自己的需求,今天我正好要用到图片加水印这方面知识,于是我整理了一下我的心得,仅供大家参考,大虾们跳过,我是菜鸟,哈哈~~

      下面开讲:

      第一步:首先我想的是原理

      因为使用微博的原因,大家都注意到了,微博上分图片在右下角都会加上你的微博地址,而且无论图片是大是小,水印都会加到右下角位置,根据这一想法,我画了一张图,直观一点解释一下...如图所示:

      从图片上我们可以很直观明了的看出这里面的原理了,所以我们代码要解决的问题就是获取图片的宽度和高度,以及根据字体大小以及文字的个数计算出所话区域的长度和高度,这样就很简单了,呵呵...

      第二步:写代码、测试代码

      过程我就省略了,直接看代码吧:  

    AddWaterText
     /// <summary>
            /// 图片加水印文字
            /// </summary>
            /// <param name="oldpath">旧图片地址</param>
            /// <param name="text">水印文字</param>
            /// <param name="newpath">新图片地址</param>
            /// <param name="Alpha">透明度</param>
            /// <param name="fontsize">字体大小</param>
            public static void AddWaterText(string oldpath, string text, string newpath, int Alpha, int fontsize)
            {
                try
                {
                    text = text + "版权所有";
                    FileStream fs = new FileStream(oldpath, 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);
                    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);
                    //gif背景色
                    gbmPhoto.Clear(Color.FromName("white"));
                    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);
                    System.Drawing.Font font = null;
                    System.Drawing.SizeF crSize = new SizeF();
                    font = new Font("宋体", fontsize, FontStyle.Bold);
                    //测量指定区域
                    crSize = gbmPhoto.MeasureString(text, font);
                    float y = imgPhotoHeight - crSize.Height;
                    float x = imgPhotoWidth - crSize.Width;
                    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, 56, 56, 56));
                    gbmPhoto.DrawString(text, font, semiTransBrush2, x + 1, y + 1);
    
                    System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(Color.FromArgb(Alpha, 176, 176, 176));
                    gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
                    bmPhoto.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    gbmPhoto.Dispose();
                    imgPhoto.Dispose();
                    bmPhoto.Dispose();
                }
                catch
                {                
                    ;                
                }
            }

      代码我就不解释了,基本上在原理中都解释过了。另:关于参数的设置,我们还可以加上是什么字体、字体颜色、是否加粗,以及定位到什么位子等,这样就封装的更好了,由于我现在只需要把水印加到右下角,所以我就没有再写了,呵呵...原理一样,封装也很好封装,留给你们写吧,哈哈...

      下面看效果图吧,因为图是最直观的展示了...

      上面就是我随便截了张图的效果图。

      调用方法:命名空间.类名.AddWaterText(picpath, "Tandy Tang - 博客园", picpath, 255, 18);

      如果你觉得对你有帮助的话,别忘了点推荐哦~~

      PS:Tandy Tang祝大家写代码写的愉快!

  • 相关阅读:
    注解之------@WebService
    注解之----@component,@controller,@service ,@repository简介
    Java 之 I/O
    标签之----@Override
    注解之----@Resource
    弹指之间 -- 简谱
    弹指之间 -- Prerequisites
    MVC5 + EF6 完整入门教程三
    MVC5 + EF6 入门完整教程二
    MVC5 + EF6 入门完整教程
  • 原文地址:https://www.cnblogs.com/tandyshen/p/picwater.html
Copyright © 2011-2022 走看看