关于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;

}

时间: 2024-08-01 13:15:02

关于C#对图片部分操作(水印、透明度)的相关文章

Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理

前言 java开发中经常遇到对图片的处理,JDK中也提供了对应的工具类,不过处理起来很麻烦,Thumbnailator是一个优秀的图片处理的开源Java类库,处理效果远比Java API的好,从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量.还支持对一个目录的所有图片进行批量处理操作,下边就和大家分享一下java中用Thumbnailator做图片各种处理的方法(相关jar包可在最

photoshop去除图片上的水印

很多素材图片上都被加上了各种各样的水印,而我们偏偏非常需要这张图片,又有很多PSer认为水印是无法去除的,只能使用图章.涂抹来修补,如果遇到水印覆盖部分的色彩复杂的图片,也只能望图兴叹了. 其实在光学的理论上来说,水印图片是可以完美还原的,我们这里不谈理论,只谈操作. 来看一个加了水印的图片. 要想去除水印,我们要确定两点,第一,水印的原始颜色是什么?第二,透明度是多少? 这两点确实是问题,很大的问题,所以,我会另写一文来说明.这里,我们来猜测一下,从图上看,水印的颜色可能是红色(FF0000)

php给图片添加文字水印

PHP对图片的操作用到GD库,这里我们介绍如何给图片添加文字水印. 大致分为四步: 1.打开图片 2.操作图片 3.输出图片 4.销毁图片 下面我们上代码来具体讲解每步的实现过程: <?php /*打开图片*/ //1.配置图片路径 $src = "bg.jpg"; //2.获取图片信息 $info = getimagesize($src); //3.通过编号获取图像类型 $type = image_type_to_extension($info[2],false); //4.在

php给图片加文字水印

<? php /*给图片加文字水印的方法*/ $dst_path = 'http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg'; $dst = imagecreatefromstring(file_get_contents($dst_path)); /*imagecreatefromstring()--从字符串中的图像流新建一个图像,返回一个图像标示符.其表达了从给定字符串得来的图像 图像格式将自己主动监測,仅仅要php支持jpeg,png,

php给图片添加文字水印方法汇总

在php中要给图片加水印我们需要给php安装GD库了,这里我们不介绍GD库安装,只介绍怎么利用php给图片添加文字水印的4种方法的汇总.有需要的小伙伴可以参考下. 1: 面向过程的编写方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image_type_to_ex

android图像处理系列之七--图片涂鸦,水印-图片叠加

图片涂鸦和水印其实是一个功能,实现的方式是一样的,就是一张大图片和一张小点图片叠加即可.前面在android图像处理系列之六--给图片添加边框(下)-图片叠加中也讲到了图片叠加,里面实现的原理是直接操作像素点.下面给出别外一种方式让图片叠加--用Canvas处理图片,canvas已经封装好了,直接调用就行. 下面看效果: += 代码: [java] view plain copy /** * 组合涂鸦图片和源图片 * @param src 源图片 * @param watermark 涂鸦图片

php图片添加文字水印方法汇总

方法一: <?php header("content-type:text/html;charset=utf-8"); //指定图片路径 $src = "img/a.png"; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image_type_to_extension($info[2],false); // echo $type; // exit; //动态的把图片导入内存中 $fun = &qu

图片上的水印怎么去除,去水印工具有什么

社会在快速发展,很多人越来越注重版权,所以不管是图片还是视频等等都会添加水印,当我们看到好看的图片或者有意思的视频想保存下来的时候,会看到有水印很不美观,这时候是不是就会有要将水印去掉的冲动了,那图片上的水印怎么去除,去水印工具有什么呢? 1.首先我们常用的可能是手机中的修图工具,这样会有些麻烦,现在有专门的去水印工具,可以一键去除不想要的部分,操作简单而且方便了不少:2.电脑上安装迅捷去水印软件,可以看到界面很清晰,一目了然,有两个选择"图片去水印"和"视频去水印"

拍照党福利驾到 华为云微认证教你实现图片压缩和水印添加

在手机拍照成为日常的今天,用照片记录生活已成为人们的一种习惯.拍照容易处理难,面对手机相册中大量的照片,你是否也苦恼过?删,舍不得:上传,会不会被盗图?能否发出足够个性的图片称霸朋友圈?不用担心,华为云微认证结合函数工作流服务,教你轻松实现图片压缩和水印添加,让您体验函数工作流带来的高资源利用率和高处理效率,让即便是无照不活的拍照党也再无后顾之忧. 云服务助力图片存储及管理一般而言,我们压缩图片有四种方法:使用图片压缩软件.网页在线图片压缩.自设图片压缩代码.使用云服务批量压缩.水印添加一般也有