zoukankan      html  css  js  c++  java
  • Water Marked 水印

    from : Pro ASPnetMVCFramework

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    
    namespace TestMvcWebApp1.ActionResultEx
    {
        public class WatermarkedImageResult : ActionResult
        {
            public WatermarkedImageResult() { }
            public string ImageFileName { get; private set; }
            public string WatermarkText { get; private set; }
            public WatermarkedImageResult(string imageFileName, string watermarkText)
            {
                ImageFileName = imageFileName;
                WatermarkText = watermarkText;
            }
            public override void ExecuteResult(ControllerContext context)
            {
                using (var image = Image.FromFile(ImageFileName))
                using (var graphics = Graphics.FromImage(image))
                using (var font = new Font("Arial", 10))
                using (var memoryStream = new MemoryStream())
                {
                    // Render the watermark text in bottom-left corner
                    var textSize = graphics.MeasureString(WatermarkText, font);
                    graphics.DrawString(WatermarkText, font, Brushes.Black, 10,
                        image.Height - textSize.Height - 10);
                    // Transmit the image in PNG format (note: must buffer it in
                    // memory first due to GDI+ limitation)284 CHAPTER 9 n CONTROLLERS AND ACTIONS
                    image.Save(memoryStream, ImageFormat.Png);
                    var response = context.RequestContext.HttpContext.Response;
                    response.ContentType = "image/png";
                    response.BinaryWrite(memoryStream.GetBuffer());
                }
            }
        }
    }
    
    

    使用:

     public ActionResult ViewmarkedImage()
            {
    
                string ImageFileName = @"C:\Documents and Settings\Administrator\My Documents\My Pictures\oem.gif";
                string WatermarkText = "这是我的水印";
                WatermarkedImageResult result = new WatermarkedImageResult(ImageFileName, WatermarkText);
                return result;
            }
    
  • 相关阅读:
    centos7使用supermin制作centos7的docker镜像包
    Linux ip netns 命令
    ip命令讲解
    openstack API应用用
    在EF6.0中打印数据库操作日志
    EF记录统一添加创建,修改时间
    Inner Join and Left Join 与条件的结合
    字符串分割
    居中方案
    移动 前端 框架
  • 原文地址:https://www.cnblogs.com/wucg/p/1911465.html
Copyright © 2011-2022 走看看