笔记-CppCLR_WinForms操作ThoughtWorks.QRCode二维码生成,保存,打印

1.在VS中下载ThoughtWorks.QRCode.dll
笔记-CppCLR_WinForms操作ThoughtWorks.QRCode二维码生成,保存,打印_第1张图片
2.二维码生成函数,返回为Bitmap

private: Bitmap^DrawQRCodeBmp(String^ content,
        int mENCODE_MODE,
        int mQRCodeScale,
        int mQRCodeVersion,
        int mERROR_CORRECTION,
        System::Text::Encoding^ encoding)
{
    if (content == nullptr)
    {
        return nullptr;
    }
    if (mQRCodeScale <= 0 || mQRCodeScale > 10)
    {
        return nullptr;
    }
    if (mENCODE_MODE<(int)ThoughtWorks::QRCode::Codec::QRCodeEncoder::ENCODE_MODE::ALPHA_NUMERIC || mENCODE_MODE>(int)ThoughtWorks::QRCode::Codec::QRCodeEncoder::ENCODE_MODE::BYTE)//BYTE = 2 
    {
        return nullptr;
    }
    //公式是:(V - 1) * 4 + 21(V是版本号)最高Version 40,(40 - 1) * 4 + 21 = 177,所以最高是177 x 177 的正方形。
    //图片大小:[(V - 1) * 4 + 21]*QRCodeScale
    //根据控件大小确定最佳参数;
    //Y=(V - 1) * 4 + 21;最小尺寸
    //L=Y*QRCodeScale;
    //数据容量=(21×21 - 8*9*2-9*9 -4*1*2);Y-8*9*2-9*9 -4*V*2

    if (mQRCodeVersion < 0 || mQRCodeVersion>40)//0-40;
    {
        return nullptr;
    }
    if (mERROR_CORRECTION<(int)ThoughtWorks::QRCode::Codec::QRCodeEncoder::ERROR_CORRECTION::L || mERROR_CORRECTION>(int)ThoughtWorks::QRCode::Codec::QRCodeEncoder::ERROR_CORRECTION::H)//0-3;L,M,Q,H
    {
        return nullptr;
    }
    QRCodeEncoder^ qrCodeEncoder = gcnew QRCodeEncoder();
    qrCodeEncoder->QRCodeEncodeMode = (ThoughtWorks::QRCode::Codec::QRCodeEncoder::ENCODE_MODE) mENCODE_MODE;
    qrCodeEncoder->QRCodeScale = mQRCodeScale;
    qrCodeEncoder->QRCodeVersion = mQRCodeVersion;
    qrCodeEncoder->QRCodeErrorCorrect = (ThoughtWorks::QRCode::Codec::QRCodeEncoder::ERROR_CORRECTION)mERROR_CORRECTION;
    return qrCodeEncoder->Encode(content, encoding);
}

3.二维码格式,图片显示调节

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
    if (this->textBox1->Text->Length > 2048)        
    {
        System::Windows::Forms::MessageBox::Show("内容过多!", "错误", System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::Error);
        return;
    }
    if (this->textBox1->Text->Length ==0)       
    {
        System::Windows::Forms::MessageBox::Show("内容为空!", "错误", System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::Error);
        return;
    }

    int mQRCodeScale;
    int mQRCodeVersion;
    mQRCodeScale = System::Convert::ToInt16(this->numericUpDown1->Value);
    mQRCodeVersion = System::Convert::ToInt16(this->numericUpDown2->Value);

    String ^mString = gcnew String(this->textBox1->Text->ToString());
    try
    {
        this->image = DrawQRCodeBmp(mString, 2, mQRCodeScale, mQRCodeVersion, 1, System::Text::Encoding::UTF8);
    }

    catch (Exception ^ e)
    {
        System::Windows::Forms::MessageBox::Show("编码出错", "错误", System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::Error);
        return;
    }

    if (this->image==nullptr)
    {
        return;
    }

    this->pictureBox1->Image =this->image;

    //不让图片超出控件范围;
    if (this->image->Height>this->pictureBox1->Height)
    {
        this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;

    }
    else
    {
        this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::CenterImage;
    }

    this->label1->Text = "" + this->image->Height + "x" + this->image->Width;
}

4.保存二维码图片

private: System::Void button8_Click(System::Object^  sender, System::EventArgs^  e) 
{
    if (this->image == nullptr)
    {
        System::Windows::Forms::MessageBox::Show("没有图片", "错误", System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::Error);
        return;
    }
    this->saveFileDialog1->FileName = "IMG_" + System::DateTime::Now.ToString("yyyy-MM-dd_HH-mm-ss");

    if (this->saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
    {

        this->image->Save(this->saveFileDialog1->FileName, System::Drawing::Imaging::ImageFormat::Jpeg);

        System::Windows::Forms::MessageBox::Show("保存成功", "提示", System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::None);

    }
}

5.打印二维码图片

private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) 
{   
    this->printDocument1->DocumentName = "IMG_" + System::DateTime::Now.ToString("yyyy-MM-dd_HH-mm-ss");
    //设置Document属性PrintDialog到PrintDocument添加到窗体。
    this->printDialog1->Document = this->printDocument1;
    if (printDialog1->ShowDialog()==System::Windows::Forms::DialogResult::OK)
    {
        this->printDocument1->Print();
    }

}

6.运行效果
笔记-CppCLR_WinForms操作ThoughtWorks.QRCode二维码生成,保存,打印_第2张图片

你可能感兴趣的:(Windows窗体应用)