1. 程式人生 > >逆天通用水印擴充套件篇~新增剪貼簿系列的功能和手動配置,卸除原基礎不常用的功能

逆天通用水印擴充套件篇~新增剪貼簿系列的功能和手動配置,卸除原基礎不常用的功能

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;
using WaterMarkAPP.Enums;
using WaterMarkAPP.Model;

namespace WaterMarkAPP.Common
{

    /// <summary>
    /// 水印幫助類
    /// </summary>
    public class WaterMarkHelper
    {
        public static bool TryFromFile(string imgPath, ref Image img)
        {
            try
            {
                img = Image.FromFile(imgPath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #region 設定水印
        /// <summary>
        /// 設定水印
        /// </summary>
        /// <param name="imgPath"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public static Image SetWaterMark(string imgPath, WaterMark model)
        {
            Image imgSource = null;//背景圖
            Image markImg = null;//水印圖片
            if (!TryFromFile(imgPath, ref imgSource))
            {
                return null;
            }

            //水印檢驗(文字,圖片[路徑下是否存在圖片])
            #region 水印校驗+水印處理
            if (model == null) { return null; }
            if (!System.IO.File.Exists(imgPath)) { return null; }//看看原圖是否存在
            //根據水印型別校驗+水印處理
            switch (model.WaterMarkType)
            {
                case WaterMarkTypeEnum.Text:
                    if (string.IsNullOrEmpty(model.Text))
                    {
                        return null;
                    }
                    else
                    {
                        markImg = TextToImager(model);//水印處理-如果是文字就轉換成圖片
                    }
                    break;
                case WaterMarkTypeEnum.Image:
                    if (!System.IO.File.Exists(model.ImgPath))
                    {
                        return null;
                    }
                    else
                    {
                        if (!TryFromFile(model.ImgPath, ref markImg))//獲得水印影象
                        {
                            return imgSource;
                        }
                    }
                    break;
                case WaterMarkTypeEnum.NoneMark:
                    return imgSource;
            }
            #endregion

            #region 建立顏色矩陣
            //建立顏色矩陣
            float[][] ptsArray ={
                                 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, model.Transparency, 0}, //注意:0.0f為完全透明,1.0f為完全不透明
                                 new float[] {0, 0, 0, 0, 1}};
            ColorMatrix colorMatrix = new ColorMatrix(ptsArray);
            //新建一個Image屬性
            ImageAttributes imageAttributes = new ImageAttributes();
            //將顏色矩陣新增到屬性
            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Default);
            #endregion

            //原圖格式檢驗+水印
            #region 原圖格式檢驗+水印
            //判斷是否是索引影象格式
            if (imgSource.PixelFormat == PixelFormat.Format1bppIndexed || imgSource.PixelFormat == PixelFormat.Format4bppIndexed || imgSource.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                #region 索引圖片,轉成點陣圖再加圖片
                //轉成點陣圖,這步很重要 
                Bitmap bitmap = new Bitmap(imgSource.Width, imgSource.Height);
                Graphics graphic = Graphics.FromImage(bitmap);

                #region 縮放處理
                //如果原圖小於水印圖片 等比縮放水印圖
                if (markImg.Width >= imgSource.Width || markImg.Height >= imgSource.Height)
                {
                    markImg = ImageShrink(imgSource, markImg);
                }
                #endregion

                #region 水印位置
                //水印位置
                int x;
                int y;
                WaterMarkLocations(model, imgSource, markImg, out x, out y);
                #endregion

                //將原圖畫在點陣圖上
                graphic.DrawImage(imgSource, new Point(0, 0));

                //將水印加在點陣圖上
                graphic.DrawImage(markImg, new Rectangle(x, y, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes);

                graphic.Dispose();
                return bitmap;
                #endregion
            }
            else
            {
                #region 非索引圖片,直接在上面加上水印
                Graphics graphic = Graphics.FromImage(imgSource);

                #region 縮放處理
                //如果原圖小於水印圖片 等比縮放水印圖
                if (markImg.Width >= imgSource.Width || markImg.Height >= imgSource.Height)
                {
                    markImg = ImageShrink(imgSource, markImg);
                }
                #endregion

                #region 水印位置
                //水印位置
                int x;
                int y;
                WaterMarkLocations(model, imgSource, markImg, out x, out y);
                #endregion

                //將水印加在原圖上
                graphic.DrawImage(markImg, new Rectangle(x, y, markImg.Width, markImg.Height), 0, 0, markImg.Width, markImg.Height, GraphicsUnit.Pixel, imageAttributes);

                graphic.Dispose();
                return imgSource;
                #endregion
            }
            #endregion
        }
        #endregion

        #region 水印處理-文字轉圖片
        /// <summary>
        /// 水印處理-文字轉圖片
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static Image TextToImager(WaterMark model)
        {
            Font f = new Font(model.FontFamily, model.FontSize, model.FontStyle);
            Bitmap fbitmap = new Bitmap(Encoding.GetEncoding("GBK").GetByteCount(model.Text) / 2 * f.Height, f.Height);
            Graphics gh = Graphics.FromImage(fbitmap);//建立一個畫板;
            gh.SmoothingMode = SmoothingMode.AntiAlias;
            gh.DrawString(model.Text, f, model.BrushesColor, 0, 0);//畫字串
            return fbitmap as Image;
        }
        #endregion

        #region 水印位置
        /// <summary>
        /// 水印位置
        /// </summary>
        /// <param name="model"></param>
        /// <param name="imgSource"></param>
        /// <param name="markImg"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        private static void WaterMarkLocations(WaterMark model, Image imgSource, Image markImg, out int x, out int y)
        {
            x = 0;
            y = 0;
            switch (model.WaterMarkLocation)
            {
                case WaterMarkLocationEnum.TopLeft:
                    x = 0;
                    y = 0;
                    break;
                case WaterMarkLocationEnum.TopCenter:
                    x = imgSource.Width / 2 - markImg.Width / 2;
                    y = 0;
                    break;
                case WaterMarkLocationEnum.TopRight:
                    x = imgSource.Width - markImg.Width;
                    y = 0;
                    break;
                case WaterMarkLocationEnum.CenterLeft:
                    x = 0;
                    y = imgSource.Height / 2 - markImg.Height / 2;
                    break;
                case WaterMarkLocationEnum.CenterCenter:
                    x = imgSource.Width / 2 - markImg.Width / 2;
                    y = imgSource.Height / 2 - markImg.Height / 2;
                    break;
                case WaterMarkLocationEnum.CenterRight:
                    x = imgSource.Width - markImg.Width;
                    y = imgSource.Height / 2 - markImg.Height / 2;
                    break;
                case WaterMarkLocationEnum.BottomLeft:
                    x = 0;
                    y = imgSource.Height - markImg.Height;
                    break;
                case WaterMarkLocationEnum.BottomCenter:
                    x = imgSource.Width / 2 - markImg.Width / 2;
                    y = imgSource.Height - markImg.Height;
                    break;
                case WaterMarkLocationEnum.BottomRight:
                    x = imgSource.Width - markImg.Width;
                    y = imgSource.Height - markImg.Height;
                    break;
            }
        }
        #endregion

        #region 縮放水印
        /// <summary>
        /// 等比縮放水印圖(縮小到原圖的1/3)
        /// </summary>
        /// <param name="imgSource"></param>
        /// <param name="successImage"></param>
        /// <returns></returns>
        private static Image ImageShrink(Image imgSource, Image markImg)
        {
            int w = 0;
            int h = 0;

            Image.GetThumbnailImageAbort callb = null;

            //對水印圖片生成縮圖,縮小到原圖的1/3(以短的一邊為準)                     
            if (imgSource.Width < imgSource.Height)
            {
                w = imgSource.Width / 3;
                h = markImg.Height * w / markImg.Width;
            }
            else
            {
                h = imgSource.Height / 3;
                w = markImg.Width * h / markImg.Height;
            }
            markImg = markImg.GetThumbnailImage(w, h, callb, new System.IntPtr());
            return markImg;
        }
        #endregion
    }
}

相關推薦

通用水印擴充套件~新增剪貼簿系列功能手動配置基礎常用功能

using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Text; using WaterMarkAPP.Enums; using WaterMarkAPP.

通用水印支援WinformWPFWebWPWin10。支援位置選擇(9個位置 ==》[X])

/// <summary> /// 水印幫助類 /// </summary> public class WaterMarkHelper { #region 設定水印 /// <summary&

《21學通C#》變數使用前需要宣告賦值賦值後可以重新賦新的值

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace 變數宣告賦值使用{ class Program { static

Linux 趁手工具之剪貼簿系列

懂 Linux 的程式設計師朋友很多,但是把 Linux 當作日常桌面系統用的還是不多。我自己的小破筆記本上,350G 的機械硬碟跑著 Win7, 120G 的MSATA SSD 跑過Ubuntu,OpenSuse,Kali(基於 Debian ),現在跑著  Debia

擴充套件歐幾里德求元+通用除法取模

#include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f const int maxn=1e5+9; int e_gcd(int a,int b,int &x,int &

IOS視訊編輯功能詳解上-新增水印

前言 用程式碼在簡單視訊編輯中,主要就是加美顏、水印(貼圖)、視訊擷取、視訊拼接、音視訊的處理,在美顏中,使用GPUImage即可實現多種濾鏡、磨皮美顏的功能,並且可以臉部識別實時美顏等功能,這個有很多成熟的處理方案,所以現在主要說後面的水印(貼圖)、視訊擷取、視訊拼接、音

520魔都上海有一家的分手花店火火火了

報價單 情人節 靜安寺 上海 花店 5月20日,520諧音“我愛你”,這一天被情侶們當做了小情人節,紛紛在這一天選擇告白愛意。可是,在上海有一家花店卻釋放出了520終極“黑暗料理”。這家分手的位置非常好,愚園路102號,距離靜安寺、久光百貨等核心商圈不過幾十米。分手花店,光這店名就非常紮心

[每日App一]QQ主題要!輕輕松松月入30萬!

div color 小白 edi upn import 多少 有意 wds 聽從吾師秦剛(微信或QQ:1111884)酋長的建議,謀哥(微信viyi88)我開始新的征程,每日更新一篇幹貨文章(要堅持啊!否則被酋長歧視了)。 好了,廢話不多說,今天我給大家揭秘一

從零開始學習jQuery (一) 開辟地入門

完全 不同 喝茶 圖靈 lac 格式化 元素 script 引入 從零開始學習jQuery (一) 開天辟地入門篇 本系列文章導航 從零開始學習jQuery (一) 開天辟地入門篇 從零開始學習jQuery (二) 萬能的選擇器 從零開始學習jQuery (

html5 (的html)

text while 因特網 清除 ofo 服務 pla 體驗 圖像 簡介 h5的新特性: cavas / video / audio / cache / element / form 最小的h5文檔: <!DOCTYPE html> <html&

開通渲雲至尊會員送正版3DS MAX 2108這是要啊!

小編聽說,大家都用破解版的 3ds Max ,想問說,破解的用的還順手麽,有木有遇到軟件崩潰,系統紊亂,病毒侵入的問題,如果這些你都沒有遇到,那麽恭喜您,下一個就是你!俗話說,常在河邊走,哪有不濕鞋,所以,聰明人都選擇了正版,而且,盜版軟件已經完全破壞了正常的“生態環境”,越來越多的人對破解、盜版這個事

這手速了!編輯機器人分秒編輯人類編輯還有希望嗎?

人工智能編輯 編輯機器人 寫作機器人 資訊機器人 什麽是編輯機器人?編輯機器人就是通過AI技術來智能化提供文本的分類、分詞、關鍵詞提取等工作,極大提高文本處理效率。智能化的資訊頻道定制服務專家,無需人工編輯,資訊內容自動化生成。助力資訊運營“無人化編輯”,實現內容的7*24小時實時更新,是企業

新媒體運營神器-迅捷旗下的這些媒體軟件簡直要了!

格式轉換 fff 免費 迅捷 包括 時代 單單 資源下載 了解 在這個信息傳播速度賊快的新媒體時代,視頻傳播已經占據了所有市場的百分之八十以上了,例如現在最流行的抖音、快手、好看等視頻平臺不斷的冒出市場,這也就意味著最原始的文字信息已經無法在滿足我們的需求了,那麽我們今天就

Java 11 正式發布這 8 個新特性教你寫出更牛逼的代碼

cti leo clas data 系列 end put The async 美國時間 09 月 25 日,Oralce 正式發布了 Java 11,這是據 Java 8 以後支持的首個長期版本。 為什麽說是長期版本,看下面的官方發布的支持路線圖表。 Java 11 正式發

這個顏值的姑娘居然是一枚程序猿~

走了 技巧 app 獲取 應用 讀書 優秀 負責 向人 讀書與旅行 “我將讀書看成是一種修行,堅持每天讀書1-2小時,我的書大多數是技術書籍,是我獲取知識的重要來源。 “清宵認為堅持讀書是每一位優秀程序員必備的素質,“我讀書時經常做筆記,將一些重點或心得記下來,第二天再

南大通用面試終結 結束無業生活

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

ES6字串擴充套件新增的方法)

1、includes(): 返回布林值,表示是否找到了引數字串。 2、startsWith(): 返回布林值,表示引數字串是否在查詢字串的頭部。 3、endsWith(): 返回布林值,表示引數字串是否在查詢字串的尾部。 let str = "string"; con

ES6正則擴充套件新增修飾符y、u)

1、y修飾符 y :也是全域性匹配,首次匹配和g修飾符效果一樣,但是第二次往後就不一樣了, g修飾符不一定要求匹配下標緊接著上一次開始匹配的去找,只要找到了就行; 而y修飾符是規定要求匹配下標緊接著上一次匹配的開始 去匹配,不合適條件就為匹配失敗為null。 從上圖程式碼第二段列印two

波士頓動力機器人人類已無法阻擋它的三級跳!

  一直重新整理大眾認知的波士頓動力又秀出了新花樣。   如今,波士頓動力的 Atlas 人形機器人可以玩跑酷了!在該公司釋出的一段最新視訊中,Atlas 展示了它可以單腳越過障礙物、跳到交錯的箱子上,毫不費力!   該公司表示,Atlas

xLua - 第三方擴充套件新增編譯

unity 使用xLua外掛,這兩天增加lua-protobuf外掛,所以需要重新編譯XLua 這裡編譯Win平臺和Android平臺,因為手頭上沒有mac電腦所以沒有編譯ios和mac平臺庫 這裡只做簡單的記錄 Windows平臺 參考文章 xLua - 第三方擴充套件的新增和