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;
            }
    
  • 相关阅读:
    bzoj2298 [HAOI2011]problem a
    P5504 [JSOI2011]柠檬
    洛谷P4383 [八省联考2018]林克卡特树
    [USACO17DEC]Standing Out from the Herd
    bzoj3926: [Zjoi2015]诸神眷顾的幻想乡
    dtoj4680. 红黑兔
    dtoj2099. 字符串查询( find)
    dtoj1721. 字符串生成器 ( strgen )
    dtoj4542. 「TJOI / HEOI2016」字符串
    loj2278. 「HAOI2017」字符串
  • 原文地址:https://www.cnblogs.com/wucg/p/1911465.html
Copyright © 2011-2022 走看看