图片加文字水印,无法控制文字大小

moodboy1982 2009-01-05 03:01:00
先看代码:

/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="FileStream">上传的文件流</param>
/// <param name="FilePath">图片要存储的物理路径</param>
/// <param name="Text">文字内容</param>
/// <param name="FontFamily">字体</param>
/// <param name="FontTran">字体透明度</param>
/// <param name="FontSize">字体大小</param>
/// <param name="FontColor">字体颜色</param>
/// <param name="FontTop">字体上部位置(小数形式百分比)</param>
/// <param name="FontLeft">字体左部位置(小数形式百分比)</param>
private void AddFontWaterMark(Stream FileStream, string FilePath, string Text, string Font_Family, int FontTran, float FontSize, string FontColor, float FontTop, float FontLeft)
{
//加文字水印
using (System.Drawing.Image image = System.Drawing.Image.FromStream(FileStream))
{
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font(Font_Family, FontSize);
int NewFontTran = Convert.ToInt32(FontTran * 25.5);
Brush b = new SolidBrush(Color.FromArgb(NewFontTran, System.Drawing.ColorTranslator.FromHtml(FontColor)));
int TextTop = (int)((image.Height) * FontTop) / 100;
int TextLeft = (int)((image.Width) * FontLeft) / 100;
g.DrawString(Text, f, b, TextLeft, TextTop);
}
image.Save(FilePath);
}
}



现在有一个问题:我FontSize设为12,可有的图片上的水印字很小,有的图片上的水印字动很大???当然我设为30也一样,有的水印字体很大,有的水印字体动更大!!!
附加一个小问题,好像不支持GIF图片加水印.

...全文
2685 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
d1f2h3 2011-06-08
  • 打赏
  • 举报
回复
有没有解决啊
xrongzhen 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用楼主 moodboy1982 的回复:]
先看代码:

C# code

/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="FileStream">上传的文件流</param>
/// <param name="FilePath">图片要存储的物理路径</param>
……
[/Quote]

那你可以根据图片的大小 选择水印字体的大小 动态改变字体大小
wangzhikang888 2010-06-12
  • 打赏
  • 举报
回复
把图片重新画一次就好了
string FilePath="图片路径";
System.Drawing.Image image = InitImage(FilePath);
System.Drawing.Graphics g = Graphics.FromImage(image);

public static Image InitImage(string imageFilePath)
{
int width = 0;
int height = 0;
using (Image originalImage = Image.FromFile(imageFilePath))
{
width = originalImage.Width;
height = originalImage.Height;
int towidth = width;
int toheight = height;

int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;


if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}

//新建一个bmp图片
using (Image bitmap = new System.Drawing.Bitmap(towidth, toheight))
{

//新建一个画板
using (Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{

//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//清空画布并以透明背景色填充
g.Clear(Color.Transparent);

//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
//以jpg格式保存缩略图
Image result = (Image)bitmap.Clone();
return result;
}
}
}
qqwzw 2009-08-17
  • 打赏
  • 举报
回复
我也遇到同样的问题,加水印图片之后又的水印图片很大,有的很小,楼主有解决吗?发一份给我。
396022332@qq.com
jiang_chao 2009-04-23
  • 打赏
  • 举报
回复
楼主解决了发我一份哦,我也遇到这个问题?
moodboy1982 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 maple2008 的回复:]
问一句,你查看图片的时候,是不是按图片的100%大小查看的?
[/Quote]

[Quote=引用 29 楼 wangping_li 的回复:]
有的上面很大,有的上面很小?
你拿两幅同样大小的图试试看
会不会是图片尺寸引起的?
[/Quote]

同样大小(大小,格式,像素等都相同)的图片,只要我用PHOTOSHOP截图另存为再加水印就不同了。
qq196260188 2009-01-11
  • 打赏
  • 举报
回复
学习了,,,
wangping_li 2009-01-11
  • 打赏
  • 举报
回复
有的上面很大,有的上面很小?
你拿两幅同样大小的图试试看
会不会是图片尺寸引起的?
moodboy1982 2009-01-10
  • 打赏
  • 举报
回复
我希望能有哪位可以告诉我,同样的代码,为什么在有的图片上加的水印字就大点,在有的图片上加的水印字就小点?能帮我解释这个,不给代码也行!
moodboy1982 2009-01-10
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 kongwei521 的回复:]
C# code///<summary>///添加文字水印///</summary>///<param name="FileStream">上传的文件流</param>///<param name="FilePath">图片要存储的物理路径</param>///<param name="Text">文字内容</param>///<param name="FontFamily">字体</param>///<param name="FontTran">字体透明度</param>///<param name="FontSize">字体大小</param>///<param name="FontColor">字体颜色</param>///<param name="FontTop">字体上部位置(小数…
[/Quote]

好像有点不对头哟,你似乎还有几段语句没有复制过来吧。
原文应该是:

public string UploadImage(System.Web.HttpPostedFile postFile)
{
try
{
if(postFile.ContentLength==0)
{
throw(new Exception("NotExistedPostFile"));
}
else
if(postFile.ContentLength>Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["MaxImageSize"]))
{
throw(new Exception("TooLargePostFile"));
}
String strExt=System.IO.Path.GetExtension(postFile.FileName);
if(System.Configuration.ConfigurationSettings.AppSettings["ImageExtension"].IndexOf(";"+strExt+";")>-1)
{
throw(new Exception("ErrorImageExtension"));
}

String strFileName=@"/uploads/images/"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".jpg";
String strAbsoluteFileName=System.Web.HttpContext.Current.Server.MapPath(strFileName);
String Copyright=System.Configuration.ConfigurationSettings.AppSettings["CopyRight"];

System.Drawing.Image imgPhoto=System.Drawing.Image.FromStream(postFile.InputStream,true);
//取高和宽
int phWidth = imgPhoto.Width;
int phHeight =imgPhoto.Height;
//建新图,指定格式为每像素 24 位;红色、绿色和蓝色分量各使用 8 位。
Bitmap bmPhoto = new Bitmap(phWidth, phHeight,PixelFormat.Format24bppRgb);
//设置分辨率
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
//准备Graphics
Graphics grPhoto = Graphics.FromImage(bmPhoto);
try
{
//指定消除锯齿的呈现。
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//拷贝原图到做图区
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
//用指定Sizes绘制图片时,测量用指定的字串宽度
//取可能的最大宽度
int[] sizes = new int[]{64,48,32,16,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("Verdana", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont);
if((ushort)crSize.Width < (ushort)phWidth)
break;
}
//指定做图点
int yPixlesFromBottom = (int)(phHeight *.05);
float yPosFromBottom = ((phHeight -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (phWidth/2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//绘制copyright
SolidBrush semiTransBrush2 =
new SolidBrush(Color.FromArgb(100, 0, 0,0));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg+1,yPosFromBottom+1),
StrFormat);
SolidBrush semiTransBrush = new SolidBrush(
Color.FromArgb(100, 255, 255, 255));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg,yPosFromBottom),
StrFormat);
bmPhoto.Save(strAbsoluteFileName,ImageFormat.Jpeg);
}
finally
{
grPhoto.Dispose();
bmPhoto.Dispose();
imgPhoto.Dispose();
}
return strFileName;
}
catch(Exception excep)
{
throw(excep);
}
}

mengxj85 2009-01-10
  • 打赏
  • 举报
回复
学习
iloveaspnet2008 2009-01-10
  • 打赏
  • 举报
回复
回帖是一种美德!传说每天回帖即可获得 10 分可用分!
niitnanfeng 2009-01-10
  • 打赏
  • 举报
回复
顶下。完了LZ总结下。找出最棒的。
蝶恋花雨 2009-01-10
  • 打赏
  • 举报
回复


/// <summary>
/// 添加文字水印
/// </summary>
/// <param name="FileStream">上传的文件流</param>
/// <param name="FilePath">图片要存储的物理路径</param>
/// <param name="Text">文字内容</param>
/// <param name="FontFamily">字体</param>
/// <param name="FontTran">字体透明度</param>
/// <param name="FontSize">字体大小</param>
/// <param name="FontColor">字体颜色</param>
/// <param name="FontTop">字体上部位置(小数形式百分比)</param>
/// <param name="FontLeft">字体左部位置(小数形式百分比)</param>
private void AddFontWaterMark(Stream FileStream, string FilePath, string Text, string Font_Family, int FontTran, float FontSize, string FontColor, float FontTop, float FontLeft)
{
//加文字水印
using (System.Drawing.Image image = System.Drawing.Image.FromStream(FileStream))
{
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = null;
SizeF crSize = new SizeF();
//循环测试数组中所定义的字体大小是否适合版权信息,如果合适就使用此种字体大小
for (int i = 0; i < 5; i++)
{
//设置字体类型,可以单独提出,作为参数
f = new Font(FontFamily, FontSize, FontStyle.Bold);
//测量此种字体大小
crSize = grPhoto.MeasureString(Text, f);

if ((ushort)crSize.Width < (ushort)phWidth)
break;
}

int NewFontTran = Convert.ToInt32(FontTran * 25.5);
Brush b = new SolidBrush(Color.FromArgb(NewFontTran, System.Drawing.ColorTranslator.FromHtml(FontColor)));
int TextTop = (int)((image.Height) * FontTop) / 100;
int TextLeft = (int)((image.Width) * FontLeft) / 100;
g.DrawString(Text, f, b, TextLeft, TextTop);
}
image.Save(FilePath);
}
}

注意FontSize传递过来的大小例如13-14-15
moodboy1982 2009-01-10
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 chagel 的回复:]
字体大小不一致有点奇怪,是不是跟你选的字体或者机器环境有关系呢?尽量用unicode的字体

GIF也是可以加水印的,你的代码中直接在image上画图,gif是不能操作的,改一下这样:
using (System.Drawing.Image image = System.Drawing.Image.FromStream(FileStream))
{
Bitmap bitmap = image as Bitmap;
Bitmap tmp = new Bitmap(bitmap.Width, bitmap.Height);
using (Graphics g = Graphics.FromImage(tmp))
{
g.DrawI…
[/Quote]

感谢!受益非浅!
不过字体与机器无关,与图片有关。
zpcoder 2009-01-10
  • 打赏
  • 举报
回复

楼上的个不错
Chris_thanks 2009-01-10
  • 打赏
  • 举报
回复

/// <summary>
/// 在图片上添加水印文字
/// </summary>
/// <param name="sourcePicture">源图片文件</param>
/// <param name="waterWords">需要添加到图片上的文字</param>
/// <param name="alpha">透明度</param>
/// <param name="position">位置</param>
/// <param name="PicturePath">文件路径</param>
/// <returns></returns>
public string DrawWords ( string sourcePicture,
string waterWords,
double alpha,
ImagePosition position,
string PicturePath ) {
//
// 判断参数是否有效
//
if ( sourcePicture == string.Empty || waterWords == string.Empty || alpha == 0.0 || PicturePath == string.Empty ) {
return sourcePicture;
}

//
// 源图片全路径
//
string sourcePictureName = PicturePath + sourcePicture;
string fileExtension = System.IO.Path.GetExtension ( sourcePictureName ).ToLower ();

//
// 判断文件是否存在,以及文件名是否正确
//
if ( System.IO.File.Exists ( sourcePictureName ) == false || (
fileExtension != ".gif" &&
fileExtension != ".jpg" &&
fileExtension != ".png" ) ) {
return sourcePicture;
}

//
// 目标图片名称及全路径
//
string targetImage = sourcePictureName.Replace ( sourcePicture, "" ) + "arx0757_"
+sourcePicture.Replace(System.IO.Path.GetExtension(sourcePictureName),"")+".jpg";

//创建一个图片对象用来装载要被添加水印的图片
Image imgPhoto = Image.FromFile ( sourcePictureName );

//获取图片的宽和高
int phWidth = imgPhoto.Width;
int phHeight = imgPhoto.Height;

//
//建立一个bitmap,和我们需要加水印的图片一样大小
Bitmap bmPhoto = new Bitmap ( phWidth, phHeight, PixelFormat.Format24bppRgb );

//SetResolution:设置此 Bitmap 的分辨率
//这里直接将我们需要添加水印的图片的分辨率赋给了bitmap
bmPhoto.SetResolution ( imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution );

//Graphics:封装一个 GDI+ 绘图图面。
Graphics grPhoto = Graphics.FromImage ( bmPhoto );

//设置图形的品质
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

//将我们要添加水印的图片按照原始大小描绘(复制)到图形中
grPhoto.DrawImage (
imgPhoto, // 要添加水印的图片
new Rectangle ( 0, 0, phWidth, phHeight ), // 根据要添加的水印图片的宽和高
0, // X方向从0点开始描绘
0, // Y方向
phWidth, // X方向描绘长度
phHeight, // Y方向描绘长度
GraphicsUnit.Pixel ); // 描绘的单位,这里用的是像素

//根据图片的大小我们来确定添加上去的文字的大小
//在这里我们定义一个数组来确定
int [] sizes = new int [] { 16, 14, 12, 10, 8, 6, 4 };

//字体
Font crFont = null;
//矩形的宽度和高度,SizeF有三个属性,分别为Height高,width宽,IsEmpty是否为空
SizeF crSize = new SizeF ();

//利用一个循环语句来选择我们要添加文字的型号
//直到它的长度比图片的宽度小
for ( int i = 0; i < 7; i++ ) {
crFont = new Font ( "arial", sizes [i], FontStyle.Bold );

//测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
crSize = grPhoto.MeasureString ( waterWords, crFont );

// ushort 关键字表示一种整数数据类型
if ( ( ushort ) crSize.Width < ( ushort ) phWidth )
break;
}

//截边5%的距离,定义文字显示(由于不同的图片显示的高和宽不同,所以按百分比截取)
int yPixlesFromBottom = ( int ) ( phHeight * .05 );

//定义在图片上文字的位置
float wmHeight = crSize.Height;
float wmWidth = crSize.Width;

float xPosOfWm;
float yPosOfWm;

switch ( position ) {
case ImagePosition.BottomMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.Center:
xPosOfWm = phWidth / 2;
yPosOfWm = phHeight / 2;
break;
case ImagePosition.LeftBottom:
xPosOfWm = wmWidth;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.LeftTop:
xPosOfWm = wmWidth / 2;
yPosOfWm = wmHeight / 2;
break;
case ImagePosition.RightTop:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = wmHeight;
break;
case ImagePosition.RigthBottom:
xPosOfWm = phWidth - wmWidth - 10;
yPosOfWm = phHeight - wmHeight - 10;
break;
case ImagePosition.TopMiddle:
xPosOfWm = phWidth / 2;
yPosOfWm = wmWidth;
break;
default:
xPosOfWm = wmWidth;
yPosOfWm = phHeight - wmHeight - 10;
break;
}

//封装文本布局信息(如对齐、文字方向和 Tab 停靠位),显示操作(如省略号插入和国家标准 (National) 数字替换)和 OpenType 功能。
StringFormat StrFormat = new StringFormat ();

//定义需要印的文字居中对齐
StrFormat.Alignment = StringAlignment.Center;

//SolidBrush:定义单色画笔。画笔用于填充图形形状,如矩形、椭圆、扇形、多边形和封闭路径。
//这个画笔为描绘阴影的画笔,呈灰色
int m_alpha = Convert.ToInt32 ( 256 * alpha );
SolidBrush semiTransBrush2 = new SolidBrush ( Color.FromArgb ( m_alpha, 0, 0, 0 ) );

//描绘文字信息,这个图层向右和向下偏移一个像素,表示阴影效果
//DrawString 在指定矩形并且用指定的 Brush 和 Font 对象绘制指定的文本字符串。
grPhoto.DrawString ( waterWords, //string of text
crFont, //font
semiTransBrush2, //Brush
new PointF ( xPosOfWm + 1, yPosOfWm + 1 ), //Position
StrFormat );

//从四个 ARGB 分量(alpha、红色、绿色和蓝色)值创建 Color 结构,这里设置透明度为153
//这个画笔为描绘正式文字的笔刷,呈白色
SolidBrush semiTransBrush = new SolidBrush ( Color.FromArgb ( 153, 255, 255, 255 ) );

//第二次绘制这个图形,建立在第一次描绘的基础上
grPhoto.DrawString ( waterWords, //string of text
crFont, //font
semiTransBrush, //Brush
new PointF ( xPosOfWm, yPosOfWm ), //Position
StrFormat );

//imgPhoto是我们建立的用来装载最终图形的Image对象
//bmPhoto是我们用来制作图形的容器,为Bitmap对象
imgPhoto = bmPhoto;
//释放资源,将定义的Graphics实例grPhoto释放,grPhoto功德圆满
grPhoto.Dispose ();

//将grPhoto保存
imgPhoto.Save ( targetImage, ImageFormat.Jpeg );
imgPhoto.Dispose ();
return targetImage.Replace ( PicturePath, "" );
}
}


  • 打赏
  • 举报
回复
字先变成gif,然后在用图片水印
过河石头 2009-01-10
  • 打赏
  • 举报
回复
问一句,你查看图片的时候,是不是按图片的100%大小查看的?
bj890 2009-01-10
  • 打赏
  • 举报
回复
up
加载更多回复(16)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧