///
目前成都創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、團(tuán)風(fēng)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
/// 處理水印圖片的水印
///
/// 帶有水印的圖片路徑
///
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;
}
}
//圖片覆蓋水印和添加圖片水印沒有任何關(guān)系 既可以同時(shí)存在也可以分開存在
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);//經(jīng)過處理透明化的圖片
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;
}
///
/// 制作一個(gè)帶有文字的透明圖
///
/// 文字
/// 文字樣式
/// 刷子
///
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;
}