水印图片

shaohui139 2009-05-05 03:56:41
在给图片添加文字的时候,如果文字长度超出图片的宽度,如何让文字自动换行呢,在网上搜了半天也没找到好的解决办法,
部分代码如下:
        protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName.Trim() != "")
{
//上传文件
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
string path = Server.MapPath("web/") + fileName + extension;
FileUpload1.PostedFile.SaveAs(path);

//加文字水印,
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("微软雅黑", 20);
Brush b = new SolidBrush(Color.Yellow);
g.DrawString("若要检查宏安全性设置,请单击 Microsoft Office 按钮,再单击“<程序名> 选项”,然后依次单击“信任中心”、“信任中心设置”。水印文字", f, b, 30, 20);
g.Dispose();


//保存加水印过后的图片,删除原始图片
string newPath = Server.MapPath("web/") + fileName + "_new" + extension;
image.Save(newPath);
image.Dispose();
if (File.Exists(path))
{
File.Delete(path);
}

Image1.ImageUrl = newPath;
}


}
...全文
439 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sssssssc 2009-07-01
  • 打赏
  • 举报
回复

SizeF _Size = gPrimalClothing.MeasureString(_Value, f, 200);//

gPrimalClothing指的是?
能说具体点吗?

cpio 2009-05-06
  • 打赏
  • 举报
回复
这种自动换行会把连起来的字母当成单词,所以要换行

如果要严格控制的话,需要进行量测,一个个的加,加到超出之后,减到最后加上去那个字符
shaohui139 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zgke 的回复:]
Graphics _Graphcis = Graphics.FromHwnd(panel1.Handle);

Font f = new Font("微软雅黑", 20);
Brush b = new SolidBrush(Color.Yellow);

string _Value = "若要检查宏安全性设置,请单击 Microsoft Office 按钮,再单击“ <程序名> 选项”,然后依次单击“信任中心”、“信任中心设置”。水印文字";

SizeF _Size= _Graphcis.MeasureString(_Value, f, 100);

_Graphcis.DrawString(_Value, f, b, new RectangleF(3…
[/Quote]
改成如下:
                Font f = new Font("微软雅黑", 20);
Brush b = new SolidBrush(Color.Black);

string _Value = "上岛咖啡几mmmmmm点思考第三方接口的肌肤看到房价的开发进度看房记的疯狂的肌肤";

SizeF _Size = gPrimalClothing.MeasureString(_Value, f, 200);//

// _Graphcis.DrawString(_Value, f, b, new RectangleF(30, 20, _Size.Width, _Size.Height));

gPrimalClothing.DrawString(_Value, f, b,new RectangleF(265, 70, _Size.Width, _Size.Height));

生成结果:


shaohui139 2009-05-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cpio 的回复:]
g.DrawString的时候,用这个重载函数

public void DrawString (
string s,
Font font,
Brush brush,
RectangleF layoutRectangle,
StringFormat format
)


示例:
(PaintEventArgs e)
{

// Create string to draw.
String drawString = "Sample Text";

// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);

// Create rectangle …
[/Quote]
改成以下:
            String drawString = "上岛咖啡几mmmmmm点思考第三方接口的肌肤看到房价的开发进度看房记的疯狂的肌肤";

Font f = new Font("微软雅黑", 20);
Brush b = new SolidBrush(Color.Black);
float width = 200.0F;
float height = 50.0F;
RectangleF drawRect = new RectangleF(265, 70, width, height);

// Draw rectangle to screen.
Pen blackPen = new Pen(Color.Transparent);
gPrimalClothing.DrawRectangle(blackPen,265, 70, width, height);

// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;

// Draw string to screen.
gPrimalClothing.DrawString(drawString, f, b, drawRect, drawFormat);

生成结果:
“上岛咖啡几”还没到边界就换行了(到边界的距离至少能容纳一个汉字),估计是碰到了后面的英文字母,这时候该如何设置呢
shaohui139 2009-05-05
  • 打赏
  • 举报
回复
感谢你们的回复~~
CqCoder 2009-05-05
  • 打赏
  • 举报
回复
关注中
rainsome 2009-05-05
  • 打赏
  • 举报
回复
mark
zgke 2009-05-05
  • 打赏
  • 举报
回复
Graphics _Graphcis = Graphics.FromHwnd(panel1.Handle);

Font f = new Font("微软雅黑", 20);
Brush b = new SolidBrush(Color.Yellow);

string _Value = "若要检查宏安全性设置,请单击 Microsoft Office 按钮,再单击“<程序名> 选项”,然后依次单击“信任中心”、“信任中心设置”。水印文字";

SizeF _Size= _Graphcis.MeasureString(_Value, f, 100);

_Graphcis.DrawString(_Value, f, b, new RectangleF(30, 20, _Size.Width, _Size.Height));

_Graphcis.Dispose();


这样指定宽 让自己绘制换行.
gyouyang 2009-05-05
  • 打赏
  • 举报
回复
没遇到这个问题... 帮忙顶 up
cpio 2009-05-05
  • 打赏
  • 举报
回复
g.DrawString的时候,用这个重载函数

public void DrawString (
string s,
Font font,
Brush brush,
RectangleF layoutRectangle,
StringFormat format
)


示例:
(PaintEventArgs e)
{

// Create string to draw.
String drawString = "Sample Text";

// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);

// Create rectangle for drawing.
float x = 150.0F;
float y = 150.0F;
float width = 200.0F;
float height = 50.0F;
RectangleF drawRect = new RectangleF(x, y, width, height);

// Draw rectangle to screen.
Pen blackPen = new Pen(Color.Black);
e.Graphics.DrawRectangle(blackPen, x, y, width, height);

// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;

// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
}

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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