2017-04-05 30 views
1

我已經有了一個完全生成QR碼的QR控制,當我用手機掃描時,它完美地工作。但是我無法將其轉換爲圖像,因此我可以將其保存到數據庫中。我查看了他們的網站,我找不到任何東西,他們只有在WinForms上使用WPF或Web。 這是我如何生成QR碼如何將UltraQRCodeBarcode Infragistics WinForms控件轉換爲圖像?

private void btnGenerate_Click(object sender, EventArgs e) 
{ 
    string data = string.Empty; 
    var fullName = txtFirstName + " " + txtMiddleName + " " + txtLastName; 
    if (!string.IsNullOrEmpty(txtLicenceNumber.Text)) 
    { 
     data = fullName; 
     data += ", " + Environment.NewLine + txtDistrict.Text; 
     data += ", " + Environment.NewLine + txtLicenceNumber.Text; 
     data += ", " + Environment.NewLine + txtRegistrationCode; 
    } 
    ultraQRCodeBarcode1.Data = data; 
} 

,這是我的拯救方法在哪裏我試圖將其轉換成字節代碼並保存 但我不是成功的,沒有財產形象在這一。

user.QrCode = ImageToByteArray(ultraQRCodeBarcode1.); 

圖像轉換

private byte[] ImageToByteArray(string imagePath) 
{ 
    byte[] data = null; 
    var fileInfo = new FileInfo(imagePath); 
    long numBytes = fileInfo.Length; 
    var fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read); 
    var binaryReader = new BinaryReader(fileStream); 
    data = binaryReader.ReadBytes((int)numBytes); 
    return data; 
} 

回答

1

UltraQRCodeBarcode具有SaveTo方法。此方法有幾個重載,允許您將QR碼保存爲圖像或存儲流。在你的情況下,我會創建一個內存流,然後將此流轉換爲像這樣的字節數組:

byte[] data; 
using(MemoryStream ms = new MemoryStream()) 
{ 
    this.ultraQRCodeBarcode1.SaveTo(ms, ImageFormat.Png); 
    byte[] data = ms.ToArray(); 
}