c#批量给图片添加水印, 想做一个小工具!批量给图片加上自己的透明图片水印和透明文字,求教.已经创建了一个带透明的png图片作为水印.如何合并到背景图片上.?求给代码和思路.谢谢c#图片bitmapgraphics 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 原来写的wpf的打水印的 楼主可以借鉴下public class WaterHelper { string _waterImgPath; BitmapSource _waterPic; string _prefix = null; // constructors public WaterHelper(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 AddWater_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 AddWater_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 { // load source image // BitmapSource bitmap = new BitmapImage(new Uri(input_filepath)); BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(input_filepath); bitmap.CacheOption = BitmapCacheOption.OnLoad; bitmap.EndInit(); // create render bitmap var rtbitmap = new RenderTargetBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Default); // draw water var drawingVisual = new DrawingVisual(); using (var dc = drawingVisual.RenderOpen()) { // draw source image dc.DrawImage(bitmap, new Rect(0, 0, bitmap.Width, bitmap.Height)); // determine the render size of the water double hs = bitmap.Height * 0.3 / this._waterPic.Height; double ws = bitmap.Width * 0.3 / this._waterPic.Width; double scale = hs > ws ? ws : hs; if (bitmap.Height / bitmap.Width > 5) { scale = bitmap.Width * 0.8 / this._waterPic.Width; } else if (bitmap.Width / bitmap.Height > 5) { scale = bitmap.Height * 0.8 / this._waterPic.Height; } double Wstart = bitmap.Width - this._waterPic.Width * scale - 20 < 0 ? 0 : bitmap.Width - this._waterPic.Width * scale - 20; double Hstart = bitmap.Height - this._waterPic.Height * scale; // draw water make dc.DrawImage(this._waterPic, new Rect(Wstart, Hstart, this._waterPic.Width * scale, this._waterPic.Height * scale)); } rtbitmap.Render(drawingVisual); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap)); // save image if (File.Exists(output_filepath)) File.Delete(output_filepath); using (FileStream file = new FileStream(output_filepath, FileMode.Create)) bitmapEncoder.Save(file); // return return true; } } #region 图片加上水印的操作 /// <summary> /// 向图片上写文字再输出 /// </summary> /// <param name="img"></param> /// <param name="text"></param> /// <returns></returns> public static Image WriteString(Image img, string text) { Bitmap bmap = new Bitmap(img); Graphics g = Graphics.FromImage(bmap); Font font = new Font("楷体", 15); PointF pf = new PointF();//((float)(img.Width/2-2.5),img.Height/2-2.5); pf.X = 0; pf.Y = 0; g.DrawString(text, font, Brushes.Gold, pf); g.Dispose(); return bmap; } /// <summary> /// 将图片加载水印图片 /// </summary> /// <param name="Img"></param> /// <param name="fromImg"></param> /// <returns></returns> public static Image WriteImg(Image Img, Image fromImg) { Bitmap b = new Bitmap(fromImg); Graphics g = Graphics.FromImage(b); g.DrawImage(Img, 0, 0, Img.Width, Img.Height); g.Dispose(); return b; } #endregion SqlDataReader读取数据。 谁能提供一个 能从 excle 读取数据 到 数据库的 方法…… 这是不是一道面试题,请讲解一下他的过程。 一个小程序,看不太懂,帮忙。 问一个dataTable和Excel的问题! 如何把dataGridView中的某一行指定為當前行。 菜鸟问题,数据读入数组出错!!! 如何对类XML格式数据流操作 利用扩展方法求一个平均值的问题 C# winform WebBrowser中弹出div层图片的问题 Lucene如何缓存共享一个New IndexSearcher() WPF,这个剪辑功能如何实现?
public class WaterHelper
{
string _waterImgPath;
BitmapSource _waterPic;
string _prefix = null; // constructors
public WaterHelper(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 AddWater_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 AddWater_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
{
// load source image
// BitmapSource bitmap = new BitmapImage(new Uri(input_filepath));
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(input_filepath);
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit(); // create render bitmap
var rtbitmap = new RenderTargetBitmap(bitmap.PixelWidth,
bitmap.PixelHeight,
bitmap.DpiX,
bitmap.DpiY,
PixelFormats.Default);
// draw water
var drawingVisual = new DrawingVisual();
using (var dc = drawingVisual.RenderOpen())
{
// draw source image
dc.DrawImage(bitmap, new Rect(0, 0, bitmap.Width, bitmap.Height));
// determine the render size of the water
double hs = bitmap.Height * 0.3 / this._waterPic.Height;
double ws = bitmap.Width * 0.3 / this._waterPic.Width;
double scale = hs > ws ? ws : hs;
if (bitmap.Height / bitmap.Width > 5)
{
scale = bitmap.Width * 0.8 / this._waterPic.Width;
}
else if (bitmap.Width / bitmap.Height > 5)
{
scale = bitmap.Height * 0.8 / this._waterPic.Height;
}
double Wstart = bitmap.Width - this._waterPic.Width * scale - 20 < 0 ? 0 : bitmap.Width - this._waterPic.Width * scale - 20;
double Hstart = bitmap.Height - this._waterPic.Height * scale;
// draw water make
dc.DrawImage(this._waterPic, new Rect(Wstart, Hstart, this._waterPic.Width * scale, this._waterPic.Height * scale));
}
rtbitmap.Render(drawingVisual);
var bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap)); // save image
if (File.Exists(output_filepath))
File.Delete(output_filepath); using (FileStream file = new FileStream(output_filepath, FileMode.Create))
bitmapEncoder.Save(file); // return
return true;
}
}
#region 图片加上水印的操作 /// <summary>
/// 向图片上写文字再输出
/// </summary>
/// <param name="img"></param>
/// <param name="text"></param>
/// <returns></returns>
public static Image WriteString(Image img, string text)
{
Bitmap bmap = new Bitmap(img);
Graphics g = Graphics.FromImage(bmap);
Font font = new Font("楷体", 15);
PointF pf = new PointF();//((float)(img.Width/2-2.5),img.Height/2-2.5);
pf.X = 0;
pf.Y = 0; g.DrawString(text, font, Brushes.Gold, pf);
g.Dispose(); return bmap;
}
/// <summary>
/// 将图片加载水印图片
/// </summary>
/// <param name="Img"></param>
/// <param name="fromImg"></param>
/// <returns></returns>
public static Image WriteImg(Image Img, Image fromImg)
{
Bitmap b = new Bitmap(fromImg);
Graphics g = Graphics.FromImage(b);
g.DrawImage(Img, 0, 0, Img.Width, Img.Height);
g.Dispose();
return b;
} #endregion