c# 使用Graphic 为图片添加水印 及文字



c# 使用Graphic 为图片添加水印 及文字

一、图片上加文字:
//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();  

二、图片上加水印:
 //using System;   
//using System.Web;   
//using System.Drawing;   
//using System.Drawing.Drawing2D;   
//using System.Drawing.Imaging;   
//using System.IO;   
//using System.Reflection;   
 
/// <summary>   
/// 给图片上水印   
/// </summary>   
/// <param name="filePath">原图片地址</param>   
/// <param name="waterFile">水印图片地址</param>   
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 = BasePath + filePath;//修改的图像路径   
    int lucencyPercent=25;   
    Image modifyImage=null;   
    Image drawedImage=null;   
    Graphics g=null;   
    try   
    {   
        //建立图形对象   
        modifyImage=Image.FromFile(ModifyImagePath,true);   
        drawedImage=Image.FromFile(BasePath + 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 
        {  
        }   
   }   
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值