首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-03-07 
asp.net 加水印后不清晰 求解决办法C# code protected void Button1_Click(object sender, EventArgs e){/

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

C# code
 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;        }

假的水印在文字周围有水渍一样的东西 求解决办法 ,
还有这个是在读取是损坏了还是在保存时损坏的,求各位大神指点啊

[解决办法]
一把水印图变成高分辨率的清晰图片,
 System.Drawing.Image bitmap = new System.Drawing.Bitmap(1024, 634); //新建bmp图片
缩小它,或变成水印的分辨率。

说白了就是分辨率的问题
[解决办法]
高质量
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);

热点排行