使用正确的方式在图片框上绘画会弄乱输出

时间:2019-04-19 15:32:44

标签: c# .net graphics

我正在使用图片框作为预览。当我做错时,图片框与打印输出匹配。当我将其更改为使用位图时,它看起来很差。我检查了图形对象的DpiX和DpiY,所有这些都为96。可以使用以下代码演示该问题。创建一个带有按钮和图片框的表单。点击按钮。然后取消注释行“ // Correctway = true;”。并观察差异。

public partial class Form1 : Form
{

    private bool Correctway;

    public Form1()
    {
        InitializeComponent();
        button1.Click += Button1_Click;
    }


    private void Button1_Click(Object sender, EventArgs e)
    {
        PrintPageEventArgs eOutput;
        Graphics g;
        string OutputText;
        Font PrintFont;
        Bitmap Output;



        //Correctway = true;
        OutputText = "CERTIFICATION";
        PrintFont = new Font("Arial", 16, FontStyle.Regular);
        Output = new Bitmap(850, 1100);
        if (Correctway)
            g = Graphics.FromImage(Output);
        else
            g = pictureBox1.CreateGraphics();
        eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
        eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
        eOutput.Graphics.DrawRectangle(Pens.Gray, 20, 30, Output.Width - 100, Output.Height - 130);
        if (Correctway)
            pictureBox1.Image = Output;

    }

}

“不良”输出。我希望我可以更好地描述它,但是我不知道发生了什么。 This is the poor output

这是所需的输出,是打印结果的输出,而当我使用不正确的步骤从图片框创建图形对象时的屏幕输出。 This is the desired output 注意:我尝试过“也可以按CTRL + G插入图像。”但它不起作用。有或没有剪贴板上的图像。

1 个答案:

答案 0 :(得分:0)

Taw在他的评论中找到了答案。我在这里发布它以完成这篇文章。解决方案是使用 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

public partial class Form1 : Form
{

    private bool Correctway;

    public Form1()
    {
        InitializeComponent();
        button1.Click += Button1_Click;
    }


    private void Button1_Click(Object sender, EventArgs e)
    {
        PrintPageEventArgs eOutput;
        Graphics g;
        string OutputText;
        Font PrintFont;
        Bitmap Output;



        //Correctway = true;
        OutputText = "CERTIFICATION";
        PrintFont = new Font("Arial", 16, FontStyle.Regular);
        Output = new Bitmap(850, 1100);
        if (Correctway)
            g = Graphics.FromImage(Output);
        else
            g = pictureBox1.CreateGraphics();
        eOutput = new PrintPageEventArgs(g, new Rectangle(new Point(25, 25), new Size(new Point(825, 1075))), new Rectangle(new Point(0, 0), new Size(new Point(850, 1100))), new PageSettings());
        eOutput.Graphics.DrawString(OutputText, PrintFont, Brushes.Black, 0, 0);
        eOutput.Graphics.DrawRectangle(Pens.Gray, 20, 30, Output.Width - 100, Output.Height - 130);
        if (Correctway)
            pictureBox1.Image = Output;

    }

}