前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >逆天通用水印扩展篇~新增剪贴板系列的功能和手动配置,卸除原基础不常用的功能

逆天通用水印扩展篇~新增剪贴板系列的功能和手动配置,卸除原基础不常用的功能

作者头像
逸鹏
发布2018-04-10 11:21:58
6130
发布2018-04-10 11:21:58
举报
文章被收录于专栏:逸鹏说道逸鹏说道

常用技能:http://www.cnblogs.com/dunitian/p/4822808.html#skill

逆天博客:http://dnt.dkil.net

逆天通用水印支持Winform,WPF,Web,WP,Win10。支持位置选择(9个位置 ==》[X]):http://www.cnblogs.com/dunitian/p/4939369.html

本次添加了一些新东西,比如剪贴板之类的水印操作。完善了部分功能(比如文件过滤,非Bitmap图片的处理,以及一些其他玩意等待你的发现)

先贴下新增的效果:

单个图片水印

多文件直接水印

网页图片批量转

图片太大

word文档图片批量转

剪贴板图片水印

图片太大

自动化配置

图片太大

上篇重复的技术点我就不继续说了,这次主要贴一下剪贴板系列的code

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
namespace WaterMarkAPP.Common
{
    public class ClipboardHelper
    {
        /// <summary>
        /// 获取剪贴板里的图片
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<string> GetImagePathList()
        {
            var imgPathList = new List<string>();
            var data = Clipboard.GetDataObject();
            var formats = data.GetFormats();
            //二进制存储 (存储在剪贴板的截图|画画图内的图片)
            if (data.GetDataPresent(DataFormats.Dib, true))
            {
                var imgSorce = Clipboard.GetImage();
                Bitmap bmp = new Bitmap(imgSorce.PixelWidth, imgSorce.PixelHeight, PixelFormat.Format32bppPArgb);
                BitmapData bmpdata = bmp.LockBits(new Rectangle(System.Drawing.Point.Empty, bmp.Size), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb);
                imgSorce.CopyPixels(Int32Rect.Empty, bmpdata.Scan0, bmpdata.Height * bmpdata.Stride, bmpdata.Stride);
                bmp.UnlockBits(bmpdata);
                CreateDirectory();
                string filePath = string.Format(@"Images\{0}.png", Guid.NewGuid());
                bmp.Save(filePath, ImageFormat.Png);
                imgPathList.Add(filePath);
            }
            //图片文件
            if (data.GetDataPresent(DataFormats.FileDrop, true))
            {
                string[] objs = (string[])data.GetData(DataFormats.FileDrop, true);
                if (objs != null)
                {
                    for (int i = 0; i < objs.Length; i++)
                    {
                        imgPathList.Add(objs[i]);
                    }
                }
            }
            //剪贴板内单文件
            if (data.GetDataPresent(DataFormats.Bitmap, true))
            {
                string filePath = SaveImg(data.GetData(DataFormats.Bitmap, true) as Bitmap);
                if (filePath != null) { imgPathList.Add(filePath); }
            }
            //HTML页面里面的图片(网页 + word)
            if (data.GetDataPresent(DataFormats.Html, true))
            {
                var obj = data.GetData(DataFormats.Html, true);
                if (obj != null)
                {
                    string dataStr = obj.ToString();
                    imgPathList.AddRange(DownloadImg(dataStr));
                }
            }
            return imgPathList;
        }
        /// <summary>
        /// 保存图片,返回图片地址
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        private static string SaveImg(Bitmap bitmap)
        {
            if (bitmap == null) { return null; }
            CreateDirectory();
            string filePath = string.Format(@"Images\{0}.png", Guid.NewGuid());
            try { bitmap.Save(filePath, ImageFormat.Png); return filePath; }
            catch (Exception ex) { DNTLog(ex); return null; }
        }
        /// <summary>
        /// 批量下载图片
        /// </summary>
        /// <param name="dataStr">页面字符串</param>
        /// <param name="i">成功条数</param>
        /// <returns></returns>
        private static IEnumerable<string> DownloadImg(string dataStr)
        {
            var imgPathList = new List<string>();
            var collection = Regex.Matches(dataStr, @"<img([^>]*)\s*src=('|\"")([^'\""]+)('|\"")", RegexOptions.ECMAScript);
            WebClient webClient = new WebClient();
            foreach (Match item in collection)
            {
                string imgPath = item.Groups[3].Value;
                try
                {
                    CreateDirectory();
                    string filePath = string.Format(@"Images\{0}", Path.GetFileName(imgPath));
                    webClient.DownloadFile(item.Groups[3].Value, filePath);//剪贴板的图片没有相对路径
                    imgPathList.Add(string.Format(@"{0}\{1}", Directory.GetCurrentDirectory(), filePath));
                }
                catch (Exception ex) { DNTLog(ex); }
            }
            return imgPathList;
        }
        private static void DNTLog(Exception ex)
        {
            File.WriteAllText("log.dnt", ex.ToString(), Encoding.UTF8);
        }
        /// <summary>
        /// 创建文件夹
        /// </summary>
        private static void CreateDirectory()
        {
            if (!Directory.Exists("Images"))
            {
                Directory.CreateDirectory("Images");
            }
        }
    }
}

水印帮助类注意点

水印帮助类请看原文

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-04-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 我为Net狂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档