大家好:
我使用背景画布和前景文本框的形式,测试打印效果。
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()能打印出来,但是效果很差。已经确认打印机没有问题。
打印出来的效果如下图,
很模糊,像是水印一样的图案。
请高手指点。。
要定两个样式,一个是给展现,一个是专门写给打印的
你能说具体一点吗。我是刚学的。
@柠檬波士: 网页的style有一个专门写给打印的,加一个media表情,好像是这个
@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);
}
}
}
http://hi.baidu.com/24754722/item/454821165b2f790ed1d66d78
颜色都变了,是不是没黑墨水啦
下面的代码能将任意的控件内容打印出来:
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);
}