/// <summary>
/// 处理水印图片的水印
/// </summary>
/// <param name="path">带有水印的图片路径</param>
/// <returns>返回处理好水印的图片</returns>
private Image OperationPhoto(string path)
{
Image p_w_picpath = null;
XMLCollectPhotoSet xMLCollectPhotoSet = new XMLCollectPhotoSet();
CollectPhotoSetInfo collectPhotoSetInfo = new CollectPhotoSetInfo();
collectPhotoSetInfo = xMLCollectPhotoSet.Get();
string pathNew=collectPhotoSetInfo.ConverPhoto;
if (collectPhotoSetInfo.CutWater == "yes")
{
//截取水印
p_w_picpath=CutWater(path);
}
else if (collectPhotoSetInfo.CutWater == "no")
{
//图片覆盖水印
if (collectPhotoSetInfo.ConverWater == "yes")
{
Image p_w_picpath1 = Image.FromFile(path);
Image p_w_picpathNew = Image.FromFile(collectPhotoSetInfo.ConverPhoto);
p_w_picpath = ConverWater(p_w_picpath1, p_w_picpathNew);
//p_w_picpath1.Dispose();
//p_w_picpathNew.Dispose();
}
else if (collectPhotoSetInfo.ConverWater == "no")
{
Image p_w_picpath1 = Image.FromFile(path);
p_w_picpath = p_w_picpath1;
}
}
//图片覆盖水印和添加图片水印没有任何关系 既可以同时存在也可以分开存在
if (collectPhotoSetInfo.UsingD == "yes")
{
//处理p_w_picpath图片
if (p_w_picpath == null)
{
Image p_w_picpath1 = Image.FromFile(path);
p_w_picpath = p_w_picpath1;
}
Image p_w_picpathWater = p_w_picpath;
string rMarkImgPath=collectPhotoSetInfo.WaterPhoto;//水印图片
string textStr = collectPhotoSetInfo.WaterText;//水印文字
Font f = new Font(collectPhotoSetInfo.FontFamily, collectPhotoSetInfo.FontSize);
Brush b = new SolidBrush(collectPhotoSetInfo.FontColor);
float opacity = collectPhotoSetInfo.Opacity;
p_w_picpath = BulidWatermarkPhoto(p_w_picpath, rMarkImgPath, opacity, textStr, f, b);//iamge被操作的图片rMarkImgPath水印图片0.6f透明度textStr文字f字体b字体颜色
}
return p_w_picpath;
}
/// <summary>
/// 去掉水印
/// </summary>
/// <param name="path">带有水印的图片</param>
/// <returns>去掉水印后的图片</returns>
private Image CutWater(string path)
{
Bitmap sourceBitmap = new Bitmap(path);
int width = sourceBitmap.Width;
int height = sourceBitmap.Height;
Bitmap resultBitmap = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(resultBitmap))
{
Rectangle resultRectangle = new Rectangle(0, 0, width, height);
Rectangle sourceRectangle = new Rectangle(0 + 1, 0 + 1,width-200, height);
g.DrawImage(sourceBitmap, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return resultBitmap;
}
/// <summary>
/// 用图片覆盖水印
/// </summary>
/// <param name="path">水印图片</param>
/// <param name="pathNew">覆盖图片</param>
/// <returns>覆盖掉水印的图片</returns>
private Image ConverWater(Image p_w_picpath, Image p_w_picpathNew)
{
try
{
int width = p_w_picpath.Width;
int height = p_w_picpath.Height;
Graphics g = Graphics.FromImage(p_w_picpath);
g.DrawImage(p_w_picpathNew, new Rectangle(width - 230, height - 180, 250, 180), 0, 0, p_w_picpath.Width, p_w_picpath.Height, GraphicsUnit.Pixel);
g.Dispose();
}
catch(Exception ex)
{
}
return p_w_picpath;
}
/// <summary>
/// 制作水印图片(文字、图片或者文字和图片)
/// </summary>
/// <param name="picImage">原始图片</param>
/// <param name="rMarkImgPath">所需水印图片</param>
/// <param name="opacityvalue">透明度</param>
/// /// <param name="textStr">文字</param>
/// <param name="font">字体</param>
/// <param name="brush">刷子</param>
public Image BulidWatermarkPhoto(Image picImage, string rMarkImgPath, float opacityvalue, string textStr, Font font, Brush brush)
{
Dictionary<string, string> dicPaths = new Dictionary<string, string>();
//解析水印图片的路径
if (rMarkImgPath != null && rMarkImgPath.Length > 0)
{
string[] paths = rMarkImgPath.Split(';');
for (int i = 0; i < paths.Length; i++)
{
string path = paths[i].Split(',')[0];
string name = paths[i].Split(',')[1];
dicPaths.Add(name, path);
}
}
Dictionary<string, string> dicTexts = new Dictionary<string, string>();
//解析文字
if (textStr != null && textStr.Length > 0)
{
string[] texts=textStr.Split(';');
for (int i = 0; i < texts.Length; i++)
{
string text = texts[i].Split(',')[0];
string name = texts[i].Split(',')[1];
dicTexts.Add(name, text);
}
}
Image p_w_picpath = picImage;
Dictionary<string, string> keyValue = DataCache.keyValue;
foreach (string key in keyValue.Keys)
{
Graphics g = Graphics.FromImage(p_w_picpath);
int x = Convert.ToInt32(keyValue[key].Split(',')[0]);
int y = Convert.ToInt32(keyValue[key].Split(',')[1]);
if (dicPaths != null && dicPaths.Count > 0)//判断是否有水印图片
{
Image copyImage = Image.FromFile(dicPaths[key]);//水印图片路径
copyImage = ChangeOpacity(copyImage, opacityvalue);//经过处理透明化的图片
g.DrawImage(copyImage, new Rectangle(x, y, 200, 200), 0, 0, p_w_picpath.Width, p_w_picpath.Height, GraphicsUnit.Pixel);//画上水印图片
}
if (dicTexts != null && dicTexts.Count > 0) //判断是否有水印字
{
Image txtImage = textImage(dicTexts[key], font, brush);//文字
txtImage = ChangeOpacity(txtImage, opacityvalue);
g.DrawImage(txtImage, new Rectangle(x, y, 200, 200), 50, 50, p_w_picpath.Width, p_w_picpath.Height, GraphicsUnit.Pixel);//画上水印文字
}
g.Dispose();
}
return p_w_picpath;
}
/// <summary>
/// 改变图片的透明度
/// </summary>
/// <param name="img">图片</param>
/// <param name="opacityvalue">透明度</param>
/// <returns></returns>
public static Bitmap ChangeOpacity(Image img, float opacityvalue)
{
float[][] nArray ={ 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, opacityvalue, 0},
new float[] {0, 0, 0, 0, 1}};
ColorMatrix matrix = new ColorMatrix(nArray);
ImageAttributes attributes = new ImageAttributes();
attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
Image srcImage = img;
Bitmap resultImage = new Bitmap(srcImage.Width, srcImage.Height);
Graphics g = Graphics.FromImage(resultImage);
g.DrawImage(srcImage, new Rectangle(0, 0, srcImage.Width, srcImage.Height), 0, 0, srcImage.Width, srcImage.Height, GraphicsUnit.Pixel, attributes);
return resultImage;
}
/// <summary>
/// 制作一个带有文字的透明图
/// </summary>
/// <param name="textStr">文字</param>
/// <param name="font">文字样式</param>
/// <param name="brush">刷子</param>
/// <returns>带有文字的透明图</returns>
private Image textImage(string textStr, Font font, Brush brush)
{
string path = Application.StartupPath+@"\collectionPhoto.jpg";
Image p_w_picpath = Image.FromFile(path);
p_w_picpath = ChangeOpacity(p_w_picpath, 0.0f);
Graphics g = Graphics.FromImage(p_w_picpath);
g.DrawString(textStr, font, brush, 60, 60);
g.Dispose();
Image p_w_picpathNew = p_w_picpath;
return p_w_picpathNew;
}
转载于:https://blog.51cto.com/iammoon24/1790017