首页 新闻 会员 周边 捐助

winform中printDocument1.Print()打印效果与预览效果不一致

0
悬赏园豆:20 [待解决问题]

大家好:

我使用背景画布和前景文本框的形式,测试打印效果。

   private void button3_Click_1(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
                this.printDocument1.Print();
        }

        private void printDocument1_PrintPage_1(object sender,System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            int x = 10;
            int y = 10;
            g.DrawImage(this.BackgroundImage, 20, 20);
            foreach (Control item in this.Controls)
            {
                if (item is TextBox)
                {
                 TextBox tx = (item as TextBox);
                 g.DrawString(tx.Text, tx.Font, Brushes.Black, tx.Left + x, tx.Top + y);
                }
            }

printPreviewDialog1.ShowDialog()预览中的效果正常,截图如下:

printDocument1.Print()能打印出来,但是效果很差。已经确认打印机没有问题。

打印出来的效果如下图,

很模糊,像是水印一样的图案。

请高手指点。。

柠檬波士的主页 柠檬波士 | 初学一级 | 园豆:182
提问于:2012-10-22 21:15
< >
分享
所有回答(4)
0

要定两个样式,一个是给展现,一个是专门写给打印的

az235 | 园豆:8483 (大侠五级) | 2012-10-22 21:37

你能说具体一点吗。我是刚学的。

支持(0) 反对(0) 柠檬波士 | 园豆:182 (初学一级) | 2012-10-22 21:41

@柠檬波士: 网页的style有一个专门写给打印的,加一个media表情,好像是这个

支持(0) 反对(0) az235 | 园豆:8483 (大侠五级) | 2012-10-23 10:02

@az235: 我是在winform中使用的。


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.printDialog1.Document = this.printDocument1; 
            this.printPreviewDialog1.Document = this.printDocument1;
            this.pageSetupDialog1.Document = this.printDocument1;

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.printPreviewDialog1.ShowDialog();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.pageSetupDialog1.ShowDialog();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
               this.printDocument1.Print();
            }
         }

        private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            int x = 15;
            int y = 10;
            g.DrawImage(this.BackgroundImage, 50, 60);
            foreach (Control item in this.Controls)
            {
                if (item is TextBox)
                {
                    TextBox tx = (item as TextBox);
                    g.DrawString(tx.Text, tx.Font, Brushes.Black, tx.Left + x, tx.Top + y);
                }
            }
           
        }

支持(0) 反对(0) 柠檬波士 | 园豆:182 (初学一级) | 2012-10-23 19:42
0

http://hi.baidu.com/24754722/item/454821165b2f790ed1d66d78

┢┦偉 | 园豆:1240 (小虾三级) | 2012-10-23 12:35
0

颜色都变了,是不是没黑墨水啦

IT屌丝 | 园豆:184 (初学一级) | 2013-08-18 11:18
0

 下面的代码能将任意的控件内容打印出来:

private void PrintButton_Click(object sender, EventArgs e)
{
var doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += (sender2, e2) => ticket.PrintToGraphics(e2.Graphics, e2.MarginBounds);
TicketPrintPreviewDialog.Document = doc;
TicketPrintPreviewDialog.ShowDialog();
}

 

 

public void PrintToGraphics(Graphics graphics, Rectangle bounds)
{
Bitmap bitmap = new Bitmap(Width, Height);
DrawToBitmap(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
Rectangle target = new Rectangle(0, 0, bounds.Width, bounds.Height);
double xScale = (double)bitmap.Width / bounds.Width;
double yScale = (double)bitmap.Height / bounds.Height;
if (xScale < yScale)
target.Width = (int)(xScale * target.Width / yScale);
else
target.Height = (int)(yScale * target.Height / xScale);
graphics.PageUnit = GraphicsUnit.Display;
graphics.DrawImage(bitmap, target);
}

淨明 | 园豆:202 (菜鸟二级) | 2015-05-10 11:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册