c#给图片添加文字的代码小结

代码实例一


代码如下:

using System;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Imag_writer
{
/// <summary>
/// 水印的类型
/// </summary>
public enum WaterMarkType
{
   /// <summary>
   /// 文字水印
   /// </summary>
   TextMark,
   /// <summary>
   /// 图片水印
   /// </summary>
   //ImageMark // 暂时只能添加文字水印
};
/// <summary>
/// 水印的位置
/// </summary>
public enum WaterMarkPosition
{
   /// <summary>
   /// 左上角
   /// </summary>
   WMP_Left_Top,
   /// <summary>
   /// 左下角
   /// </summary>
   WMP_Left_Bottom,
   /// <summary>
   /// 右上角
   /// </summary>
   WMP_Right_Top,
   /// <summary>
   /// 右下角
   /// </summary>
   WMP_Right_Bottom
};
/// <summary>
/// 处理图片的类(包括加水印,生成缩略图)
/// </summary>
public class ImageWaterMark
{
   public ImageWaterMark()
   {
    //
    // TODO: 在此处添加构造函数逻辑
    //
   }
   #region 给图片加水印
   /// <summary>
   /// 添加水印(分图片水印与文字水印两种)
   /// </summary>
   /// <param name="oldpath">原图片绝对地址</param>
   /// <param name="newpath">新图片放置的绝对地址</param>
   /// <param name="wmtType">要添加的水印的类型</param>
   /// <param name="sWaterMarkContent">水印内容,若添加文字水印,此即为要添加的文字;
   /// 若要添加图片水印,此为图片的路径</param>
   public void addWaterMark(string oldpath, string newpath,
    WaterMarkType wmtType, string sWaterMarkContent)
   {
    try
    {
     Image image = Image.FromFile(oldpath);
     Bitmap b = new Bitmap(image.Width, image.Height,
      PixelFormat.Format24bppRgb);
     Graphics g = Graphics.FromImage(b);
     g.Clear(Color.White);
     g.SmoothingMode = SmoothingMode.HighQuality;
     g.InterpolationMode = InterpolationMode.High;
     g.DrawImage(image, 0, 0, image.Width, image.Height);
     switch (wmtType)
     {
      case WaterMarkType.TextMark:
       //文字水印
       this.addWatermarkText(g, sWaterMarkContent, "WM_BOTTOM_RIGHT",
        image.Width, image.Height);
       break;
     }
     b.Save(newpath);
     b.Dispose();
     image.Dispose();
    }
    catch
    {
     if(File.Exists(oldpath))
     {
      File.Delete(oldpath);
     }
    }
    finally
    {
     if(File.Exists(oldpath))
     {
      File.Delete(oldpath);
     }
    }
   }
   /// <summary>
   ///   加水印文字
   /// </summary>
   /// <param name="picture">imge 对象</param>
   /// <param name="_watermarkText">水印文字内容</param>
   /// <param name="_watermarkPosition">水印位置</param>
   /// <param name="_width">被加水印图片的宽</param>
   /// <param name="_height">被加水印图片的高</param>
   private void addWatermarkText(Graphics picture, string _watermarkText,
    string _watermarkPosition, int _width, int _height)
   {
    // 确定水印文字的字体大小
    int[] sizes = new int[]{32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4};
    Font crFont = null;
    SizeF crSize = new SizeF();
    for (int i = 0;i < sizes.Length; i++)
    {
     crFont = new Font("Arial Black", sizes[i], FontStyle.Bold);
     crSize = picture.MeasureString(_watermarkText, crFont);
     if((ushort)crSize.Width < (ushort)_width)
     {
      break;
     }
    }
    // 生成水印图片(将文字写到图片中)
    Bitmap floatBmp = new Bitmap((int)crSize.Width + 3,
          (int)crSize.Height + 3, PixelFormat.Format32bppArgb);
    Graphics fg=Graphics.FromImage(floatBmp);
    PointF pt=new PointF(0,0);
    // 画阴影文字
    Brush TransparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
    Brush TransparentBrush1 = new SolidBrush(Color.FromArgb(255, Color.Black));
    fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X, pt.Y + 1);
    fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X + 1, pt.Y);
    fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 1, pt.Y + 1);
    fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X, pt.Y + 2);
    fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 2, pt.Y);
    TransparentBrush0.Dispose();
    TransparentBrush1.Dispose();
    // 画文字
    fg.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    fg.DrawString(_watermarkText,
     crFont, new SolidBrush(Color.White),
     pt.X, pt.Y, StringFormat.GenericDefault);
    // 保存刚才的操作
    fg.Save();
    fg.Dispose();
    // floatBmp.Save("d:\\WebSite\\DIGITALKM\\ttt.jpg");
    // 将水印图片加到原图中
    this.addWatermarkImage(
     picture,
     new Bitmap(floatBmp),
     "WM_BOTTOM_RIGHT",
     _width,
     _height);
   }
   /// <summary>
   ///   加水印图片
   /// </summary>
   /// <param name="picture">imge 对象</param>
   /// <param name="iTheImage">Image对象(以此图片为水印)</param>
   /// <param name="_watermarkPosition">水印位置</param>
   /// <param name="_width">被加水印图片的宽</param>
   /// <param name="_height">被加水印图片的高</param>
   private void addWatermarkImage( Graphics picture,Image iTheImage,
    string _watermarkPosition,int _width,int _height)
   {
    Image watermark = new Bitmap(iTheImage);
    ImageAttributes imageAttributes = new ImageAttributes();
    ColorMap colorMap = new ColorMap();
    colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
    colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
    ColorMap[] remapTable = {colorMap};
    imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
    float[][] colorMatrixElements = {
             new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
             new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
             new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
             new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
             new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
            };
    ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
    imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
    int xpos = 0;
    int ypos = 0;
    int WatermarkWidth = 0;
    int WatermarkHeight = 0;
    double bl = 1d;
    //计算水印图片的比率
    //取背景的1/4宽度来比较
    if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
    {
     bl = 1;
    }
    else if ((_width > watermark.Width * 4) && (_height<watermark.Height * 4))
    {
     bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
    }
    else if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
    {
     bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
    }
    else
    {
     if ((_width * watermark.Height) > (_height * watermark.Width))
     {
      bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
     }
     else
     {
      bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
     }
    }
    WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
    WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
    switch (_watermarkPosition)
    {
     case "WM_TOP_LEFT":
      xpos = 10;
      ypos = 10;
      break;
     case "WM_TOP_RIGHT":
      xpos = _width - WatermarkWidth - 10;
      ypos = 10;
      break;
     case "WM_BOTTOM_RIGHT":
      xpos = _width - WatermarkWidth - 10;
      ypos = _height -WatermarkHeight - 10;
      break;
     case "WM_BOTTOM_LEFT":
      xpos = 10;
      ypos = _height - WatermarkHeight - 10;
      break;
    }
    picture.DrawImage(
     watermark,
     new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight),
     0,
     0,
     watermark.Width,
     watermark.Height,
     GraphicsUnit.Pixel,
     imageAttributes);
    watermark.Dispose();
    imageAttributes.Dispose();
   }
   /// <summary>
   ///   加水印图片
   /// </summary>
   /// <param name="picture">imge 对象</param>
   /// <param name="WaterMarkPicPath">水印图片的地址</param>
   /// <param name="_watermarkPosition">水印位置</param>
   /// <param name="_width">被加水印图片的宽</param>
   /// <param name="_height">被加水印图片的高</param>
   private void addWatermarkImage( Graphics picture,string WaterMarkPicPath,
    string _watermarkPosition,int _width,int _height)
   {
    Image watermark = new Bitmap(WaterMarkPicPath);
    this.addWatermarkImage(picture, watermark, _watermarkPosition, _width,
     _height);
   }
   #endregion
   #region 生成缩略图
   /// <summary>
   /// 保存图片
   /// </summary>
   /// <param name="image">Image 对象</param>
   /// <param name="savePath">保存路径</param>
   /// <param name="ici">指定格式的编解码参数</param>
   private void SaveImage(Image image, string savePath, ImageCodecInfo ici)
   {
    //设置 原图片 对象的 EncoderParameters 对象
    EncoderParameters parameters = new EncoderParameters(1);
    parameters.Param[0] = new EncoderParameter(
     System.Drawing.Imaging.Encoder.Quality, ((long) 90));
    image.Save(savePath, ici, parameters);
    parameters.Dispose();
   }
   /// <summary>
   /// 获取图像编码解码器的所有相关信息
   /// </summary>
   /// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
   /// <returns>返回图像编码解码器的所有相关信息</returns>
   private ImageCodecInfo GetCodecInfo(string mimeType)
   {
    ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
    foreach(ImageCodecInfo ici in CodecInfo)
    {
     if(ici.MimeType == mimeType)
      return ici;
    }
    return null;
   }
   /// <summary>
   /// 生成缩略图
   /// </summary>
   /// <param name="sourceImagePath">原图片路径(相对路径)</param>
   /// <param name="thumbnailImagePath">生成的缩略图路径,如果为空则保存为原图片路径(相对路径)</param>
   /// <param name="thumbnailImageWidth">缩略图的宽度(高度与按源图片比例自动生成)</param>
   public void ToThumbnailImages(
    string SourceImagePath,
    string ThumbnailImagePath,
    int ThumbnailImageWidth)
   {
    Hashtable htmimes = new Hashtable();
    htmimes[".jpeg"] = "image/jpeg";
    htmimes[".jpg"] = "image/jpeg";
    htmimes[".png"] = "image/png";
    htmimes[".tif"] = "image/tiff";
    htmimes[".tiff"] = "image/tiff";
    htmimes[".bmp"] = "image/bmp";
    htmimes[".gif"] = "image/gif";
    // 取得原图片的后缀
    string sExt = SourceImagePath.Substring(
     SourceImagePath.LastIndexOf(".")).ToLower();
    //从 原图片创建 Image 对象
    Image image = Image.FromFile(SourceImagePath);
    int num = ((ThumbnailImageWidth / 4) * 3);
    int width = image.Width;
    int height = image.Height;
    //计算图片的比例
    if ((((double) width) / ((double) height)) >= 1.3333333333333333f)
    {
     num = ((height * ThumbnailImageWidth) / width);
    }
    else
    {
     ThumbnailImageWidth = ((width * num) / height);
    }
    if ((ThumbnailImageWidth < 1) || (num < 1))
    {
     return;
    }
    //用指定的大小和格式初始化 Bitmap 类的新实例
    Bitmap bitmap = new Bitmap(ThumbnailImageWidth, num,
     PixelFormat.Format32bppArgb);
    //从指定的 Image 对象创建新 Graphics 对象
    Graphics graphics = Graphics.FromImage(bitmap);
    //清除整个绘图面并以透明背景色填充
    graphics.Clear(Color.Transparent);
    graphics.SmoothingMode = SmoothingMode.HighQuality;
    graphics.InterpolationMode = InterpolationMode.High;
    //在指定位置并且按指定大小绘制 原图片 对象
    graphics.DrawImage(image, new Rectangle(0, 0, ThumbnailImageWidth, num));
    image.Dispose();
    try
    {
     //将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
     SaveImage(bitmap, ThumbnailImagePath,
      GetCodecInfo((string)htmimes[sExt]));
    }
    catch(System.Exception e)
    {
     throw e;
    }
   }
   #endregion
}
}

代码实例二


代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Drawing.Imaging;
/* Author : IT
* Date: 2011-11-13 14:52:53
* Blog: www.chenpan.name
*/
namespace WaterImage
{
public partial class Form2 : Form
{
Image imgWeight;
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 从数据库中加载二进制图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string strSql = "Select Top 1 FileContent From Sys_FileSave";
Byte[] byteImage = new Byte[0];
byteImage = (Byte[])(DbHelperSQL.GetSingle(strSql));
MemoryStream stmBLOBData = new MemoryStream(byteImage);
imgWeight = Image.FromStream(stmBLOBData); pictureBox1.Image = imgWeight;
}
/// <summary>
/// 在原图片基础上加载文字水印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Graphics GImage = Graphics.FromImage(imgWeight);
addWatermarkText(GImage, "重量为60.00吨", "WM_BOTTOM_RIGHT", imgWeight.Width, imgWeight.Height);
pictureBox1.Image = imgWeight;
}
/// <summary>
/// 在原图片基础上加载图片水印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Graphics GImage = Graphics.FromImage(imgWeight);
addWatermarkImage(GImage, @"C:\Documents and Settings\Administrator\桌面\Mark.png", "WM_TOP_LEFT", imgWeight.Width, imgWeight.Height);
pictureBox1.Image = imgWeight;
}
/// <summary>
/// 生成图片缩略图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
GreateMiniImage(@"C:\Documents and Settings\Administrator\桌面\Source.jpg", @"C:\Documents and Settings\Administrator\桌面\Small.png", 100, 200);
}
/// <summary>
/// 加水印文字
/// </summary>
/// <param name="picture">imge 对象</param>
/// <param name="_watermarkText">水印文字内容</param>
/// <param name="_watermarkPosition">水印位置</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
private void addWatermarkText(Graphics picture, string _watermarkText, string _watermarkPosition, int _width, int _height)
{
int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };
Font crFont = null;
SizeF crSize = new SizeF();
for (int i = 0; i < 7; i++)
{
crFont = new Font("arial", sizes[i], FontStyle.Bold);
crSize = picture.MeasureString(_watermarkText, crFont);
if ((ushort)crSize.Width < (ushort)_width)
break;
}
float xpos = 0;
float ypos = 0;
switch (_watermarkPosition)
{
case "WM_TOP_LEFT":
xpos = ((float)_width * (float).01) + (crSize.Width / 2);
ypos = (float)_height * (float).01;
break;
case "WM_TOP_RIGHT":
xpos = ((float)_width * (float).99) - (crSize.Width / 2);
ypos = (float)_height * (float).01;
break;
case "WM_BOTTOM_RIGHT":
xpos = ((float)_width * (float).99) - (crSize.Width / 2);
ypos = ((float)_height * (float).99) - crSize.Height;
break;
case "WM_BOTTOM_LEFT":
xpos = ((float)_width * (float).01) + (crSize.Width / 2);
ypos = ((float)_height * (float).99) - crSize.Height;
break;
}
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));
picture.DrawString(_watermarkText, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
picture.DrawString(_watermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat);
semiTransBrush2.Dispose();
semiTransBrush.Dispose();
}
/// <summary>
/// 加水印图片
/// </summary>
/// <param name="picture">imge 对象</param>
/// <param name="WaterMarkPicPath">水印图片的地址</param>
/// <param name="_watermarkPosition">水印位置</param>
/// <param name="_width">被加水印图片的宽</param>
/// <param name="_height">被加水印图片的高</param>
private void addWatermarkImage(Graphics picture, string WaterMarkPicPath, string _watermarkPosition, int _width, int _height)
{
Image watermark = new Bitmap(WaterMarkPicPath);
ImageAttributes imageAttributes = new ImageAttributes();
ColorMap colorMap = new ColorMap();
colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
ColorMap[] remapTable = { colorMap };
imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
float[][] colorMatrixElements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
};
ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
int xpos = 0;
int ypos = 0;
int WatermarkWidth = 0;
int WatermarkHeight = 0;
double bl = 1d;
//计算水印图片的比率
//取背景的1/4宽度来比较
if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
{
bl = 1;
}
else if ((_width > watermark.Width * 4) && (_height < watermark.Height * 4))
{
bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
}
else
if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
{
bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
}
else
{
if ((_width * watermark.Height) > (_height * watermark.Width))
{
bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);
}
else
{
bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
}
}
WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
switch (_watermarkPosition)
{
case "WM_TOP_LEFT":
xpos = 10;
ypos = 10;
break;
case "WM_TOP_RIGHT":
xpos = _width - WatermarkWidth - 10;
ypos = 10;
break;
case "WM_BOTTOM_RIGHT":
xpos = _width - WatermarkWidth - 10;
ypos = _height - WatermarkHeight - 10;
break;
case "WM_BOTTOM_LEFT":
xpos = 10;
ypos = _height - WatermarkHeight - 10;
break;
}
picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
watermark.Dispose();
imageAttributes.Dispose();
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="oldpath">原图片地址</param>
/// <param name="newpath">新图片地址</param>
/// <param name="tWidth">缩略图的宽</param>
/// <param name="tHeight">缩略图的高</param>
private void GreateMiniImage(string oldpath, string newpath, int tWidth, int tHeight)
{
try
{
System.Drawing.Image image = System.Drawing.Image.FromFile(oldpath);
double bl = 1d;
if ((image.Width <= image.Height) && (tWidth >= tHeight))
{
bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight);
}
else if ((image.Width > image.Height) && (tWidth < tHeight))
{
bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth);
}
else
if ((image.Width <= image.Height) && (tWidth <= tHeight))
{
if (image.Height / tHeight >= image.Width / tWidth)
{
bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth);
}
else
{
bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight);
}
}
else
{
if (image.Height / tHeight >= image.Width / tWidth)
{
bl = Convert.ToDouble(image.Height) / Convert.ToDouble(tHeight);
}
else
{
bl = Convert.ToDouble(image.Width) / Convert.ToDouble(tWidth);
}
}
Bitmap b = new Bitmap(image, Convert.ToInt32(image.Width / bl), Convert.ToInt32(image.Height / bl));
b.Save(newpath);
b.Dispose();
image.Dispose();
}
catch
{
}
}
}
}

(0)

相关推荐

  • C#在图片增加文字的实现代码

    业务需要动态给图片增加文字(书本的封面图片),修改字体大小.字体.颜色.控制位置 测试代码: string path = @"E:\cover.png"; Bitmap bmp = new Bitmap(path); Graphics g = Graphics.FromImage(bmp); String str = "贤愚经"; Font font = new Font("仿宋_GB2312", 14, FontStyle.Bold);//设置

  • c#给图片添加文字的代码小结

    代码实例一 复制代码 代码如下: using System; using System.IO; using System.Collections; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace Imag_writer { /// <summary> /// 水印的类型 /// </summary> public enum WaterMarkT

  • php面向对象与面向过程两种方法给图片添加文字水印

    目前绝大多数PHP程序员使用面向过程的方式,因为解析WEB页面本身就非常"过程化"(从一个标签到另一个标签).在HTML中嵌入过程处理代码是很直接自然的作法,所以PHP程序员通常使用这种方式. 如果你是刚接触PHP,用面向过程的风格来书写代码很可能是你唯一的选择.但是如果你经常上PHP论坛和新闻组的话,你应该会看到有关"对象"的文章.你也可能看到过如何书写面向对象的PHP代码的教程.或者你也可能下载过一些现成的类库,并尝试着去实例化其中的对象和使用类方法--尽管你可

  • Python实现图片添加文字

    在工作中有时候会给图上添加文字,常用的是PS工具,不过我想通过代码的方式来给图片添加文字. 需要使用的Python的图像库:PIL.更加详细的知识点如下: Imaga模块:用来创建,打开,保存图片文件 new(path):用来创建一个新的图片文件.该文件位于path所在的路径中..打开后返回Image类型的图片. open(path):用来打开已经存在的图片文件.该文件位于path所在的路径中.打开后返回Image类型的图片. save(path):用来把创建或者打开的图片保到path所在的路径

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

    1: 面向过程的编写方法 //指定图片路径 $src = '001.png'; //获取图片信息 $info = getimagesize($src); //获取图片扩展名 $type = image_type_to_extension($info[2],false); //动态的把图片导入内存中 $fun = "imagecreatefrom{$type}"; $image = $fun('001.png'); //指定字体颜色 $col = imagecolorallocateal

  • Python 使用 Pillow 模块给图片添加文字水印的方法

    像微博一类的平台上传图片时,平台都会添加一个水印,宣誓着对图片的所有权,我们自己的博客平台也可以给自己的图片添加上水印. 还是用 Pillow 模块来实现 先来看一个简单的例子 >>> from PIL import Image >>> from PIL import ImageDraw >>> >>> image = Image.open('/Users/wxnacy/Downloads/vm-error1.png') >&g

  • C#利用GDI+给图片添加文字(文字自适应矩形区域)

    前言 这篇文章是 GDI+ 总结系列的第二篇,如果对 GDI+ 的基础使用不熟悉的朋友可以先看第一篇文章<C# 使用 GDI+ 画图>. 需求 需求是要做一个编辑文字的页面.用户在网页端写文字,文字区域是个矩形框,用户可以通过下方的拖动条调节文字大小. 如下图: 提交数据的时候前端传文字区域的左上角和右下角定位给后台.因为前端的字体大小单位与后端没什么关系,所以不能直接传字体大小,也就是后端要根据矩形区域以及文字内容来自己推算用什么样的字体大小合适. 简单说就是知道文字的矩形区域,以及文字内容

  • php图片添加文字水印实现代码

    php类库给现有的图片加文字水印,代码不是很完善,欢迎大家多多指教!代码如下: <?php /*PHP图片加文字水印类库 QQ:3697578482 伤心的歌 该类库暂时只支持文字水印,位置为右下角,颜色随机 调用方法: 1.在需要加水印的文件顶部引入类库: include_once 'imageClass.php'; 2.声明新类: $tpl=new image_fu; 3.给图片水印提供参数: $tpl->img(图片路径,水印文字,字体路径,字体大小,字体角度); 比如:$tpl->

  • python使用PIL给图片添加文字生成海报示例

    前言 曾经,我也算半个所谓的文学青年.大学前两年大部分时间泡在图书馆看各种文学类的书. 那时的我,对于未来有很多遐想:写小说.写时评.写诗歌... 总而言之,就是成为一个文字工作者 现在我确实成为了一个文字工作者,只不过写的是代码... 在某个月黑风高的晚上,看着满屏花花绿绿的代码,揉着酸涩的眼睛,打了一个长长的哈欠.突然进入了禅定时刻: "还记得年少时的梦吗?" 我又开始想写作了,一个写了几年代码的老男人,在被生活粗暴地摁在地上摩擦几回后,突然触发了内心的柔软,想写些东西. 要写些什

  • golang中实现给gif、png、jpeg图片添加文字水印

    添加水印示例 添加main文件:"watermark/main.go" package main import ( "fmt" "watermark/textwatermark" ) func main() { SavePath := "./kaf" str := textwatermark.FontInfo{18, "努力向上", textwatermark.TopLeft, 20, 20, 255, 2

  • 随鼠标移动的图片或文字特效代码

    跟随鼠标的图片特效 document.onmousemove = function () { var x = window.event.clientX; var y = window.event.clientY; var divId = document.getElementById("divId"); if (!divId) { return; } divId.style.left = x; divId.style.top = y; } 这里放的是图片 [Ctrl+A 全选 注:如需

随机推荐