zoukankan      html  css  js  c++  java
  • 在Asp.net中为图像加入版权信息

    你试过用.net来进行图像处理吗?公司最近进行的一个项目,数据库中存入了很多的产品图片,可是都没有版权信息,当时客户要求将所有的图片打上公司名称?这时候,你该怎么办?

    虽说我还是江湖嫩手,可是对这一问题的解决方案还是很多,归结如下:

    1.使用图形处理软件,如Photoshop等,利用其批处理功能,可以实现这个功能,可是每次数据录入员加进图片的话,还得加工一下图片,很麻烦的,看录入员大姐平常对我很不错,每日笑脸相迎,能忍心折磨她么?这个方案否决了。

    2.使用.net行云流水般的图像处理,当录入员大姐上传图片的时候,就自动加上公司标记,这样岂不更好,嗯,这想法不错,可以进入2005最佳解决方案第万强了,好说干就干。

    using System.Drawing;
    using System.IO;
    using System.Drawing.Imaging;

            private void AddTextToImg(string fileName,string text)
            {
                if(!File.Exists(MapPath(fileName)))
                {
                    throw new FileNotFoundException("The file don't exist!");
                }
               
                if( text == string.Empty )
                {
                    return;
                }
                //还需要判断文件类型是否为图像类型,这里就不赘述了

                System.Drawing.Image image = System.Drawing.Image.FromFile(MapPath(fileName));
                Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
                Graphics g = Graphics.FromImage(bitmap);

                float fontSize = 12.0f;             //字体大小
                float textWidth = text.Length*fontSize;  //文本的长度
                //下面定义一个矩形区域,以后在这个矩形里画上白底黑字
                float rectX = 0;       
                float rectY = 0;
                float rectWidth = text.Length*(fontSize+8);
                float rectHeight = fontSize+8;
                //声明矩形域
                RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);

                Font font = new Font("宋体",fontSize);   //定义字体
                Brush whiteBrush = new SolidBrush(Color.White);   //白笔刷,画文字用
                Brush blackBrush = new SolidBrush(Color.Black);   //黑笔刷,画背景用

                g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);           

                g.DrawString(text,font,whiteBrush,textArea);
                MemoryStream ms = new MemoryStream( );
                //保存为Jpg类型
                bitmap.Save(ms,ImageFormat.Jpeg);

                //输出处理后的图像,这里为了演示方便,我将图片显示在页面中了
                Response.Clear();
                Response.ContentType = "image/jpeg";
                Response.BinaryWrite( ms.ToArray() );

                g.Dispose();
                bitmap.Dispose();
                image.Dispose();
            }

    调用时很简单,

    AddTextToImg("me.jpg","小智");

    申明

    非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

    博文欢迎转载,但请给出原文连接。

  • 相关阅读:
    定义 : angular view 和controller 之前的 ng-init 由谁来负责
    pl/sql如何在云端服务器上新建数据库
    oracle连接服务器上数据库
    svn check下来的代码在eclipse中没有run on server
    sql server在修改表结构后不能保存
    用mybatis-generator自动生成代码报“前言中不允许有内容”
    启动Tomact时报failed to start conponent
    spring MVC中controller失效
    maven项目中连接sqlserver和mysql的区别
    DBCP连接池的配置
  • 原文地址:https://www.cnblogs.com/Athrun/p/552026.html
Copyright © 2011-2022 走看看