关于C#对图片部分操作(水印透明度)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于C#对图片部分操作(水印透明度)相关的知识,希望对你有一定的参考价值。
/// <summary>
/// 处理水印图片的水印
/// </summary>
/// <param name="path">带有水印的图片路径</param>
/// <returns>返回处理好水印的图片</returns>
private Image OperationPhoto(string path)
{
Image image = null;
XMLCollectPhotoSet xMLCollectPhotoSet = new XMLCollectPhotoSet();
CollectPhotoSetInfo collectPhotoSetInfo = new CollectPhotoSetInfo();
collectPhotoSetInfo = xMLCollectPhotoSet.Get();
string pathNew=collectPhotoSetInfo.ConverPhoto;
if (collectPhotoSetInfo.CutWater == "yes")
{
//截取水印
image=CutWater(path);
}
else if (collectPhotoSetInfo.CutWater == "no")
{
//图片覆盖水印
if (collectPhotoSetInfo.ConverWater == "yes")
{
Image image1 = Image.FromFile(path);
Image imageNew = Image.FromFile(collectPhotoSetInfo.ConverPhoto);
image = ConverWater(image1, imageNew);
//image1.Dispose();
//imageNew.Dispose();
}
else if (collectPhotoSetInfo.ConverWater == "no")
{
Image image1 = Image.FromFile(path);
image = image1;
}
}
//图片覆盖水印和添加图片水印没有任何关系 既可以同时存在也可以分开存在
if (collectPhotoSetInfo.UsingD == "yes")
{
//处理image图片
if (image == null)
{
Image image1 = Image.FromFile(path);
image = image1;
}
Image imageWater = image;
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;
image = BulidWatermarkPhoto(image, rMarkImgPath, opacity, textStr, f, b);//iamge被操作的图片rMarkImgPath水印图片0.6f透明度textStr文字f字体b字体颜色
}
return image;
}
/// <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 image, Image imageNew)
{
try
{
int width = image.Width;
int height = image.Height;
Graphics g = Graphics.FromImage(image);
g.DrawImage(imageNew, new Rectangle(width - 230, height - 180, 250, 180), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
g.Dispose();
}
catch(Exception ex)
{
}
return image;
}
/// <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 image = picImage;
Dictionary<string, string> keyValue = DataCache.keyValue;
foreach (string key in keyValue.Keys)
{
Graphics g = Graphics.FromImage(image);
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, image.Width, image.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, image.Width, image.Height, GraphicsUnit.Pixel);//画上水印文字
}
g.Dispose();
}
return image;
}
/// <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 = [email protected]"\collectionPhoto.jpg";
Image image = Image.FromFile(path);
image = ChangeOpacity(image, 0.0f);
Graphics g = Graphics.FromImage(image);
g.DrawString(textStr, font, brush, 60, 60);
g.Dispose();
Image imageNew = image;
return imageNew;
}
本文出自 “学习程序让我想到数学..” 博客,请务必保留此出处http://iammoon24.blog.51cto.com/2552968/1790017
以上是关于关于C#对图片部分操作(水印透明度)的主要内容,如果未能解决你的问题,请参考以下文章