C# GDI+ 简单实现图片写文字和图片叠加(水印)(转)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

namespace Web_PictureWaterMark
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.imgSource.ImageUrl = @"~/Image/Bear.bmp";
        }

        //图片叠加
        protected void btn_WaterMark_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath(@"image/Bear.bmp");
            System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);
            System.Drawing.Image imgWarter = System.Drawing.Image.FromFile(Server.MapPath(@"Image/Water.bmp"));
            using (Graphics g = Graphics.FromImage(imgSrc))
            {
                g.DrawImage(imgWarter, new Rectangle(imgSrc.Width - imgWarter.Width,
                                                 imgSrc.Height - imgWarter.Height,
                                                 imgWarter.Width,
                                                 imgWarter.Height),
                        0, 0, imgWarter.Width, imgWarter.Height, GraphicsUnit.Pixel);
            }   

            string newpath = Server.MapPath(@"Image/WaterMark.bmp");
            imgSrc.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
            this.image_Water.ImageUrl = @"~/Image/WaterMark.bmp";          

        }

        //图片写字
        protected void btn_FontMark_Click(object sender, EventArgs e)
        {
            string path = Server.MapPath(@"image/Bear.bmp");
            System.Drawing.Image imgSrc = System.Drawing.Image.FromFile(path);

            using (Graphics g = Graphics.FromImage(imgSrc))
            {
                g.DrawImage(imgSrc, 0, 0, imgSrc.Width, imgSrc.Height);
                using(Font f=new Font("宋体",20))
                {
                    using (Brush b = new SolidBrush(Color.Red))
                    {
                        string addText = "我的地盘我做主";
                        g.DrawString(addText, f, b, 100,20);
                    }
                }
            }
            string fontpath = Server.MapPath(@"image/FontMark.bmp");
            imgSrc.Save(fontpath, System.Drawing.Imaging.ImageFormat.Bmp);
            this.image_Font.ImageUrl = @"~/image/FontMark.bmp";          
         
        }
    }
}

————————————————————————————————————————————————————————————————————————————————————————————————————————

图片上传后,可以在上传的图片上面写上自己站点的网址,或者把自己网址的水印图片叠加到上传的的图片上来实现水印的效果。
利用C# GUI 实现起来简直是太容易了,代码如下:

.页面HTML代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Web_PictureWaterMark._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET C#为图片加水印</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:Image ID="imgSource" runat="server" ImageUrl="~/Image/Bear.bmp" />    
    </div>
    <div>
        <asp:Button ID="btn_WaterMark" runat="server" Text="为图片添加水印效果" 
            onclick="btn_WaterMark_Click" />
        <asp:Button ID="btn_FontMark" runat="server" Text="为图片添加文字效果" 
            onclick="btn_FontMark_Click" />
     </div>
     <div style="width:410px; float:left; margin:0px;">
         <asp:Image ID="image_Water" runat="server" />
    </div>
     <div style="width:410px; float:left; margin:0px;">
         <asp:Image ID="image_Font" runat="server" />
    </div>
    </form>
</body>
</html>

 

posted @ 2017-05-20 11:21  捡贝壳的孩子  阅读(7065)  评论(0编辑  收藏  举报