110,536
社区成员
发帖
与我相关
我的任务
分享
if(tag == 0) //指定预览.
g.Clear(Color.LightBlue); //画布背景色.
时,跑到 Main中说:
Application.Run(new Form1());
报错说,Parameter is invalid...什么情况...using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FontWatermarkDemo {
public partial class Form1: Form {
#region 字体成员及其图片元素.
//默认字体.
private float fontSize = 8;
private FontStyle fontStyle = FontStyle.Regular;
private Color fontColor = Color.Black;
//需要指定.
private FontFamily fontFamily = null;
private Font font = null;
private Bitmap bMapPreview; //预览图片.
private Bitmap bMapSave; //保存的图片.
#endregion
public Form1() {
InitializeComponent();
//退出.
btnExit.Click += (sender, e) => { this.Dispose(); };
}
//加载图片.
private void btnLoadImg_Click(object sender, EventArgs e) {
openFD.Filter = "图片格式|*.jpeg;*.jpg;*.png;*.bmp;*.gif"; //过滤文件.
if(openFD.ShowDialog() != DialogResult.OK) return;
foreach(var file in openFD.FileNames) { //将FileInfo对象加到ListBox.
OwnFileInfo oFile = new OwnFileInfo(file);
lboxImgList.Items.Add(oFile);
}
}
//检查是否已加载图片.
private void txtWatermarkFontInput_TextChanged(object sender, EventArgs e) {
if(lboxImgList.Items.Count <= 0) {
MessageBox.Show("请先加载图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
OwnFileInfo file = lboxImgList.Items[0] as OwnFileInfo;
if(file != null)
FontWatermark(txtWatermarkFontInput.Text.Trim(), file, 0);
}
//设置字体.
private void btnSetFont_Click(object sender, EventArgs e) {
fontD.ShowApply = true; //显示"应用"按钮.
fontD.ShowHelp = false; //不显示"帮助"按钮.
if(fontD.ShowDialog() != DialogResult.No) { //响应"应用"或"确定"按钮.
Font fontNew = fontD.Font;
fontSize = fontNew.Size;
fontStyle = fontNew.Style;
fontFamily = fontNew.FontFamily;
fontColor = fontD.Color;
}
OwnFileInfo file = lboxImgList.Items[0] as OwnFileInfo;
if(file != null)
FontWatermark(txtWatermarkFontInput.Text.Trim(), file, 0);
}
//水印文字的显示方法.
/// <summary>
/// 水印文字.
/// </summary>
/// <param name="strInput">输入的文字.</param>
/// <param name="imgFullPath">水印图片的完整路径.</param>
/// <param name="tag">标识,如"0"表示预览不保存,"1"表示预览保存.</param>
void FontWatermark(string strInput, OwnFileInfo file, int tag) {
SolidBrush sBrush = new SolidBrush(fontColor); //画文字的刷子.
switch(tag) {
case 0:
using(bMapPreview = new Bitmap(picBoxWatermarkPreview.Width, picBoxWatermarkPreview.Height)) {
FontWatermarkDealSaveOrNot(bMapPreview, sBrush, strInput, file, 0);
picBoxWatermarkPreview.Image = bMapPreview; //用PictureBox显示临时图片.
}
break;
case 1:
using(bMapSave = new Bitmap(Image.FromFile(file.FileName))) {
FontWatermarkDealSaveOrNot(bMapSave, sBrush, strInput, file, 1);
}
break;
default:
throw new ArgumentException("unknow tag");
}
}
//水印文字的显示,处理是否保存的内部方法.
void FontWatermarkDealSaveOrNot(Image bMap, Brush brush, string strInput, OwnFileInfo file, int tag) {
using(Graphics g = Graphics.FromImage(bMap)) {
SizeF sizeFont; //字体宽高.
int fWidth; //字体宽度.
int fHeight; //字体的高度.
if(fontFamily == null) { //未设定字体.
font = new Font(strInput, fontSize);
sizeFont = g.MeasureString(strInput, font);
fWidth = (int)sizeFont.Width;
fHeight = (int)sizeFont.Height;
}
else {
font = new Font(fontFamily, fontSize, fontStyle);
sizeFont = g.MeasureString(strInput, font);
fWidth = (int)sizeFont.Width;
fHeight = (int)sizeFont.Height;
}
//画字体的位置.
Point fontPosition = new Point((bMap.Width - fWidth) / 2, (bMap.Height - fHeight) / 2);
if(tag == 0) //指定预览.
g.Clear(Color.LightBlue); //画布背景色.
g.DrawString(strInput, font, brush, fontPosition);
if(tag == 1) //指定保存.
bMapSave = new Bitmap(Image.FromFile(file.FileName));
}
}
//存储要保存图片的路径.
private void btnSavePathBrowse_Click(object sender, EventArgs e) {
if(folderBD.ShowDialog() != DialogResult.OK) return;
txtSavePath.Text = folderBD.SelectedPath;
}
//确定保存.
private void btnConfirm_Click(object sender, EventArgs e) {
foreach(var f in lboxImgList.Items) {
OwnFileInfo file = f as OwnFileInfo;
if(file != null)
FontWatermark(txtWatermarkFontInput.Text.Trim(), file, 1);
}
}
}
}