Chinaunix首页 | 论坛 | 博客
  • 博客访问: 840931
  • 博文数量: 366
  • 博客积分: 10267
  • 博客等级: 上将
  • 技术积分: 4290
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:04
文章分类

全部博文(366)

文章存档

2012年(366)

分类: 系统运维

2012-04-04 18:15:11

在开发过程中由于公司暂时没有证卡打印机,只好通过普通的A4纸打印机做练习。咱们的普通黑白打印机在打印一些文字和图片犹豫是黑白颜色的,所以显示不出来什么效果,无论调整大小和分辨率出来的效果几乎一致。在项目上线后客户买了证卡打印机结果打印出来的效果特别差(马赛克)。因为卡片的尺寸是338×213的,所以起初设计的发送给打印机的图片大小也是338×213的,经过多次个方面的测试打印出来的效果都是极差,问了问证卡打印机在中关村的代理商,他们告诉结果确是调整图片的DPI,但是DPI调整后仍然不行。当时我也猜到人家只是代理商,估计过多的技术问题他们也不懂。后来代理商发了一些证卡打印机厂商给一些资料里发现设计的图片要比338×213大N倍才可以。

  既然是证卡打印机所以卡片上的内容都是动态生成的。所以要根据设计的文字和图片大小动态去调整大小才可以实现。按照厂商给提供的图片大小1004×650打印出来效果比以前好多了,但是文字会出现锯齿。解决文字出现的锯齿问题如下:

起初是将文字画在了背景图片上面所以出现了锯齿,后来发现既然证卡打印机也是驱动打印,所以干脆先发送一张照片过去然后在发送文字过去,打印出来的效果解决了。

废话不再多说了,实现的代码如下:

void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            Image img = null;
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            
            //正面背景图
            if (cdCfg.BackImgFlg && cardType.ZMBJT != null)
            {
                try
                {
                    //背景展示
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(cardType.ZMBJT))
                    {
                        img = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter().Deserialize(ms) as Image;
                    }
                }
                catch { }
            }
            //发送打印背景图片
            if (img != null)
            {
                e.Graphics.DrawImage(img, 0, 0, picBackGround1.Width + 5, picBackGround1.Height + 2);
            }
            //发送照片
            if (cdCfg.PhotoFlag)
            {
                //判断是否存在照片
                if (_user.ZP != null)
                {
                    try
                    {
                        //照片展示
                        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(_user.ZP))
                        {
                            //画照片
                            e.Graphics.DrawImage(Image.FromStream(ms), cdCfg.PhotoLocation.X, cdCfg.PhotoLocation.Y, cdCfg.PhotoSize.Width, cdCfg.PhotoSize.Height);
                        }
                    }
                    catch (Exception ex) { FormFactory.ShowMessageBoxErrorDialog(this, "加载照片失败!", ex.Message); }
                }
            }
            //发送要打印的文字信息
            //循环遍历要打印的标签(cdCfg是已经设计好的文字信息,包含了文字的颜色字体大小等)
            for (int i = 0; i < cdCfg.LabelCfgList.Length; i++)
            {
                Font font = new Font(cdCfg.LabelCfgList[i].MyFont.Name, cdCfg.LabelCfgList[i].MyFont.Size, cdCfg.LabelCfgList[i].MyFont.Style);
                SolidBrush sbrush = new SolidBrush(cdCfg.LabelCfgList[i].MyColor);
                PointF point = new PointF(cdCfg.LabelCfgList[i].MyLocation.X, cdCfg.LabelCfgList[i].MyLocation.Y);
                //发送要打印的文字
                if (cdCfg.LabelCfgList[i].VariablFlag)
                {
                    e.Graphics.DrawString(GetValueByName(_user, cdCfg.LabelCfgList[i].VariableName), font, sbrush, point);
                }
                else
                {
                    e.Graphics.DrawString(cdCfg.LabelCfgList[i].Text, font, sbrush, point);
                }
            }
            //e.Graphics.DrawImage(picBackGround1.BackgroundImage, 0, 0, picBackGround1.Width, picBackGround1.Height);
            e.HasMorePages = false;
        }

 

如果代码大家看不懂话可以给我留言。

阅读(1752) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~