Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用OnPaint()方法_C#_Winforms_Qr Code_Onpaint - Fatal编程技术网

C# 使用OnPaint()方法

C# 使用OnPaint()方法,c#,winforms,qr-code,onpaint,C#,Winforms,Qr Code,Onpaint,我正在使用将QRcode生成到WinForm应用程序中,但我真的不知道如何使用OnPaint()方法 所以我有这个: public partial class Form1 : Form { public Form1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { QrEncoder encoder = new QrEncoder(ErrorCor

我正在使用将QRcode生成到WinForm应用程序中,但我真的不知道如何使用OnPaint()方法

所以我有这个:

public partial class Form1 : Form
{
  public Form1()
  {
    InitializeComponent();
  }

  protected override void OnPaint(PaintEventArgs e)
  {
    QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
    QrCode qrCode;
    encoder.TryEncode("link to some website", out qrCode);

    new GraphicsRenderer(new FixedCodeSize(200, QuietZoneModules.Two))
                             .Draw(e.Graphics, qrCode.Matrix);

    base.OnPaint(e);
  }

  private void Form1_Load(object sender, EventArgs e)
  {
    this.Invalidate();
  }
}

我在表单中有一个简单的pictureBox,我只想在其中生成QRcode图像(如果有可能在pictureBox中生成)。

如果您将图像放入pictureBox,并且只生成一次图像,那么您不必担心绘制方法(您没有制作动画等,它只是QRcode)

只需在表单加载中执行此操作(或在任何地方生成图像)

更新-附加代码以方便您的库

    var bmp = new Bitmap(200, 200);
    using (var g = Graphics.FromImage(bmp))
    {
        new GraphicsRenderer(
            new FixedCodeSize(200, QuietZoneModules.Two)).Draw(g, qrCode.Matrix);
    }
    pictureBox1.Image = bmp;

如果您将图像放入picturebox,并且只生成一次图像,则无需担心绘制方法(您没有制作动画等,它只是一个二维码)

只需在表单加载中执行此操作(或在任何地方生成图像)

更新-附加代码以方便您的库

    var bmp = new Bitmap(200, 200);
    using (var g = Graphics.FromImage(bmp))
    {
        new GraphicsRenderer(
            new FixedCodeSize(200, QuietZoneModules.Two)).Draw(g, qrCode.Matrix);
    }
    pictureBox1.Image = bmp;

这就是我最终所做的:

public partial class Form1 : Form
    {
        public event PaintEventHandler Paint;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
            this.Controls.Add(pictureBox1);
        }

        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode;
            encoder.TryEncode("www.abix.dk", out qrCode);

            new GraphicsRenderer(
                new FixedCodeSize(200, QuietZoneModules.Two)).Draw(e.Graphics, qrCode.Matrix);
        }
    }

这就是我最终所做的:

public partial class Form1 : Form
    {
        public event PaintEventHandler Paint;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox_Paint);
            this.Controls.Add(pictureBox1);
        }

        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            QrEncoder encoder = new QrEncoder(ErrorCorrectionLevel.M);
            QrCode qrCode;
            encoder.TryEncode("www.abix.dk", out qrCode);

            new GraphicsRenderer(
                new FixedCodeSize(200, QuietZoneModules.Two)).Draw(e.Graphics, qrCode.Matrix);
        }
    }

使用此代码,您将直接在表单1上绘制smowhere。我认为此链接可能会帮助您在表单1上直接绘制smowhere。我认为此链接可能会帮助您,但库中没有返回类型图像的方法,因此我可以将其分配给图片框。我还尝试了
pictureBox.Invalidate()
(因此它不是“表单中的任何位置”,但仍然没有结果)。非常好!现在,我可以轻松地将生成的图像物理保存为位图文件。非常感谢你!但是库中没有返回类型图像的方法,因此我可以将其分配给图片框。我还尝试了
pictureBox.Invalidate()
(因此它不是“表单中的任何位置”,但仍然没有结果)。非常好!现在,我可以轻松地将生成的图像物理保存为位图文件。非常感谢你!虽然这样做有效,但每次重新绘制表单时都会重复。我已经更新了上面的答案-如果你将该代码放入表单加载(或单击按钮等),那么你只需生成一次QR图像-希望这有帮助:)尽管这样做有效,但每次重新绘制表单时都会重复。我已经更新了上面的答案-如果您将该代码放入表单加载(或单击按钮等),那么您只需生成一次QR图像-希望有帮助:)