ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 589|ตอบกลับ: 0

[VB.NET] การพิมพ์ QR Code ลงในรายงาน RDLC และแสดงผลบน ReportViewer

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6060

การสร้างรายงาน (Report) หรือไฟล์ที่มีนามสกุล RDLC เพื่อแสดงผลบน ReportViewer ซึ่งพวกนี้เป็น Control มาตรฐานที่ทางไมโครซอฟท์แถมติดมาให้ด้วย โดยโค้ดชุดนี้ไม่ได้ว่าถึงเรื่องของฐานข้อมูล แต่จะเป็นการส่งค่าพารามิเตอร์ (Parameter) ไปยังเอกสาร RDLC คือชื่อไฟล์ภาพ และตำแหน่งที่เก็บภาพ เพื่อนำมาแสดงผลในรายงาน ...


หน้าจอหลัก


การ Add Parameters


การกำหนด Parameter


Add References ...


https://www.youtube.com/watch?v=bW8hksxTUHk
คลิปวิดีโอการสร้าง Parameter ที่จะต้องใช้งานในโปรเจค ...  

มาดูโค้ดต้นฉบับกันเถอะ ...
  1. Imports ZXing.Common
  2. Imports ZXing
  3. Imports ZXing.Rendering
  4. Imports ZXing.QrCode
  5. Imports System.IO
  6. Imports System.Drawing.Imaging
  7. Imports Microsoft.Reporting.WinForms

  8. Public Class frmZXingReportViewer
  9.     '// Images Path
  10.     Private strPathImages As String = MyPath(Application.StartupPath) & "Images" & Chr(92)

  11.     ' / --------------------------------------------------------------------------------
  12.     Private Sub frmZXingReportViewer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  13.         txtURL.Text = "ทดสอบภาษาไทยสำหรับ ZXing QR Code เพื่อพิมพ์รายงานใน RDLC (Report Viewer)"
  14.         'txtURL.Text = "http://www.g2gsoft.com/"
  15.     End Sub

  16.     ' / --------------------------------------------------------------------------------
  17.     '// Generate QR Barcodes based on the specified text.
  18.     ' / --------------------------------------------------------------------------------
  19.     Private Sub txtURL_TextChanged(sender As Object, e As System.EventArgs) Handles txtURL.TextChanged
  20.         If String.IsNullOrWhiteSpace(txtURL.Text) Then
  21.             picBarcode.Image = Nothing
  22.             Return
  23.         End If
  24.         '//
  25.         Dim options As EncodingOptions = New QrCodeEncodingOptions
  26.         With options
  27.             .Margin = 1
  28.             .NoPadding = True
  29.             .Width = picBarcode.Width
  30.             .Height = picBarcode.Height
  31.             .Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8")
  32.             .PureBarcode = False
  33.         End With
  34.         '//
  35.         Dim objWriter As BarcodeWriter = New BarcodeWriter With {
  36.                 .Format = BarcodeFormat.QR_CODE,
  37.                 .Options = options,
  38.                 .Renderer = New BitmapRenderer
  39.             }
  40.         picBarcode.Image = New Bitmap(objWriter.Write(txtURL.Text))
  41.         picBarcode.SizeMode = PictureBoxSizeMode.StretchImage
  42.     End Sub

  43.     ' / --------------------------------------------------------------------------------
  44.     '// Print QR Code into RDLC.
  45.     ' / --------------------------------------------------------------------------------
  46.     Private Sub btnPrintQRCode_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintQRCode.Click
  47.         If String.IsNullOrWhiteSpace(txtURL.Text) Then
  48.             picBarcode.Image = Nothing
  49.             Return
  50.         End If
  51.         Try
  52.             Dim imageFullPath As String = strPathImages & "temp.png"
  53.             '// Save picture.
  54.             '//Dim FileToSaveAs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, imageFullPath)
  55.             Dim FileToSaveAs As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, imageFullPath)
  56.             picBarcode.Image.Save(FileToSaveAs, System.Drawing.Imaging.ImageFormat.Png)
  57.             '// Parameter names in RDLC, uppercase and lowercase affect functionality. (picname not the same as PicName)
  58.             Dim pictureName As ReportParameter = New ReportParameter("picName", imageFullPath)
  59.             Dim picturePath As ReportParameter = New ReportParameter("picPath", New Uri(imageFullPath).AbsoluteUri)
  60.             With ReportViewer1
  61.                 .LocalReport.EnableExternalImages = True
  62.                 .LocalReport.SetParameters(New ReportParameter() {pictureName, picturePath})
  63.                 .RefreshReport()
  64.             End With
  65.         Catch ex As Exception
  66.             MessageBox.Show(ex.Message)
  67.         End Try
  68.     End Sub

  69.     ' / --------------------------------------------------------------------------------
  70.     '// Copy image to clipboard.
  71.     ' / --------------------------------------------------------------------------------
  72.     Private Sub btnCopyClipboard_Click(sender As System.Object, e As System.EventArgs) Handles btnCopyClipboard.Click
  73.         If picBarcode.Image Is Nothing Then
  74.             MessageBox.Show("There is no QR Code.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  75.             Return
  76.         End If
  77.         '/ Add it as an image
  78.         Clipboard.SetImage(picBarcode.Image)
  79.         '/ Create a PNG on disk and add the location to the clipboard.
  80.         Dim TempName As String = "TempName.jpg"
  81.         Dim TempPath As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, TempName)
  82.         Using FS As New System.IO.FileStream(TempPath, IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.Read)
  83.             picBarcode.Image.Save(FS, System.Drawing.Imaging.ImageFormat.Png)
  84.         End Using
  85.         Dim Paths As New System.Collections.Specialized.StringCollection()
  86.         Paths.Add(TempPath)
  87.         Clipboard.SetFileDropList(Paths)
  88.     End Sub

  89.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  90.         Me.Close()
  91.     End Sub

  92.     Private Sub frmZXingReportViewer_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  93.         Me.Dispose()
  94.         GC.SuppressFinalize(Me)
  95.         Application.Exit()
  96.     End Sub

  97. #Region "FUNCTION"
  98.     ' / --------------------------------------------------------------------------------
  99.     ' / Get my project path
  100.     ' / AppPath = C:\My Project\bin\debug
  101.     ' / Replace "\bin\debug" with ""
  102.     ' / Return : C:\My Project\
  103.     Function MyPath(AppPath As String) As String
  104.         '/ Return Value
  105.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  106.         '// If not found folder then put the \ (BackSlash) at the end.
  107.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  108.     End Function

  109. #End Region
  110. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...










ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-30 04:56 , Processed in 0.170498 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้