public class WatermarkHelper
{
string _waterImgPath;
BitmapSource _waterPic;
string _prefix = null;
// constructors
public WatermarkHelper(string waterImgFilePath, string prefix = null)
{
this._waterImgPath = waterImgFilePath;
this._waterPic = new BitmapImage(new Uri(this._waterImgPath));
if (prefix == null)
prefix = "";
this._prefix = prefix;
// this._result = false;
}
// public members
public void AddWatermark_SaveToDir(string inputFilePath, string outputDir)
{
if (!Directory.Exists(outputDir))
Directory.CreateDirectory(outputDir);
string output_filepath = Path.Combine(
outputDir,
string.Format("{0}{1}", this._prefix, Path.GetFileName(inputFilePath))
); // <-- use Path.Combine to prevent potential problems
// process
Process(inputFilePath, output_filepath);
}
public void AddWatermark_SaveToFile(string inputFilePath, string output_filepath) // <-- you see what i mean when i say 'flexibility'
{
// create directory if it doesn't exist yet
string output_dir = Path.GetDirectoryName(output_filepath);
if (!Directory.Exists(output_dir))
Directory.CreateDirectory(output_dir);
// process
Process(inputFilePath, output_filepath);
}
// internal logic
bool Process(string input_filepath, string output_filepath) // <-- let upper logic determine the output filename, these will provide flexibility
WPF给图片加水印
最新推荐文章于 2024-07-02 00:05:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)