3つのButtonコントロールの処理
プログラムの処理は、3つのButtonコントロールのClickイベントハンドラで行います。また、PDFドキュメントの作成は、独自のプロシージャ(メソッド)「print_out」を作成し、この中に処理を組み立てていきます。
顔写真挿入処理の作成
名刺には顔写真を入れるようにしましたので、この写真挿入処理をボタン「顔写真挿入」のClickイベントハンドラに作成します。
- (1) 最初に、名前空間「System.Diagnostics」を宣言します。これは、後で作成したPDFドキュメントを表示するのに使用します。
- (2) メンバ変数を3つ宣言します。
- (3) ボタン「顔写真挿入」のClickイベントハンドラでは、「ファイルを開くダイアログ」を表示し、顔写真を選択してもらいPictureBoxコントロールに表示します。
Imports System.Diagnostics '(1)
Public Class Form1
'メンバ変数宣言
Private qr_fname As String = "" '(2)
Private cnt As Integer = 0
Private pdfProcess As Process
③ Private Sub Button1_Click(sender As System.Object, _
e As System.EventArgs) _
Handles Button1.Click '(3)
'顔写真挿入処理
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox2.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
using System.Diagnostics; //(1)
namespace QRcode_cs
{
public partial class Form1 : Form
{
// メンバ変数宣言
private string qr_fname = ""; //(2)
private int cnt = 0;
private Process pdfProcess;
private void button1_Click(object sender, EventArgs e) //(3)
{
//顔写真挿入処理
if(openFileDialog1.ShowDialog()
== System.Windows.Forms.DialogResult.OK)
{
pictureBox2.Image = Image.FromFile(openFileDialog1.FileName);
}
}
名刺印刷処理の作成
続いて、名刺印刷処理を作成します。ここでは、入力されたメールアドレスからQRコードを作成し、ビットマップファイルに保存するとともに、PDFドキュメント作成処理を呼び出します。
- (1) まず、顔写真とメールアドレスが入力されていないと印刷処理は実行しないようにします。
- (2) 次に、QRコードを作成します。GcBarCode1コントロールのValueプロパティに、入力されたメールアドレスを代入すると、その時点でQRコードが作成されます(フォームに配置したGcBarCode1コントロールのQRコードが変化します)。
- (3) 作成したQRコードを、CreateBitmapメソッドを使用してビットマップファイルに保存します。DPIは「120」を指定します。CreateBitmapメソッドは、作成したビットマップをImageオブジェクトで返してきますので、これを変数に格納します。
- (4) ImageクラスのSaveメソッドを使ってビットマップをファイルに保存します。ただし、複数の名刺を作成する場合、Saveメソッドはイメージの作成元と同じファイルにイメージを保存できないため、ファイル名を変えて保存するようにします。
- (5) PDFドキュメントを作成する処理を呼び出します。
- (6) 「終了」ボタンは、そのままプログラムを終了させます。
Private Sub Button2_Click(sender As System.Object, _
e As System.EventArgs) Handles Button2.Click
'名刺印刷処理
If PictureBox2.Image Is Nothing Then '(1)
MessageBox.Show("顔写真を選択してください", _
"入力エラー", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
End If
If TextBox4.Text = "" Then
MessageBox.Show("メールアドレスを入力してください", "入力エラー", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'QRコードの作成
GcBarCode1.Value = TextBox4.Text '(2)
Dim qrimg As Image = GcBarCode1.CreateBitmap(120) '(3)
cnt += 1 '(4)
qr_fname = Application.StartupPath + "/qrimg" _
+ cnt.ToString() + ".bmp"
qrimg.Save(qr_fname, System.Drawing.Imaging.ImageFormat.Bmp)
End If
'PDF作成
print_out() '(5)
End Sub
Private Sub Button3_Click(sender As System.Object, _
e As System.EventArgs) _
Handles Button3.Click '(6)
Me.Close()
End Sub
private void button2_Click(object sender, EventArgs e)
{
//名刺印刷処理
if (pictureBox2.Image == null) //(1)
{
MessageBox.Show("顔写真を選択してください", "入力エラー",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (textBox4.Text == "")
{
MessageBox.Show("メールアドレスを入力してください", "入力エラー",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//QRコードの作成
gcBarCode1.Value = textBox4.Text; //(2)
Image qrimg = gcBarCode1.CreateBitmap(120); //(3)
qr_fname = Application.StartupPath + "/qrimg.bmp"; //(4)
cnt++;
qr_fname = Application.StartupPath + "/qrimg" + cnt.ToString() + ".bmp";
qrimg.Save(qr_fname, System.Drawing.Imaging.ImageFormat.Bmp);
}
//PDF作成
print_out(); //(5)
}
private void button3_Click(object sender, EventArgs e) //(6)
{
this.Close();
}
