国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 添加圖片水印類的方法

2024-09-07 17:05:38
字體:
供稿:網(wǎng)友
C# 添加圖片水印類的方法,它舉例分析了圖像中添加水印的方法,同時也分析了C#的相關技術,有這方面需要的朋友可以關注錯新技術頻道發(fā)表出的文章,希望對你學習有幫助!
復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Web;
using System.Drawing.Drawing2D;
using System.Reflection;
namespace Chen
{
public class warterPic
{
///

/// 給圖片上水印
///


///原圖片地址
///水印圖片地址
///
public void markwater(string filepath, string waterfile)
{
//gif不水印
int i = filepath.LastIndexOf(".");
string ex = filepath.Substring(i, filepath.Length - i);
if (string.Compare(ex, ".gif", true) == 0)
{
return;
}
string modifyimagepath = filepath;//修改的圖像路徑
int lucencypercent = 25;
Image modifyimage = null;
Image drawedimage = null;
Graphics g = null;
try
{
//建立圖形對象
modifyimage = Image.FromFile(modifyimagepath, true);
drawedimage = Image.FromFile(waterfile, true);
g = Graphics.FromImage(modifyimage);
//獲取要繪制圖形坐標
int x = modifyimage.Width - drawedimage.Width;
int y = modifyimage.Height - drawedimage.Height; //設置顏色矩陣
float[][] matrixitems ={ 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, (float)lucencypercent / 100f, 0 }, new float[] { 0, 0, 0, 0, 1 } };
ColorMatrix colormatrix = new ColorMatrix(matrixitems);
ImageAttributes imgattr = new ImageAttributes();
imgattr.SetColorMatrix(colormatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //繪制陰影圖像
g.DrawImage(drawedimage, new Rectangle(x, y, drawedimage.Width, drawedimage.Height), 10, 10, drawedimage.Width, drawedimage.Height, GraphicsUnit.Pixel, imgattr); //保存文件
string[] allowimagetype ={ ".jpg", ".gif", ".png", ".bmp", ".tiff", ".wmf", ".ico" };
FileInfo fi = new FileInfo(modifyimagepath);
ImageFormat imagetype = ImageFormat.Gif;
switch (fi.Extension.ToLower())
{
case ".jpg":
imagetype = ImageFormat.Jpeg;
break;
case ".gif":
imagetype = ImageFormat.Gif;
break;
case ".png":
imagetype = ImageFormat.Png;
break;
case ".bmp":
imagetype = ImageFormat.Bmp;
break;
case ".tif":
imagetype = ImageFormat.Tiff;
break;
case ".wmf":
imagetype = ImageFormat.Wmf;
break;
case ".ico":
imagetype = ImageFormat.Icon;
break;
default: break;
}
MemoryStream ms = new MemoryStream();
modifyimage.Save(ms, imagetype);
byte[] imgdata = ms.ToArray();
modifyimage.Dispose();
drawedimage.Dispose();
g.Dispose();
FileStream fs = null;
//File.Delete(modifyimagepath);
fs = new FileStream(modifyimagepath, FileMode.Create, FileAccess.Write);
if (fs != null)
{
fs.Write(imgdata, 0, imgdata.Length);
fs.Close();
}
}
finally
{
try
{
drawedimage.Dispose();
modifyimage.Dispose();
g.Dispose();
}
catch
{ }
}
}
}
}

上文是關于C# 添加圖片水印類的方法介紹,相信大家都有了一定的了解,想要了解更多的技術信息,請繼續(xù)關注錯新技術頻道吧!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 开阳县| 揭西县| 杭锦后旗| 蒙城县| 尚志市| 东光县| 颍上县| 阜阳市| 大足县| 泸定县| 临朐县| 琼结县| 工布江达县| 泰州市| 怀宁县| 南靖县| 迁安市| 永州市| 北安市| 绥德县| 永修县| 兴安县| 西安市| 湟中县| 龙泉市| 弋阳县| 同仁县| 托里县| 遵义市| 社会| 永年县| 阿城市| 成安县| 和平县| 青田县| 永寿县| 玛纳斯县| 伊春市| 齐齐哈尔市| 文登市| 武穴市|