实例介绍
【实例简介】
C#实现给照片批量添加水印功能 实例源码,可添加文字水印和图片水印等,其中还有透明度以及水印字体设置等功能
【实例截图】

【核心代码】
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 获取系统字体
private void GetSystemFont(ToolStripComboBox cb)
{
InstalledFontCollection myFont = new InstalledFontCollection();
foreach (FontFamily ff in myFont.Families)
{
cb.Items.Add(ff.Name);
}
cb.SelectedItem = "宋体";
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
cbbPosition.SelectedIndex = 0;
}
string [] ImgArray=null;
string ImgDirectoryPath=null;
private void btnLoadImg_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
lbImgList.Items.Clear();
ImgArray = openFileDialog1.FileNames;
string ImgP = ImgArray[0].ToString();
ImgP = ImgP.Remove(ImgP.LastIndexOf("\\"));
ImgDirectoryPath = ImgP;
for (int i = 0; i < ImgArray.Length; i )
{
string ImgPath = ImgArray[i].ToString();
string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") 1, ImgPath.Length - ImgPath.LastIndexOf("\\") - 1);
lbImgList.Items.Add(ImgName);
}
tsslStatus.Text = "图片总数:" lbImgList.Items.Count;
}
}
private void lbImgList_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbImgList.SelectedItems.Count > 0)
{
tsslText.Text = "图片位置:" ImgDirectoryPath "\\" lbImgList.SelectedItems[0].ToString();
}
}
private void btnPreview_Click(object sender, EventArgs e)
{
if (lbImgList.Items.Count > 0)
{
if (rbTxt.Checked)
{
if (txtWaterMarkFont.Text != "" && txtSavaPath.Text.Trim() != "")
{
AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);
Form2 frm2 = new Form2();
frm2.ig = BigBt;
frm2.ShowDialog();
}
}
else
{
if (txtWaterMarkImg.Text != "" && txtSavaPath.Text != "")
{
ChangeAlpha();
AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 2);
Form2 frm2 = new Form2();
frm2.ig = BigBt;
frm2.ShowDialog();
}
}
}
}
private void trackBar1_Enter(object sender, EventArgs e)
{
lbImgList.Focus();
}
private void btnSelect_Click(object sender, EventArgs e)
{
if (openFileDialog2.ShowDialog() == DialogResult.OK)
{
txtWaterMarkImg.Text = openFileDialog2.FileName;
if (rbPIC.Checked == true)
{
ChangeAlpha();
pbImgPreview.Image = Image.FromFile(txtWaterMarkImg.Text.Trim());
}
}
}
private void rbTxt_CheckedChanged(object sender, EventArgs e)
{
trackBar1.Enabled = false;
if(rbPIC.Checked)
pbImgPreview.Image = null;
}
private void rbPIC_CheckedChanged(object sender, EventArgs e)
{
trackBar1.Enabled = true;
if (rbTxt.Checked)
{
pbImgPreview.Image = null;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (lbImgList.Items.Count > 0)
{
fontDialog1.ShowColor = true;
fontDialog1.ShowHelp = false;
fontDialog1.ShowApply = false;
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
FontF = fontDialog1.Font.FontFamily;
fontColor = fontDialog1.Color;
fontSize = fontDialog1.Font.Size;
fontStyle = fontDialog1.Font.Style;
AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);
pbImgPreview.Image = bt;
}
}
}
Bitmap bt=null;
float fontSize = 8;
Color fontColor=Color.Black;
FontFamily FontF = null;
FontStyle fontStyle = FontStyle.Regular;
int Fwidth;
int Fheight;
Bitmap BigBt;
Font f;
Brush b;
private void AddFontWatermark(string txt,string Iname,int i)//预览
{
b = new SolidBrush(fontColor);
bt = new Bitmap(368,75);
BigBt = new Bitmap(Image.FromFile(ImgDirectoryPath "\\" Iname));
Graphics g = Graphics.FromImage(bt);
Graphics g1 = Graphics.FromImage(BigBt);
g.Clear(Color.Gainsboro);
pbImgPreview.Image = bt;
if (FontF == null)
{
f = new Font(txt,fontSize);
SizeF XMaxSize = g.MeasureString(txt, f);
Fwidth = (int)XMaxSize.Width;
Fheight = (int)XMaxSize.Height;
g.DrawString(txt,f, b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2);
if (cbbPosition.SelectedIndex==0)//正中
{
g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2);
}
if (cbbPosition.SelectedIndex == 1)//左上
{
g1.DrawString(txt, f, b, 30,30);
}
if (cbbPosition.SelectedIndex ==2)//左下
{
g1.DrawString(txt, f, b, 30, (int)(BigBt.Height - Fheight)-30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), 30);
}
if (cbbPosition.SelectedIndex == 4)//右下
{
g1.DrawString(txt, f, b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);
}
}
else
{
f = new Font(FontF, fontSize, fontStyle);
SizeF XMaxSize = g.MeasureString(txt, f);
Fwidth = (int)XMaxSize.Width;
Fheight = (int)XMaxSize.Height;
g.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(368 - Fwidth) / 2, (int)(75 - Fheight) / 2);
if (cbbPosition.SelectedIndex == 0)//正中
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth) / 2, (int)(BigBt.Height - Fheight) / 2);
}
if (cbbPosition.SelectedIndex == 1)//左上
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, 30);
}
if (cbbPosition.SelectedIndex == 2)//左下
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, 30, (int)(BigBt.Height - Fheight)-30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), Fheight);
}
if (cbbPosition.SelectedIndex == 4)//右下
{
g1.DrawString(txt, new Font(FontF, fontSize, fontStyle), b, (int)(BigBt.Width - Fwidth), (int)(BigBt.Height - Fheight)-30);
}
}
if (i == 1)
{
string ipath;
if (NewFolderPath.Length == 3)
ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") 1);
else
ipath = NewFolderPath;
string imgstype = Iname.Substring(Iname.LastIndexOf(".") 1, Iname.Length - 1 - Iname.LastIndexOf("."));
if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Jpeg);
}
if (imgstype.ToLower() == "png")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Png);
}
if (imgstype.ToLower() == "bmp")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Bmp);
}
if (imgstype.ToLower() == "gif")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Gif);
}
g1.Dispose();
BigBt.Dispose();
}
if (i == 2)
{
if (cbbPosition.SelectedIndex == 0)//正中
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);
}
if (cbbPosition.SelectedIndex == 1)//左上
{
g1.DrawImage(effect, 30, 30);
}
if (cbbPosition.SelectedIndex == 2)//左下
{
g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, 30);
}
if (cbbPosition.SelectedIndex == 4)//右下
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width)-30, (int)(BigBt.Height - effect.Height) - 30);
}
}
if (i == 3)
{
if (cbbPosition.SelectedIndex == 0)//正中
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width) / 2, (int)(BigBt.Height - effect.Height) / 2);
}
if (cbbPosition.SelectedIndex == 1)//左上
{
g1.DrawImage(effect, 30, 30);
}
if (cbbPosition.SelectedIndex == 2)//左下
{
g1.DrawImage(effect, 30, (int)(BigBt.Height - effect.Height) - 30);
}
if (cbbPosition.SelectedIndex == 3) //右上
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), 30);
}
if (cbbPosition.SelectedIndex == 4)//右下
{
g1.DrawImage(effect, (int)(BigBt.Width - effect.Width), (int)(BigBt.Height - effect.Height) - 30);
}
string ipath;
if (NewFolderPath.Length == 3)
ipath = NewFolderPath.Remove(NewFolderPath.LastIndexOf(":") 1);
else
ipath = NewFolderPath;
string imgstype = Iname.Substring(Iname.LastIndexOf(".") 1, Iname.Length - 1 - Iname.LastIndexOf("."));
if (imgstype.ToLower() == "jpeg" || imgstype.ToLower() == "jpg")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Jpeg);
}
if (imgstype.ToLower() == "png")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Png);
}
if (imgstype.ToLower() == "bmp")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Bmp);
}
if (imgstype.ToLower() == "gif")
{
BigBt.Save(ipath "\\_" Iname, ImageFormat.Gif);
}
}
}
private void txtWaterMarkFont_TextChanged(object sender, EventArgs e)
{
if (lbImgList.Items.Count <= 0)
{
MessageBox.Show("请加载图片","警告",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
else
{
AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[0].ToString(), 0);
pbImgPreview.Image = bt;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 调节透明度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
Bitmap effect;
Bitmap source;
Image new_img;
private void ChangeAlpha()
{
pbImgPreview.Refresh();
source = new Bitmap(Image.FromFile(txtWaterMarkImg.Text.Trim()));
if (source.Width <= 368)
effect = new Bitmap(368, 75);
else
{
Image.GetThumbnailImageAbort callb = null;
//对水印图片生成缩略图,缩小到原图得1/4
new_img = source.GetThumbnailImage(source.Width / 4, source.Width / 4, callb, new System.IntPtr());
effect = new Bitmap(this.new_img.Width, this.new_img.Height);
}
Graphics _effect = Graphics.FromImage(effect);
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,0,0},
new float[]{0,0,0,trackBar1.Value/255f,1}};
ColorMatrix imgMatrix = new ColorMatrix(matrixItems);
ImageAttributes imgEffect = new ImageAttributes();
imgEffect.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
if (source.Width <= 368)
{
_effect.DrawImage(source, new Rectangle(0, 0, 368, 75), 0, 0, 368, 75, GraphicsUnit.Pixel, imgEffect);
}
else
{
_effect.DrawImage(new_img, new Rectangle(0, 0, new_img.Width, new_img.Height), 0, 0, new_img.Width, new_img.Height, GraphicsUnit.Pixel, imgEffect);
}
pbImgPreview.Image = effect;
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
if(rbPIC.Checked&&txtWaterMarkImg.Text.Trim()!="")
ChangeAlpha();
}
private void btnPerform_Click(object sender, EventArgs e)
{
if (rbTxt.Checked&&txtSavaPath.Text!=""&&txtWaterMarkFont.Text!="")
{
for (int i = 0; i < lbImgList.Items.Count; i )
{
AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(), 1);
}
MessageBox.Show("添加水印成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
if (rbPIC.Checked && txtSavaPath.Text != "" && pbImgPreview.Image != null)
{
for (int i = 0; i < lbImgList.Items.Count; i )
{
AddFontWatermark(txtWaterMarkFont.Text.Trim(), lbImgList.Items[i].ToString(),3);
}
MessageBox.Show("添加水印成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
string NewFolderPath;
private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtSavaPath.Text = folderBrowserDialog1.SelectedPath;
NewFolderPath = txtSavaPath.Text.Trim();
}
}
}
标签: 水印
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)