老早以前写的,害怕以后忘了或找不到了,发到这里以备忘。

using
System;

using
System.IO;

using
System.Drawing;

using
System.Drawing.Imaging;


///

<summary>


///
PictureMarker 的摘要说明。

///

</summary>


public

class
PictureMarker
{

///

<summary>


///
PictureMarker的构造函数

///

</summary>



public
PictureMarker()
{
}


///

<summary>


///
PictureMarker的构造函数

///

</summary>


///

<param name="width">
缩略图的宽度
</param>


///

<param name="height">
缩略图的高度
</param>


///

<param name="ismust">
是否一定要生成缩略图,推荐为false
</param>



public
PictureMarker(
int
width,
int
height,
bool
ismust)
{

this
.width
=
width;

this
.height
=
height;

this
.ismust
=
ismust;
}


///

<summary>


///
PictureMarker的构造函数

///

</summary>


///

<param name="width">
缩略图的宽度
</param>


///

<param name="height">
缩略图的高度
</param>


///

<param name="ismust">
是否一定要生成缩略图,推荐为false
</param>


///

<param name="isSavePrimaryPic">
是否要保存源图片
</param>


///

<param name="primaryPicPath">
源图片的保存目录
</param>



public
PictureMarker(
int
width,
int
height,
bool
ismust,
bool
isSavePrimaryPic,
string
primaryPicPath)
:

this
(width, height, ismust)
{

this
.isSavePrimaryPic
=
isSavePrimaryPic;

this
.primaryPicPath
=
primaryPicPath;
}


private

int
width;

private

int
height;

private

bool
ismust
=

false
;

private

bool
isSavePrimaryPic
=

false
;

private

string
primaryPicPath;


///

<summary>


///
缩略图的宽度

///

</summary>



public

int
Width
{

get
{
return
width; }

set
{ width
=
value; }
}


///

<summary>


///
缩略图的高度

///

</summary>



public

int
Height
{

get
{
return
height; }

set
{ height
=
value; }
}


///

<summary>


///
是否一定要生成缩略图,推荐为false

///

</summary>



public

bool
IsMust
{

get
{
return
ismust; }

set
{ ismust
=
value; }
}


///

<summary>


///
是否要保存源图片

///

</summary>



public

bool
IsSavePrimaryPic
{

get
{
return
isSavePrimaryPic; }

set
{ isSavePrimaryPic
=
value; }
}


///

<summary>


///
源图片的保存目录

///

</summary>



public

string
PrimaryPicPath
{

get
{
return
primaryPicPath; }

set
{ primaryPicPath
=
value; }
}


///

<summary>


///
在图片上添加版权信息水印

///

</summary>


///

<param name="input">
目标图片
</param>


///

<param name="copyright">
文字版权信息
</param>


///

<param name="savepath">
保存路径
</param>


///

<param name="position">
水印显示位置
</param>



public

void
CreateMark(Stream input,
string
copyright,
string
savepath, WaterPosition position)
{

int
xpoint, ypoint;
System.Drawing.Image photo

=
System.Drawing.Image.FromStream(input);

int
pwidth
=
photo.Width;

int
pheight
=
photo.Height;
Bitmap markpic

=

new
Bitmap(photo);
markpic.SetResolution(photo.HorizontalResolution, photo.VerticalResolution);

Graphics graphics

=
Graphics.FromImage(markpic);

xpoint

=
(pwidth
-

100
)
/

2
;
ypoint

=
pheight
-

40
;
graphics.DrawString(copyright,

new
Font(
"
楷体
"
,
15
),
new
System.Drawing.SolidBrush(Color.FromArgb(
255
,
0
,
255
,
255
)),

new
RectangleF(xpoint, ypoint,
400
,
30
));



try

{
markpic.Save(savepath);
}

finally

{
input.Close();
photo.Dispose();
markpic.Dispose();
graphics.Dispose();
}
}


///

<summary>


///
在图片上添加版权信息水印

///

</summary>


///

<param name="input">
目标图片
</param>


///

<param name="copyright">
图片版权信息
</param>


///

<param name="savepath">
保存路径
</param>


///

<param name="position">
水印显示位置
</param>



public

void
CreateMark(Stream input, Stream copyright,
string
savepath, WaterPosition position)
{

int
xpoint, ypoint;
System.Drawing.Image photo

=
System.Drawing.Image.FromStream(input);
System.Drawing.Image copy

=
System.Drawing.Image.FromStream(copyright);

int
pwidth
=
photo.Width;

int
pheight
=
photo.Height;

int
cwidth
=
copy.Width;

int
cheight
=
copy.Height;

if
(pwidth
-
cwidth
<

20

||
pheight
-
cheight
<

20
)
{
input.Close();
copyright.Close();
photo.Dispose();
copy.Dispose();

throw

new
ArgumentException(
"
不合适的图片尺寸.目标图片比版权图片至少宽20像素,高20像素.
"
);
}


if
(isSavePrimaryPic)
{

if
(primaryPicPath
==

null

||
primaryPicPath
==

""
)
{
input.Close();
copyright.Close();
photo.Dispose();
copy.Dispose();

throw

new
ArgumentException(
"
当要求保存源图片时,源图片的保存路径不能为空
"
);
}


try

{
photo.Save(primaryPicPath

+

"
\\
"

+
Path.GetFileName(savepath)
+

"
.jpg
"
, ImageFormat.Jpeg);
}

catch
(Exception ex)
{
input.Close();
copyright.Close();
photo.Dispose();
copy.Dispose();


throw
ex;
}
}

Bitmap markpic

=

new
Bitmap(photo);
markpic.SetResolution(photo.HorizontalResolution, photo.VerticalResolution);

Graphics graphics

=
Graphics.FromImage(markpic);
System.Drawing.Imaging.ImageAttributes att

=

new
System.Drawing.Imaging.ImageAttributes();


float
[][] matrixelements
=
{


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.5f
,
0.0f
},


new

float
[] {
0.0f
,
0.0f
,
0.0f
,
0.0f
,
1.0f
}};

System.Drawing.Imaging.ColorMatrix matrix

=

new
System.Drawing.Imaging.ColorMatrix(matrixelements);

att.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);


switch
(position)
{

case
WaterPosition.Left:
xpoint

=

10
;
ypoint

=
(pheight
-
cheight)
/

2
;

break
;

case
WaterPosition.LeftUp:
xpoint

=

10
;
ypoint

=

10
;

break
;

case
WaterPosition.MiddleUp:
xpoint

=
(pwidth
-
cwidth)
/

2
;
ypoint

=

10
;

break
;

case
WaterPosition.RightUp:
xpoint

=
pwidth
-
cwidth
-

10
;
ypoint

=

10
;

break
;

case
WaterPosition.Right:
xpoint

=
pwidth
-
cwidth
-

10
;
ypoint

=
(pheight
-
cheight)
/

2
;

break
;

case
WaterPosition.RightDown:
xpoint

=
pwidth
-
cwidth
-

10
;
ypoint

=
pheight
-
cheight
-

10
;

break
;

case
WaterPosition.MiddleDown:
xpoint

=
(pwidth
-
cwidth)
/

2
;
ypoint

=
pheight
-
cheight
-

10
;

break
;

case
WaterPosition.LeftDown:
xpoint

=

10
;
ypoint

=
pheight
-
cheight
-

10
;

break
;

default
:
xpoint

=
(pwidth
-
cwidth)
/

2
;
ypoint

=
(pheight
-
cheight)
/

2
;

break
;
};

graphics.DrawImage(copy,

new
Rectangle(xpoint, ypoint, cwidth, cheight),
0
,
0
, cwidth, cheight, GraphicsUnit.Pixel, att);


if
(width
!=

0

&&
height
!=

0
)
{

if
(ismust)
{

int
twidth, theight;

if
(markpic.Width
*
height
>
markpic.Height
*
width)
{
twidth

=
width;
theight

=
markpic.Height
*
width
/
markpic.Width;
}

else

{
theight

=
height;
twidth

=
markpic.Width
*
height
/
markpic.Height;
}

markpic.SetResolution(twidth, theight);
markpic

=
(Bitmap)markpic.GetThumbnailImage(twidth, theight,
null
,
new
IntPtr());
}

else

{

if
(width
<
markpic.Width
||
height
<
markpic.Height)
{

if
(width
<
markpic.Width
||
height
<
markpic.Height)
{

int
twidth, theight;

if
(markpic.Width
*
height
>
markpic.Height
*
width)
{
twidth

=
width;
theight

=
markpic.Height
*
width
/
markpic.Width;
}

else

{
theight

=
height;
twidth

=
markpic.Width
*
height
/
markpic.Height;
}

markpic.SetResolution(twidth, theight);
markpic

=
(Bitmap)markpic.GetThumbnailImage(twidth, theight,
null
,
new
IntPtr());
}
}
}
}


try

{
markpic.Save(savepath, ImageFormat.Jpeg);
}

finally

{
graphics.Dispose();
input.Close();
copyright.Close();
photo.Dispose();
copy.Dispose();
markpic.Dispose();
}
}



///

<summary>


///
在图片上添加版权信息水印

///

</summary>


///

<param name="input">
目标图片
</param>


///

<param name="copyright">
版权信息,可以为图片路径或文字版权信息
</param>


///

<param name="directory">
保存目录
</param>


///

<param name="filename">
保存文件名
</param>


///

<param name="isDrectory">
copyright是否为文件路径
</param>


///

<param name="position">
水印显示位置
</param>



public

void
CreateMark(Stream input,
string
copyright,
string
directory,
string
filename,
bool
isDrectory, WaterPosition position)
{

if
(isDrectory)
{
FileStream _copyright

=

new
FileStream(copyright, FileMode.Open, FileAccess.Read, FileShare.Read);
CreateMark(input, _copyright, directory, filename, position);
}

else

{
CreateMark(input, copyright, directory

+

"
\\
"

+
filename, position);
}
}


///

<summary>


///
在图片上添加版权信息水印

///

</summary>


///

<param name="input">
目标图片
</param>


///

<param name="copyright">
图片版权信息
</param>


///

<param name="directory">
保存目录
</param>


///

<param name="filename">
保存文件名
</param>


///

<param name="position">
水印显示位置
</param>



public

void
CreateMark(Stream input, Stream copyright,
string
directory,
string
filename, WaterPosition position)
{
CreateMark(input, copyright, directory

+

"
\\
"

+
filename, position);
}


///

<summary>


///
在图片上添加版权信息水印

///

</summary>


///

<param name="filepath">
目标图片路径
</param>


///

<param name="copyright">
版权信息,可以为图片路径或文字版权信息
</param>


///

<param name="directory">
保存目录
</param>


///

<param name="filename">
保存文件名
</param>


///

<param name="isDrectory">
copyright是否为文件路径
</param>


///

<param name="position">
水印显示位置
</param>



public

void
CreateMark(
string
filepath,
string
copyright,
string
directory,
string
filename,
bool
isDrectory, WaterPosition position)
{
FileStream input

=

new
FileStream(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
CreateMark(input, copyright, directory, filename, isDrectory, position);
}


///

<summary>


///
在图片上添加版权信息水印

///

</summary>


///

<param name="filepath">
目标图片路径
</param>


///

<param name="copyright">
版权信息,可以为图片路径或文字版权信息
</param>


///

<param name="savepath">
保存路径
</param>


///

<param name="isDrectory">
copyright是否为文件路径
</param>


///

<param name="position">
水印显示位置
</param>



public

void
CreateMark(
string
filepath,
string
copyright,
string
savepath,
bool
isDrectory, WaterPosition position)
{

if
(isDrectory)
{
FileStream input

=

new
FileStream(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
FileStream _copyright

=

new
FileStream(copyright, FileMode.Open, FileAccess.Read, FileShare.Read);
CreateMark(input, _copyright, savepath, position);
}

else

{
FileStream input

=

new
FileStream(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
CreateMark(input, copyright, savepath, position);
}
}


public

void
CreateThumbnailImage(Stream input,
string
savepath)
{

if
(width
==

0

||
height
==

0
)
{
input.Close();

throw

new
ArgumentException(
"
缩略图的宽度和高度不能为0.
"
);
}
Image photo

=
Image.FromStream(input);


if
(isSavePrimaryPic)
{

if
(primaryPicPath
==

null

||
primaryPicPath
==

""
)
{
input.Close();
photo.Dispose();

throw

new
ArgumentException(
"
当要求保存源图片时,源图片的保存路径不能为空
"
);
}


try

{
photo.Save(Path.Combine(primaryPicPath, Path.GetFileNameWithoutExtension(savepath)

+

"
.jpg
"
), ImageFormat.Jpeg);
}

catch
(Exception ex)
{
input.Close();
photo.Dispose();


throw
ex;
}
}

Bitmap markpic

=

new
Bitmap(photo);


if
(ismust)
{

int
twidth, theight;

if
(markpic.Width
*
height
>
markpic.Height
*
width)
{
twidth

=
width;
theight

=
markpic.Height
*
width
/
markpic.Width;
}

else

{
theight

=
height;
twidth

=
markpic.Width
*
height
/
markpic.Height;
}

markpic.SetResolution(twidth, theight);
markpic

=
(Bitmap)markpic.GetThumbnailImage(twidth, theight,
null
,
new
IntPtr());
}

else

{

if
(width
<
markpic.Width
||
height
<
markpic.Height)
{

if
(width
<
markpic.Width
||
height
<
markpic.Height)
{

int
twidth, theight;

if
(markpic.Width
*
height
>
markpic.Height
*
width)
{
twidth

=
width;
theight

=
markpic.Height
*
width
/
markpic.Width;
}

else

{
theight

=
height;
twidth

=
markpic.Width
*
height
/
markpic.Height;
}

markpic.SetResolution(twidth, theight);
markpic

=
(Bitmap)markpic.GetThumbnailImage(twidth, theight,
null
,
new
IntPtr());
}
}
}


try

{
markpic.Save(savepath, ImageFormat.Jpeg);
}

finally

{
input.Close();
photo.Dispose();
markpic.Dispose();
}
}


public

void
CreateThumbnailImage(
string
source,
string
savepath)
{
FileStream input

=

new
FileStream(source, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
CreateThumbnailImage(input, savepath);
}


///

<summary>


///
水印显示的位置

///

</summary>



public

enum
WaterPosition
{

///

<summary>


///
左上

///

</summary>


LeftUp,

///

<summary>


///
中上

///

</summary>


MiddleUp,

///

<summary>


///
右上

///

</summary>


RightUp,

///

<summary>


///


///

</summary>


Right,

///

<summary>


///
右下

///

</summary>


RightDown,

///

<summary>


///
中下

///

</summary>


MiddleDown,

///

<summary>


///
左下

///

</summary>


LeftDown,

///

<summary>


///


///

</summary>


Left,

///

<summary>


///
正中间

///

</summary>


Middle
}
}


源代码

标签: none

添加新评论