欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#(.net)水印圖片的生成完整實(shí)例

 更新時(shí)間:2014年09月17日 09:05:34   投稿:shichen2014  
這篇文章主要介紹了C#(.net)水印圖片的生成方法,以一個(gè)完整實(shí)例的形式講述了水印圖片的生成技巧,非常實(shí)用,需要的朋友可以參考下

本文以一個(gè)完整實(shí)例講述了C#水印圖片的生成方法。是非常實(shí)用的技巧。分享給大家供大家參考。

具體實(shí)例代碼如下:

/*
* 
*  使用說明:
*  建議先定義一個(gè)WaterImage實(shí)例
*  然后利用實(shí)例的屬性,去匹配需要進(jìn)行操作的參數(shù)
*  然后定義一個(gè)WaterImageManage實(shí)例
*  利用WaterImageManage實(shí)例進(jìn)行DrawImage(),印圖片水印
*  DrawWords()印文字水印
* 
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace ABC
{

  /// <summary>
  /// 圖片位置
  /// </summary>
  public enum ImagePosition
  {
    LeftTop,    //左上
    LeftBottom,  //左下
    RightTop,    //右上
    RigthBottom, //右下
    TopMiddle,   //頂部居中
    BottomMiddle, //底部居中
    Center      //中心
  }

  /// <summary>
  /// 水印圖片的操作管理 Design by Gary Gong From Demetersoft.com
  /// </summary>
  public class WaterImageManage
  {
    /// <summary>
    /// 生成一個(gè)新的水印圖片制作實(shí)例
    /// </summary>
    public WaterImageManage()
    {
      //
      // TODO: Add constructor logic here
      //
    }

    /// <summary>
    /// 添加圖片水印
    /// </summary>
    /// <param name="sourcePicture">源圖片文件名</param>
    /// <param name="waterImage">水印圖片文件名</param>
    /// <param name="alpha">透明度(0.1-1.0數(shù)值越小透明度越高)</param>
    /// <param name="position">位置</param>
    /// <param name="PicturePath" >圖片的路徑</param>
    /// <returns>返回生成于指定文件夾下的水印文件名</returns>
    public string DrawImage(string sourcePicture,
                     string waterImage,
                     float alpha,

                     ImagePosition position,
                     string PicturePath)
    {
      //
      // 判斷參數(shù)是否有效
      //
      if (sourcePicture == string.Empty || waterImage == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
      {
        return sourcePicture;
      }

      //
      // 源圖片,水印圖片全路徑
      //
      string sourcePictureName = PicturePath + sourcePicture;
      string waterPictureName = PicturePath + waterImage;
      string fileSourceExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();
      string fileWaterExtension = System.IO.Path.GetExtension(waterPictureName).ToLower();
      //
      // 判斷文件是否存在,以及類型是否正確
      //
      if (System.IO.File.Exists(sourcePictureName) == false ||
        System.IO.File.Exists(waterPictureName) == false || (
        fileSourceExtension != ".gif" &&
        fileSourceExtension != ".jpg" &&
        fileSourceExtension != ".png") || (
        fileWaterExtension != ".gif" &&
        fileWaterExtension != ".jpg" &&
        fileWaterExtension != ".png")
        )
      {
        return sourcePicture;
      }

      //

      // 目標(biāo)圖片名稱及全路徑
      //
      string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_1101.jpg";

      //
      // 將需要加上水印的圖片裝載到Image對(duì)象中
      //
      Image imgPhoto = Image.FromFile(sourcePictureName);
      //
      // 確定其長寬
      //
      int phWidth = imgPhoto.Width;
      int phHeight = imgPhoto.Height;

      //
      // 封裝 GDI+ 位圖,此位圖由圖形圖像及其屬性的像素?cái)?shù)據(jù)組成。
      //
      Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

      //
      // 設(shè)定分辨率
      // 
      bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

      //
      // 定義一個(gè)繪圖畫面用來裝載位圖
      //
      Graphics grPhoto = Graphics.FromImage(bmPhoto);

      //
      //同樣,由于水印是圖片,我們也需要定義一個(gè)Image來裝載它
      //
      Image imgWatermark = new Bitmap(waterPictureName);

      //
      // 獲取水印圖片的高度和寬度
      //
      int wmWidth = imgWatermark.Width;
      int wmHeight = imgWatermark.Height;

      //SmoothingMode:指定是否將平滑處理(消除鋸齒)應(yīng)用于直線、曲線和已填充區(qū)域的邊緣。
      // 成員名稱  說明 
      // AntiAlias   指定消除鋸齒的呈現(xiàn)。 
      // Default    指定不消除鋸齒。

      // HighQuality 指定高質(zhì)量、低速度呈現(xiàn)。 
      // HighSpeed  指定高速度、低質(zhì)量呈現(xiàn)。 
      // Invalid    指定一個(gè)無效模式。 
      // None     指定不消除鋸齒。 
      grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

 

      //
      // 第一次描繪,將我們的底圖描繪在繪圖畫面上
      //
      grPhoto.DrawImage(imgPhoto,
                    new Rectangle(0, 0, phWidth, phHeight),
                    0,
                    0,
                    phWidth,
                    phHeight,
                    GraphicsUnit.Pixel);

      //
      // 與底圖一樣,我們需要一個(gè)位圖來裝載水印圖片。并設(shè)定其分辨率
      //
      Bitmap bmWatermark = new Bitmap(bmPhoto);
      bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

      //
      // 繼續(xù),將水印圖片裝載到一個(gè)繪圖畫面grWatermark
      //
      Graphics grWatermark = Graphics.FromImage(bmWatermark);

      //
      //ImageAttributes 對(duì)象包含有關(guān)在呈現(xiàn)時(shí)如何操作位圖和圖元文件顏色的信息。
      //   

      ImageAttributes imageAttributes = new ImageAttributes();

      //
      //Colormap: 定義轉(zhuǎn)換顏色的映射
      //
      ColorMap colorMap = new ColorMap();

      //
      //我的水印圖被定義成擁有綠色背景色的圖片被替換成透明
      //
      colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
      colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

      ColorMap[] remapTable = { colorMap };

      imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

      float[][] colorMatrixElements = { 
      new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, // red紅色
      new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f}, //green綠色
      new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, //blue藍(lán)色    
      new float[] {0.0f, 0.0f, 0.0f, alpha, 0.0f}, //透明度   
      new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};//

      // ColorMatrix:定義包含 RGBA 空間坐標(biāo)的 5 x 5 矩陣。
      // ImageAttributes 類的若干方法通過使用顏色矩陣調(diào)整圖像顏色。
      ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);


      imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
       ColorAdjustType.Bitmap);

      //
      //上面設(shè)置完顏色,下面開始設(shè)置位置
      //
      int xPosOfWm;
      int yPosOfWm;

      switch (position)
      {
        case ImagePosition.BottomMiddle:
          xPosOfWm = (phWidth - wmWidth) / 2;
          yPosOfWm = phHeight - wmHeight - 10;
          break;

        case ImagePosition.Center:
          xPosOfWm = (phWidth - wmWidth) / 2;
          yPosOfWm = (phHeight - wmHeight) / 2;
          break;
        case ImagePosition.LeftBottom:
          xPosOfWm = 10;
          yPosOfWm = phHeight - wmHeight - 10;
          break;
        case ImagePosition.LeftTop:
          xPosOfWm = 10;
          yPosOfWm = 10;
          break;
        case ImagePosition.RightTop:
          xPosOfWm = phWidth - wmWidth - 10;
          yPosOfWm = 10;
          break;
        case ImagePosition.RigthBottom:
          xPosOfWm = phWidth - wmWidth - 10;
          yPosOfWm = phHeight - wmHeight - 10;
          break;
        case ImagePosition.TopMiddle:
          xPosOfWm = (phWidth - wmWidth) / 2;
          yPosOfWm = 10;
          break;
        default:
          xPosOfWm = 10;
          yPosOfWm = phHeight - wmHeight - 10;
          break;
      }

      // 第二次繪圖,把水印印上去
      //
      grWatermark.DrawImage(imgWatermark,
       new Rectangle(xPosOfWm,
                 yPosOfWm,
                 wmWidth,
                 wmHeight),
                 0,
                 0,
                 wmWidth,
                 wmHeight,
                 GraphicsUnit.Pixel,
                 imageAttributes);

      imgPhoto = bmWatermark;
      grPhoto.Dispose();
      grWatermark.Dispose();

      //
      // 保存文件到服務(wù)器的文件夾里面
      //
      imgPhoto.Save(targetImage, ImageFormat.Jpeg);
      imgPhoto.Dispose();
      imgWatermark.Dispose();
      return targetImage.Replace(PicturePath, "");
    }

/*
* 
* 使用說明:
*  建議先定義一個(gè)WaterImage實(shí)例
*  然后利用實(shí)例的屬性,去匹配需要進(jìn)行操作的參數(shù)
*  然后定義一個(gè)WaterImageManage實(shí)例
*  利用WaterImageManage實(shí)例進(jìn)行DrawImage(),印圖片水印
*  DrawWords()印文字水印
* 
-*/

    /// <summary>
    /// 在圖片上添加水印文字
    /// </summary>
    /// <param name="sourcePicture">源圖片文件(文件名,不包括路徑)</param>

    /// <param name="waterWords">需要添加到圖片上的文字</param>
    /// <param name="alpha">透明度</param>
    /// <param name="position">位置</param>
    /// <param name="PicturePath">文件路徑</param>
    /// <returns></returns>
    public string DrawWords(string sourcePicture,
                     string waterWords,
                     float alpha,
                     ImagePosition position,
                     string PicturePath)
    {
      //
      // 判斷參數(shù)是否有效
      //
      if (sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0 || PicturePath == string.Empty)
      {
        return sourcePicture;
      }

      //
      // 源圖片全路徑
      //
      if (PicturePath.Substring(PicturePath.Length - 1, 1) != "/")
        PicturePath += "/";
      string sourcePictureName = PicturePath + sourcePicture;
      string fileExtension = System.IO.Path.GetExtension(sourcePictureName).ToLower();

      //
      // 判斷文件是否存在,以及文件名是否正確
      //
      if (System.IO.File.Exists(sourcePictureName) == false || (
        fileExtension != ".gif" &&
        fileExtension != ".jpg" &&

        fileExtension != ".png"))
      {
        return sourcePicture;
      }

      //
      // 目標(biāo)圖片名稱及全路徑
      //
      string targetImage = sourcePictureName.Replace(System.IO.Path.GetExtension(sourcePictureName), "") + "_0703.jpg";

      //創(chuàng)建一個(gè)圖片對(duì)象用來裝載要被添加水印的圖片
      Image imgPhoto = Image.FromFile(sourcePictureName);

      //獲取圖片的寬和高
      int phWidth = imgPhoto.Width;
      int phHeight = imgPhoto.Height;

      //
      //建立一個(gè)bitmap,和我們需要加水印的圖片一樣大小
      Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

      //SetResolution:設(shè)置此 Bitmap 的分辨率
      //這里直接將我們需要添加水印的圖片的分辨率賦給了bitmap
      bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

      //Graphics:封裝一個(gè) GDI+ 繪圖圖面。
      Graphics grPhoto = Graphics.FromImage(bmPhoto);

      //設(shè)置圖形的品質(zhì)
      grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

      //將我們要添加水印的圖片按照原始大小描繪(復(fù)制)到圖形中
      grPhoto.DrawImage(
       imgPhoto,                      //  要添加水印的圖片
       new Rectangle(0, 0, phWidth, phHeight), // 根據(jù)要添加的水印圖片的寬和高
       0,                           // X方向從0點(diǎn)開始描繪
       0,                           // Y方向

       phWidth,                      // X方向描繪長度
       phHeight,                      // Y方向描繪長度
       GraphicsUnit.Pixel);               // 描繪的單位,這里用的是像素

      //根據(jù)圖片的大小我們來確定添加上去的文字的大小
      //在這里我們定義一個(gè)數(shù)組來確定
      int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

      //字體
      Font crFont = null;
      //矩形的寬度和高度,SizeF有三個(gè)屬性,分別為Height高,width寬,IsEmpty是否為空
      SizeF crSize = new SizeF();

      //利用一個(gè)循環(huán)語句來選擇我們要添加文字的型號(hào)
      //直到它的長度比圖片的寬度小
      for (int i = 0; i < 7; i++)
      {
        crFont = new Font("arial", sizes[i], FontStyle.Bold);

        //測(cè)量用指定的 Font 對(duì)象繪制并用指定的 StringFormat 對(duì)象格式化的指定字符串。
        crSize = grPhoto.MeasureString(waterWords, crFont);

        // ushort 關(guān)鍵字表示一種整數(shù)數(shù)據(jù)類型
        if ((ushort)crSize.Width < (ushort)phWidth)
          break;
      }

      //截邊5%的距離,定義文字顯示(由于不同的圖片顯示的高和寬不同,所以按百分比截取)
      int yPixlesFromBottom = (int)(phHeight * .05);

      //定義在圖片上文字的位置
      float wmHeight = crSize.Height;
      float wmWidth = crSize.Width;

      float xPosOfWm;

      float yPosOfWm;

   switch (position)
      {
        case ImagePosition.BottomMiddle:
          xPosOfWm = phWidth / 2;
          yPosOfWm = phHeight - wmHeight - 10;
          break;
        case ImagePosition.Center:
          xPosOfWm = phWidth / 2;
          yPosOfWm = phHeight / 2;
          break;
        case ImagePosition.LeftBottom:
          xPosOfWm = wmWidth;
          yPosOfWm = phHeight - wmHeight - 10;
          break;
        case ImagePosition.LeftTop:
          xPosOfWm = wmWidth / 2;
          yPosOfWm = wmHeight / 2;
          break;
        case ImagePosition.RightTop:
          xPosOfWm = phWidth - wmWidth - 10;
          yPosOfWm = wmHeight;
          break;
        case ImagePosition.RigthBottom:
          xPosOfWm = phWidth - wmWidth - 10;
          yPosOfWm = phHeight - wmHeight - 10;
          break;
        case ImagePosition.TopMiddle:
          xPosOfWm = phWidth / 2;
          yPosOfWm = wmWidth;

          break;
        default:
          xPosOfWm = wmWidth;
          yPosOfWm = phHeight - wmHeight - 10;
          break;
      }

      //封裝文本布局信息(如對(duì)齊、文字方向和 Tab ??课唬?,顯示操作(如省略號(hào)插入和國家標(biāo)準(zhǔn) (National) 數(shù)字替換)和 OpenType 功能。
      StringFormat StrFormat = new StringFormat();

      //定義需要印的文字居中對(duì)齊
      StrFormat.Alignment = StringAlignment.Center;

      //SolidBrush:定義單色畫筆。畫筆用于填充圖形形狀,如矩形、橢圓、扇形、多邊形和封閉路徑。
      //這個(gè)畫筆為描繪陰影的畫筆,呈灰色
      int m_alpha = Convert.ToInt32(256 * alpha);
      SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(m_alpha, 0, 0, 0));

      //描繪文字信息,這個(gè)圖層向右和向下偏移一個(gè)像素,表示陰影效果
      //DrawString 在指定矩形并且用指定的 Brush 和 Font 對(duì)象繪制指定的文本字符串。
      grPhoto.DrawString(waterWords,                  //string of text
                    crFont,                     //font
                    semiTransBrush2,              //Brush
                    new PointF(xPosOfWm + 1, yPosOfWm + 1), //Position
                    StrFormat);

      //從四個(gè) ARGB 分量(alpha、紅色、綠色和藍(lán)色)值創(chuàng)建 Color 結(jié)構(gòu),這里設(shè)置透明度為153
      //這個(gè)畫筆為描繪正式文字的筆刷,呈白色
      SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));

      //第二次繪制這個(gè)圖形,建立在第一次描繪的基礎(chǔ)上
      grPhoto.DrawString(waterWords,         //string of text
                    crFont,                  //font
                    semiTransBrush,              //Brush
                    new PointF(xPosOfWm, yPosOfWm), //Position
                    StrFormat);

      //imgPhoto是我們建立的用來裝載最終圖形的Image對(duì)象
      //bmPhoto是我們用來制作圖形的容器,為Bitmap對(duì)象
      imgPhoto = bmPhoto;
      //釋放資源,將定義的Graphics實(shí)例grPhoto釋放,grPhoto功德圓滿
      grPhoto.Dispose();

      //將grPhoto保存
      imgPhoto.Save(targetImage, ImageFormat.Jpeg);
      imgPhoto.Dispose();

      return targetImage.Replace(PicturePath, "");
    }
  }

  /// <summary>
  /// 裝載水印圖片的相關(guān)信息
  /// </summary>
  public class WaterImage
  {
    public WaterImage()
    {

    }

    private string m_sourcePicture;
    /// <summary>
    /// 源圖片地址名字(帶后綴)

    /// </summary>
    public string SourcePicture
    {
      get { return m_sourcePicture; }
      set { m_sourcePicture = value; }
    }

    private string m_waterImager;
    /// <summary>
    /// 水印圖片名字(帶后綴)
    /// </summary>
    public string WaterPicture
    {
      get { return m_waterImager; }
      set { m_waterImager = value; }
    }

    private float m_alpha;
    /// <summary>
    /// 水印圖片文字的透明度
    /// </summary>
    public float Alpha
    {
      get { return m_alpha; }
      set { m_alpha = value; }
    }

    private ImagePosition m_postition;
    /// <summary>
    /// 水印圖片或文字在圖片中的位置
    /// </summary>
    public ImagePosition Position
    {
      get { return m_postition; }
      set { m_postition = value; }
    }

    private string m_words;
    /// <summary>
    /// 水印文字的內(nèi)容
    /// </summary>
    public string Words
    {
      get { return m_words; }
      set { m_words = value; }
    }

  }
}

相信本文所述對(duì)大家的C#程序設(shè)計(jì)有一定的借鑒參考作用。

相關(guān)文章

  • C#中LINQ的Select與SelectMany函數(shù)使用

    C#中LINQ的Select與SelectMany函數(shù)使用

    這篇文章主要介紹了C#中LINQ的Select與SelectMany函數(shù)使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • C#調(diào)用HTTP POST請(qǐng)求上傳圖片的示例代碼

    C#調(diào)用HTTP POST請(qǐng)求上傳圖片的示例代碼

    現(xiàn)在很多B/S系統(tǒng)的開發(fā)都是通過API方式來進(jìn)行的,一般服務(wù)端會(huì)開放一個(gè)API接口,客戶端調(diào)用API接口來實(shí)現(xiàn)圖片或文件上傳的功能,感興趣的可以了解一下
    2021-05-05
  • c#對(duì)xml增刪改查操作示例

    c#對(duì)xml增刪改查操作示例

    本文主要介紹c#對(duì)xml增刪改查操作的示例,大家參考使用吧
    2014-01-01
  • C#實(shí)現(xiàn)將RTF轉(zhuǎn)為HTML的示例代碼

    C#實(shí)現(xiàn)將RTF轉(zhuǎn)為HTML的示例代碼

    RTF文檔即富文本格式(Rich?Text?Format)的文檔。我們?cè)谔幚砦募r(shí),遇到需要對(duì)文檔格式進(jìn)行轉(zhuǎn)換時(shí),可以將RTF轉(zhuǎn)為其他格式,如轉(zhuǎn)為DOCX/DOC、PDF或者HTML。本文將利用C#實(shí)現(xiàn)RTF轉(zhuǎn)HTML,需要的可以參考一下
    2022-04-04
  • C#巧用DateTime預(yù)設(shè)可選的日期范圍(如本年度、本季度、本月等)

    C#巧用DateTime預(yù)設(shè)可選的日期范圍(如本年度、本季度、本月等)

    這篇文章主要介紹了C#巧用DateTime預(yù)設(shè)可選的日期范圍,如本年度、本季度、本月等,感興趣的小伙伴們可以參考一下
    2016-04-04
  • C#實(shí)現(xiàn)復(fù)雜XML的序列化與反序列化

    C#實(shí)現(xiàn)復(fù)雜XML的序列化與反序列化

    這篇文章主要介紹了C#實(shí)現(xiàn)復(fù)雜XML的序列化與反序列化的方法,是非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下
    2014-09-09
  • C#面向?qū)ο笤O(shè)計(jì)的七大原則

    C#面向?qū)ο笤O(shè)計(jì)的七大原則

    這篇文章主要為大家詳細(xì)介紹了C#面向?qū)ο笤O(shè)計(jì)的七大原則,未讀文章之前大家說一說都有哪七大原則,是不是七大原則都可以全部列出,想要了解的朋友可以參考一下
    2016-05-05
  • 高效C#編碼優(yōu)化原則

    高效C#編碼優(yōu)化原則

    這篇文章主要介紹了高效C#編碼優(yōu)化原則,非常實(shí)用,需要的朋友可以參考下
    2014-08-08
  • C# Record構(gòu)造函數(shù)的行為更改詳解

    C# Record構(gòu)造函數(shù)的行為更改詳解

    C# 9 中的record類型是僅具有只讀屬性的輕量級(jí)、不可變數(shù)據(jù)類型(或輕量級(jí)類),下面這篇文章主要給大家介紹了關(guān)于C# Record構(gòu)造函數(shù)的行為更改的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • 詳解c# 中的DateTime

    詳解c# 中的DateTime

    這篇文章主要介紹了c# 中的DateTime的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評(píng)論