通过.NET程序给图片添加文字的两种方式~
1、通过.net自带类库实现,代码如下:
2、通过Office 2003自带的Microsoft.Office.Document.Imaging 11.0 Type Library来进行操作,文件位置:c:\program files\common files\microsoft shared\modi\mdivwctl.dll
代码如下:
PS : 这两种方式都要求User必须对该图片要有操作权限,否则会报Access denied.
Image Img = Image.FromFile(@"E:\AAA.bmp");
System.Drawing.Imaging.PixelFormat _PF = Img.PixelFormat;
/* 备注:如果Img图像是索引像素格式,下面的方法将引发异常
* 如:
* PixelFormat.Format1bppIndexed
* PixelFormat.Format4bppIndexed
* PixelFormat.Format8bppIndexed
* 另外,下列格式也会引发异常
* PixelFormat.Undefined
* PixelFormat.DontCare
* PixelFormat.Format16bppArgb1555
* PixelFormat.Format16bppGrayScale
*/
Graphics Gra = System.Drawing.Graphics.FromImage(Img);
Font _F = new Font("宋体",24);
SolidBrush _B = new SolidBrush(Color.Black);
Pen _P = new Pen(_B,10);
Gra.DrawString("博客园",_F,_B,10,10);
Gra.DrawLine(_P,1,1,100,100);
Gra.Flush();
Gra.Dispose();
Img.Save(@"E:\AAA2.bmp");
System.Drawing.Imaging.PixelFormat _PF = Img.PixelFormat;
/* 备注:如果Img图像是索引像素格式,下面的方法将引发异常
* 如:
* PixelFormat.Format1bppIndexed
* PixelFormat.Format4bppIndexed
* PixelFormat.Format8bppIndexed
* 另外,下列格式也会引发异常
* PixelFormat.Undefined
* PixelFormat.DontCare
* PixelFormat.Format16bppArgb1555
* PixelFormat.Format16bppGrayScale
*/
Graphics Gra = System.Drawing.Graphics.FromImage(Img);
Font _F = new Font("宋体",24);
SolidBrush _B = new SolidBrush(Color.Black);
Pen _P = new Pen(_B,10);
Gra.DrawString("博客园",_F,_B,10,10);
Gra.DrawLine(_P,1,1,100,100);
Gra.Flush();
Gra.Dispose();
Img.Save(@"E:\AAA2.bmp");
2、通过Office 2003自带的Microsoft.Office.Document.Imaging 11.0 Type Library来进行操作,文件位置:c:\program files\common files\microsoft shared\modi\mdivwctl.dll
代码如下:
using System;
using MODI;
namespace AddImg
{
class TestAddImg
{
[STAThread]
static void Main(string[] args)
{
MODI.Document Doc = new Document();
Doc.Create(@"D:\myTiff.TIF");
Doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,false,false);
Doc.Save();
MODI.Image img = (MODI.Image)Doc.Images[0];
Console.WriteLine(img.Layout.Text);
Console.ReadLine();
}
}
}
using MODI;
namespace AddImg
{
class TestAddImg
{
[STAThread]
static void Main(string[] args)
{
MODI.Document Doc = new Document();
Doc.Create(@"D:\myTiff.TIF");
Doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,false,false);
Doc.Save();
MODI.Image img = (MODI.Image)Doc.Images[0];
Console.WriteLine(img.Layout.Text);
Console.ReadLine();
}
}
}
PS : 这两种方式都要求User必须对该图片要有操作权限,否则会报Access denied.