给图片添加文字水印

public void ImageWaterMarkText(string filename, ImageFormat format)
        {
            Image originalImage = Image.FromFile(filename);
            Graphics g = Graphics.FromImage(originalImage);

            Font drawFont = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);           //找到图片旋转中点
            float xpos = originalImage.Width > originalImage.Height ? originalImage.Width -  originalImage.Height/ 2 : originalImage.Width / 2;
            float ypos = originalImage.Width > originalImage.Height ? originalImage.Height / 2 : originalImage.Height - originalImage.Width / 2;

            var triggleLeg = (originalImage.Width > originalImage.Height ? originalImage.Height : originalImage.Width) / 3;
            ar gPath = new GraphicsPath();
            gPath.AddLine(originalImage.Width - triggleLeg, originalImage.Height, originalImage.Width, originalImage.Height - triggleLeg);
            gPath.AddLine(originalImage.Width, originalImage.Height - triggleLeg, originalImage.Width, originalImage.Height);
            gPath.AddLine(originalImage.Width, originalImage.Height, originalImage.Width - triggleLeg, originalImage.Height);
            g.FillPath(new SolidBrush(Color.FromArgb(120, Color.Black)), gPath);
            
            g.TranslateTransform(xpos, ypos);
            g.RotateTransform(-45);

            SizeF crSize;
            string watermarkText;                        watermarkText = "测试文字";
            crSize = g.MeasureString("Chine LianTong", drawFont);

            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Red), -crSize.Width / 2, 250);

            watermarkText = DateTime.Now.ToString("Current: yyyy-MM-dd HH:mm");
            g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Red), -crSize.Width / 2, 250 + crSize.Height + 5);

            g.Dispose();

            MemoryStream stream = new MemoryStream();
            originalImage.Save(stream, format);
            originalImage.Dispose();

            Image img = Image.FromStream(stream);
            img.Save(filename);
            img.Dispose();

        }
时间: 2024-08-06 11:39:37

给图片添加文字水印的相关文章

php 图片添加文字水印 以及 图片合成(微信快码传播)

1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPath)); $font = 'msyhl.ttc';//字体 $black = imagecolorallocate($img, 0, 0, 0);//字体颜色 RGB $fontSize = 20; //字体大小 $circleSize = 60; //旋转角度 $left = 50; //左边距

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

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

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 /** * @desc 图片处理类 */ class Pic{ private $info; private $res; public $thumb_pic; public function __construct($picPath){ //获取图片信息 $this->info = getimagesize($picPath); //获取图片名 $this->info['type'] = image_type_to_extension($this->info[2],fa

PIL图片添加文字水印

python PIL图像处理中添加文字水印 代码如下: 运行效果图: 原文地址:https://www.cnblogs.com/VYao/p/9753502.html

在PHP中给图片添加文字水印

<?php if (function_exists('imagepng')) { dir('GD库不存在'); } //图片路径 $imagePath = './img/a.jpg'; //获取文件类型 $imageInfo = getimagesize($imagePath); $imageExtension = image_type_to_extension($imageInfo[2], false); //获取图片 $func = 'imagecreatefrom' . $imageExt

为图片添加文字水印

<?php $filename="des_big.jpg"; $fileInfo=getimagesize($filename); //echo $fileInfo; echo "<br>"; $mime=$fileInfo['mime']; //echo $mime; echo "<br>"; $createFun=str_replace("/","createfrom",$

php给图片加文字水印

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