我正在使用this library在WinForm应用程序中生成QRcode,但是我不知道如何使用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中生成它)。
发布于 2013-04-30 15:33:28
如果你把你的图片放在一个picturebox中,并且你只生成一次图片,那么你不需要担心paint方法(你不是在做动画等等,它只是一个二维码)
只需在表单加载(或生成图像的任何位置)中执行此操作
mypicturebox.Image = qrCodeImage;更新-为您的库提供便利的附加代码
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;发布于 2013-04-30 16:40:13
这就是我最终所做的:
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);
}
}https://stackoverflow.com/questions/16294506
复制相似问题