0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画面上のQRコードをデコードするCsharpのフォームアプリを作る

Posted at

下記、記事を発展させて、画面上に表示されたQRコードを、読みこむフォームを作る


QRCodeEncoderDecoderLibraryを用いてQRコードをデコードする(読み込む)Csharpの単体アプリを作る
https://qiita.com/santarou6/items/7b3a449f7f794639f252


qr_decode_frm

//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /reference:QRCodeEncoderDecoderLibrary.dll /target:winexe qr_decode_frm.cs

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using QRCodeEncoderDecoderLibrary;

namespace qr_decode_frm
{
public class Form1 : Form
{

	public Button button1;

	public Form1()
	{
	this.TransparencyKey = this.BackColor;
	button1 = new Button();
	button1.Size = new Size(40, 40);
	button1.Location = new Point(0, 0);
	button1.Text = "★";
	this.Controls.Add(button1);
	button1.Click += new EventHandler(button1_Click);
	}
	
	private void button1_Click(object sender, EventArgs e)
	{
	
	try{
	
	Rectangle rc = this.ClientRectangle;
	Point p = this.PointToScreen(new Point(0, 0));
	double rt = 1.5;  //96dpi_rt=1, 144dpi_rt=1.5
	p.X = (int)(p.X * rt);
	p.Y = (int)(p.Y * rt);
	rc.Width = (int)(rc.Width * rt);
	rc.Height = (int)(rc.Height * rt);
	Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);
	var g = Graphics.FromImage(bmp);
	g.CopyFromScreen(p.X, p.Y, 0, 0,rc.Size, CopyPixelOperation.SourceCopy);
	
	QRDecoder   QRCodeDecoder;
	Bitmap      QRCodeInputImage;
	
	QRCodeDecoder = new QRDecoder();
	QRCodeInputImage = bmp;
	byte[][] DataByteArray = QRCodeDecoder.ImageDecoder(QRCodeInputImage);
	
	//string Result = System.Text.Encoding.GetEncoding(932).GetString(DataByteArray[0]);
	string Result = System.Text.Encoding.UTF8.GetString(DataByteArray[0]);
	//string Result = System.Text.Encoding.GetEncoding(51932).GetString(DataByteArray[0]);
	
	MessageBox.Show(Result);
	//Clipboard.SetDataObject(Result,true,20,500);
	
	}
	catch(Exception ex){
		MessageBox.Show("読み取りエラー\n\n"+ex);
	}
	
	}

	[STAThread]
	static void Main()
	{
	Application.EnableVisualStyles();
	Application.Run(new Form1());
	}

}
}

結果
kekka.PNG

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?