在PrintPreview中更改了绘图位置。 [英] Drawing position is changed in PrintPreview.

查看:75
本文介绍了在PrintPreview中更改了绘图位置。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WinForms项目中工作,我在打印文本时遇到了问题。所以我创建了简单的示例来测试问题。  在Form中,我使用
Form.Graphics 手动绘制了两行之间的字符串,并使用 PrintPageEventArgs 再次完成相同的操作.Graphics
PrintDocument .PrintPage 事件中。在打印预览中更改了绘图位置。请看下面的图像,它显示了问题(即绘制Form.Graphics和
PrintPageEventArgs .Graphics)之间的不同。请告诉我,为什么绘图位置会改变?或者有没有解决这个问题的工作?。

I’m working in WinForms project and I have the queries in Printing the text . So I have created the simple sample to test the issue.  In Form, I have drawn the string between two lines manually by using Form.Graphics and done the same again by using PrintPageEventArgs.Graphics in PrintDocument.PrintPage event. The drawing position is changed in print preview. Please see the below image , that show the issue(i.e drawing the lines is differed between Form.Graphics and PrintPageEventArgs.Graphics). Please let me know , why the drawing position is changed? or Is there any work around to solve this issue?.

public Form1()
{
    InitializeComponent();
    this.Paint += Form1_Paint;
}

void Document_PrintPage(object sender, PrintPageEventArgs e)
{
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 10, 10, 10, 25);
    e.Graphics.DrawString("Some Chars are getting Cut in Print Preview", this.Font, new SolidBrush(Color.Red), 10, 10);
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 228, 10, 228, 25);
}

void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 10, 10, 10, 25);
    e.Graphics.DrawString("Some Chars are getting Cut in Print Preview", this.Font, new SolidBrush(Color.Red), 10, 10);
    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 228, 10, 228, 25);
}

private void button1_Click(object sender, EventArgs e)
{
    PrintPreviewDialog ppd = new PrintPreviewDialog();
    PrintDocument doc = new PrintDocument();
    ppd.Document = doc;
    ppd.Document.PrintPage += Document_PrintPage;
    ppd.ShowDialog();
}






示例:  TestSample

Sample: TestSample

谢谢

Piruthiviraj

Piruthiviraj

Piruthiviraj

Piruthiviraj

推荐答案

嗨  Piruthiviraj,

请不要用特定的数字来修复长度,而不是根据要判断的字符串的长度来确定, 尝试下面的代码,希望这可以帮助你:

void FrmPrintPreview_Paint(object sender, PaintEventArgs e)
        {
            string measureString = "Some Chars are getting Cut in Print Preview";
            SizeF stringSize = new SizeF();
            stringSize = e.Graphics.MeasureString(measureString, this.Font);

            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 10, 10, 10, 25);
            e.Graphics.DrawString(measureString, this.Font, new SolidBrush(Color.Red), 10, 10);
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), stringSize.Width + 10, 10, stringSize.Width + 10, 25);
        }
void Document_PrintPage(object sender, PrintPageEventArgs e)
        {
            string measureString = "Some Chars are getting Cut in Print Preview";
            SizeF stringSize = new SizeF();
            stringSize = e.Graphics.MeasureString(measureString, this.Font);

            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), 10, 10, 10, 25);
            e.Graphics.DrawString(measureString, this.Font, new SolidBrush(Color.Red), 10, 10);
            e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Red)), stringSize.Width + 10, 10, stringSize.Width + 10, 25);
        }




最好的问候,

鲍勃< span style ="color:black; font-family:'Calibri',sans-serif; font-size:11pt">


这篇关于在PrintPreview中更改了绘图位置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆