扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
///
目前成都创新互联已为近1000家的企业提供了网站建设、域名、雅安服务器托管、网站托管、企业网站设计、团风网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
/// 处理水印图片的水印
///
/// 带有水印的图片路径
///
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_picpath2 = Image.FromFile(path);
Image p_w_picpathNew = Image.FromFile(collectPhotoSetInfo.ConverPhoto);
p_w_picpath = ConverWater(p_w_picpath2, p_w_picpathNew);
//p_w_picpath2.Dispose();
//p_w_picpathNew.Dispose();
}
else if (collectPhotoSetInfo.ConverWater == "no")
{
Image p_w_picpath2 = Image.FromFile(path);
p_w_picpath = p_w_picpath2;
}
}
//图片覆盖水印和添加图片水印没有任何关系 既可以同时存在也可以分开存在
if (collectPhotoSetInfo.UsingD == "yes")
{
//处理p_w_picpath图片
if (p_w_picpath == null)
{
Image p_w_picpath2 = Image.FromFile(path);
p_w_picpath = p_w_picpath2;
}
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;
}
///
/// 去掉水印
///
/// 带有水印的图片
///
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;
}
///
/// 用图片覆盖水印
///
/// 水印图片
/// 覆盖图片
///
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;
}
///
/// 制作水印图片(文字、图片或者文字和图片)
///
/// 原始图片
/// 所需水印图片
/// 透明度
/// /// 文字
/// 字体
/// 刷子
public Image BulidWatermarkPhoto(Image picImage, string rMarkImgPath, float opacityvalue, string textStr, Font font, Brush brush)
{
Dictionary
//解析水印图片的路径
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
//解析文字
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
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;
}
///
/// 改变图片的透明度
///
/// 图片
/// 透明度
///
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;
}
///
/// 制作一个带有文字的透明图
///
/// 文字
/// 文字样式
/// 刷子
///
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;
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流