asp.net 加水印后不清晰 求解决办法

ytdiary 2011-12-24 10:34:55
 protected void Button1_Click(object sender, EventArgs e)
{
// 根据选中状态初始化值
float pos_t_x = 390.0f;
float pos_t_y = 132.0f;
float pos_b_x = 980.0f;
float pos_b_y = 363.0f;

if (RadioButton2.Checked == true)
{
pos_t_x = 430;
pos_t_y = 125;
pos_b_x = 970;
pos_b_y = 380;
}

if (RadioButton3.Checked == true)
{
pos_t_x = 75;
pos_t_y = 143;
pos_b_x = 680;
pos_b_y = 380;
}

string Filename =string.Empty;
string file = Server.MapPath("") + "\\" + Label1.Text.Replace("/", "\\");

file = file.Replace("image", "card_bg");
file = file.Replace("_all", "");

System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(DateTime.Now.Year.ToString());
sb.Append(DateTime.Now.Month.ToString());
sb.Append(DateTime.Now.Day.ToString());
sb.Append(DateTime.Now.Hour.ToString());
sb.Append(DateTime.Now.Minute.ToString());
sb.Append(DateTime.Now.Second.ToString());
sb.Append(".jpg");

Filename = sb.ToString();

//生成缩略图
//原始图片名称
//生成高质量图片名称
string strFile = Server.MapPath("~") + "/card/card_" + Filename;

//从文件获取图片对象
//System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);
System.Drawing.Image image = System.Drawing.Image.FromFile(@file);

System.Drawing.Image bitmap = new System.Drawing.Bitmap(1024, 634); //新建bmp图片
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //新建画板
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //制定高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量、低速度呈现平滑程度
g.Clear(System.Drawing.Color.White); //清空画布
//在制定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel);


int fontsize = 20; // 文字大小


// 抬头
//文字水印
System.Drawing.Graphics testGrahpics = System.Drawing.Graphics.FromImage(bitmap);


System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
string fontpath = Server.MapPath("font/font.ttf");//相对于虚拟目录的字体文件路径
pfc.AddFontFile(fontpath);

System.Drawing.Font font = new System.Drawing.Font(pfc.Families[0], fontsize);

System.Drawing.Brush brush = new System.Drawing.SolidBrush(ColorTranslator.FromHtml("#E0DEDD"));

//分行
string sInput ="尊敬的" + TextBox3.Text.Trim().ToString() + DropDownList1.SelectedItem.Text.ToString() + ":"; //获取输入的水印文字

testGrahpics.DrawString(sInput, font, brush, pos_t_x, pos_t_y);

fontsize = 14;

// 写落尾---------------------------------------------------------------


testGrahpics = System.Drawing.Graphics.FromImage(bitmap);
font = new System.Drawing.Font(pfc.Families[0], fontsize);
sInput = TextBox4.Text.Trim().ToString() + "致"; //获取输入的水印文字


// 为了保证落尾右侧对其,需要重新计算其X初始坐标
pos_b_x = pos_b_x - (sInput.Length) *(5.0f + (float) fontsize);

testGrahpics.DrawString(sInput, font, brush, pos_b_x, pos_b_y);

testGrahpics.Dispose();
//保存缩略图c
try
{
bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
Response.Write("保存缩略图失败" + ex.Message);
}
//释放资源
g.Dispose();
bitmap.Dispose();
image.Dispose();
Image1.ImageUrl = "/card/card_" + Filename;
Image1.Visible = true;
HyperLink1.NavigateUrl = "/card/card_" + Filename;
}

假的水印在文字周围有水渍一样的东西 求解决办法 ,
还有这个是在读取是损坏了还是在保存时损坏的,求各位大神指点啊
...全文
98 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ytdiary 2011-12-24
  • 打赏
  • 举报
回复
3Q 不解决问题啊

我用png格式了 会好点
wuyq11 2011-12-24
  • 打赏
  • 举报
回复
高质量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1]; System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
ytdiary 2011-12-24
  • 打赏
  • 举报
回复
谢谢

但是添加水印后图片大小不能改啊。。。。
或者考虑其他实现方式也可以
全局变量 2011-12-24
  • 打赏
  • 举报
回复
一把水印图变成高分辨率的清晰图片,
System.Drawing.Image bitmap = new System.Drawing.Bitmap(1024, 634); //新建bmp图片
缩小它,或变成水印的分辨率。

说白了就是分辨率的问题

111,125

社区成员

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

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

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