c# - - - winform 生成二维码

winform 生成二维码

1.1 winform 生成二维码

效果。

GIF 2024-05-20 21-39-28-生成二维码.gif

编译后的文件。

image.png

画界面。

image.png

Generate 按钮点击事件。

/// <summary>
/// 生成二维码,点击按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
    string level = comboBox1.SelectedItem.ToString();
    QRCodeGenerator.ECCLevel eCCLevel = (QRCodeGenerator.ECCLevel)(level == "L" ? 0 : level == "M" ? 1 : level == "Q" ? 2 : 3);
    using (QRCodeGenerator qRCodeGenerator = new QRCodeGenerator())
    {
        using (QRCodeData qRCodeData = qRCodeGenerator.CreateQrCode(textBox1.Text, eCCLevel))
        {
            using (QRCoder.QRCode qRCode = new QRCoder.QRCode(qRCodeData))
            {
                if (null == GetIconBitmap())
                {
                    pictureBox1.BackgroundImage = qRCode.GetGraphic(8, Color.Black, Color.White, true);
                }
                else
                {
                    pictureBox1.BackgroundImage = qRCode.GetGraphic(8, Color.Black, Color.White, GetIconBitmap(), (int)numericUpDown1.Value);
                }
                pictureBox1.Size = new System.Drawing.Size(pictureBox1.BackgroundImage.Width, pictureBox1.BackgroundImage.Height);
                //pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; // 居中
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; // 等比缩放
            }
        }
    }
}

GetIconBitmap 方法,获取用户选择的图标。

/// <summary>
/// 获取用户选择的图标
/// </summary>
/// <returns></returns>
public Bitmap GetIconBitmap()
{
    Bitmap bitmap = null;
    if (textBox2.Text.Length > 0)
    {
        try
        {
            bitmap = new Bitmap(textBox2.Text);
        }
        catch (Exception)
        {
        }
    }
    return bitmap;
}

Save QR code 按钮点击事件。

/// <summary>
/// 保存图片到本地
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.Filter = "Bitmap Image|*.bmp|PNG Image|*.png|JPeg Image|*.jpg|Gif Image|*.gif";
    saveFileDialog.Title = "Save an Image File";
    saveFileDialog.ShowDialog();

    if (!string.IsNullOrEmpty(saveFileDialog.FileName))
    {
        using (FileStream fileStream = (FileStream)saveFileDialog.OpenFile())
        {
            ImageFormat imageFormat = null;
            switch (saveFileDialog.FilterIndex)
            {
                case 1:
                    imageFormat = ImageFormat.Bmp;
                    break;
                case 2:
                    imageFormat = ImageFormat.Png;
                    break;
                case 3:
                    imageFormat = ImageFormat.Jpeg;
                    break;
                case 4:
                    imageFormat = ImageFormat.Gif;
                    break;
                default:
                    throw new NotSupportedException("File extension is not supported");
            }
            pictureBox1.BackgroundImage.Save(fileStream, imageFormat);
            fileStream.Close();
            MessageBox.Show("Successfully saved!");
        }
    }
}

Select 按钮点击事件。

/// <summary>
/// 点击选择图标文件的位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Title = "Select icon";
    openFileDialog.Multiselect = false;
    openFileDialog.CheckFileExists = true;
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        textBox2.Text = openFileDialog.FileName;
        if (numericUpDown1.Value == 0)
        {
            numericUpDown1.Value = 15;
        }
    }
    else
    {
        textBox2.Text = "";
    }
}

1.2 效果

GIF 2024-05-20 21-39-28-生成二维码.gif