How to dynamically load images in a crystal report (image file version and database version)

Source: Internet
Author: User
Step 1: database file version if you use binary fields in the database to store image files, these fields in Microsoft Access are called OLE fields, while Oracle and SQLServer call them BLOB (Binary large objects) it is very easy to display fields in the report, because I have not done any similar reports for displaying image files, etc.

Step 1: database file version if you use binary fields in the database to store image files, these fields in Microsoft Access are called OLE fields, while Oracle and SQL Server call them BLOB (Binary large objects) it is very easy to display fields in the report, because I have not done any similar reports for displaying image files, etc.

Step 1: database file version

If you use binary fields in the database to store image files
These fields in Microsoft Access are called OLE fields, while Oracle and SQL Server call them BLOB (Binary Large Object) fields.
It is very easy to display the report.
Because I have not made reports similar to that for displaying image files before, and I will be happy after I make them.
In fact, for this binary form, it is the same as a common field. just drag it to the template.

Setp2: Let's use the program to load images on the disk.

In fact, with the last practice of "display of arbitrary selection of specified fields in the Crystal Report", this idea is very clear.
It is to first create a report that is consistent with the database version. Of course, it is a friendly way to use Ado. Net.
Construct a DataSet by yourself and read the local image as a binary stream.
Then, push the DataSet to the report.


I used the "Employee table" in the sample database of the crystal report"

Let's take a look at the structure. We mainly look at the format of each field, because we perform the corresponding

Pay special attention to the format of the image field. In VB. Net, we can use Byte to echo it.

The template file is as follows, nothing special


The Code is as follows, with detailed comments.

1 '************************************** ***********************************
2 '** Module name: CR_DynLoadPics
3'
4' ** Creator: Babyt (Atay) http://www.cnblogs.com/babyt
5' ** Date:
6' ** modifier:
7' ** Date:
8 '** Description: obtains image files from a local disk for display.
9' this program is a prototype program without complete protection
10' ** version: V1.0.0
11 '************************************** ***********************************
12
13 Imports System. Data
14' note that I/O is added here to read image files.
15 Imports System. IO
16 PublicClass Form1Class Form1
17 Inherits System. Windows. Forms. Form
18
19 Code generated by Windows Form Designer # Region "code generated by Windows Form Designer"
20
21 Public Sub New () SubNew ()
22MyBase. New ()
23
24' this call is required by the Windows Forms designer.
25 InitializeComponent ()
26
27 'add any initialization after InitializeComponent () is called
28
29End Sub
30
31 'form override dispose to clear the component list.
32 Protected Overloads Overrides Sub Dispose () Sub Dispose (ByVal disposingAs Boolean)
33If disposing Then
34If Not (componentsIs Nothing) Then
35 components. Dispose ()
36End If
37End If
38MyBase. Dispose (disposing)
39End Sub
40
41 'windows forms designer required
42 Private components As System. ComponentModel. IContainer
43
44' Note: The following process is required by the Windows Forms designer
45' you can use the Windows Form Designer to modify this process.
46' do not use the code editor to modify it.
47 Friend WithEvents Button1As System. Windows. Forms. Button
48 Friend WithEvents TextBox1As System. Windows. Forms. TextBox
49 Friend WithEvents CrystalReportViewer1As CrystalDecisions. Windows. Forms. CrystalReportViewer
50 Friend WithEvents Label1As System. Windows. Forms. Label
51 PrivateSub InitializeComponent () Sub InitializeComponent ()
52Me. Button1 = New System. Windows. Forms. Button
53Me. TextBox1 = New System. Windows. Forms. TextBox
54Me. CrystalReportViewer1 = New CrystalDecisions. Windows. Forms. CrystalReportViewer
55Me. Label1 = New System. Windows. Forms. Label
56Me. SuspendLayout ()
57'
58 'button1
59'
60Me. Button1.Location = New System. Drawing. Point (520, 40)
61Me. Button1.Name = "Button1"
62Me. Button1.TabIndex = 0
63Me. Button1.Text = "Generate Report"
64'
65 'textbox1
66'
67Me. TextBox1.Location = New System. Drawing. Point (40, 40)
68Me. TextBox1.Name = "TextBox1"
69Me. TextBox1.Size = New System. Drawing. Size (392, 21)
70Me. TextBox1.TabIndex = 1
71Me. TextBox1.Text = "E: \ myNet \ CR_DynLoadPics \ Pics"
72'
73 'crystalreportviewer1
74'
75Me. CrystalReportViewer1.ActiveViewIndex =-1
76Me. CrystalReportViewer1.AutoScroll = True
77Me. CrystalReportViewer1.DisplayBackgroundEdge = False
78Me. CrystalReportViewer1.DisplayGroupTree = False
79Me. CrystalReportViewer1.Location = New System. Drawing. Point (24, 80)
80Me. CrystalReportViewer1.Name = "CrystalReportViewer1"
81Me. CrystalReportViewer1.ReportSource = Nothing
82Me. CrystalReportViewer1.Size = New System. Drawing. Size (616,320)
83Me. CrystalReportViewer1.TabIndex = 2
84'
85 'label1
86'
87Me. Label1.Location = New System. Drawing. Point (40, 8)
88Me. Label1.Name = "Label1"
89Me. Label1.Size = New System. Drawing. Size (432, 23)
90Me. Label1.TabIndex = 3
91Me. Label1.Text = "Please enter the directory where the image file is located correctly. \ is not included at the end. Verification is not performed here! "
92'
93 'form1
94'
95Me. AutoScaleBaseSize = New System. Drawing. Size (6, 14)
96Me. ClientSize = New System. Drawing. Size (664,429)
97Me. Controls. Add (Me. Label1)
98Me. Controls. Add (Me. CrystalReportViewer1)
99Me. Controls. Add (Me. TextBox1)
100Me. Controls. Add (Me. Button1)
101Me. Name = "Form1"
102Me. Text = "Form1"
103Me. ResumeLayout (False)
104
105End Sub
106
107 # End Region
108
109 Private Sub button#click () Sub button#click (ByVal senderAs System. Object, ByVal e As System. EventArgs) Handles Button1.Click
110 'view the definition of CrystalReport1 to view the corresponding report class
111Dim crReportDocument As New CrystalReport1
112Dim myDataSet As New DataSet
113Dim PicPath As String
114Dim myColume As System. Data. DataColumn
115Dim myRow As DataRow
116
117 '---------------------------------------------------------------------
118 'construct a table, corresponding to the xsd File
119 myDataSet. Tables. Add ("employee ")
120 'to construct the table structure. Only some fields can be constructed.
121 myDataSet. Tables (0). Columns. Add ("last name", System. Type. GetType ("System. String "))
122 myDataSet. Tables (0). Columns. Add ("name", System. Type. GetType ("System. String "))
123 myDataSet. Tables (0). Columns. Add ("employee photo", System. Type. GetType ("System. Byte []")
124
125 '---------------------------------------------------------------------
126 'write data
127 'Note that verification is not performed here
128 PicPath = TextBox1.Text &"\"
129 'write data rows (three image formats are used respectively)
130 'prompt:
131 'Here you can use the file path saved in your database
132 'Use your DataSet to fill the DataSet that will be pushed to the report
133 ', for example:
134 AddOneRow (myDataSet. Tables (0), "Babyt", "JPG file", PicPath & "BBT_042105_04.jpg ")
135 AddOneRow (myDataSet. Tables (0), "FaceSun", "Gif file", PicPath & "Image49.gif ")
136 AddOneRow (myDataSet. Tables (0), "Zhang", "Gif file", PicPath & "Image5.gif ")
137 AddOneRow (myDataSet. Tables (0), "King", "JPG file", PicPath & "1.jpg ")
138 AddOneRow (myDataSet. Tables (0), "Swallow", "JPG file", PicPath & "2.jpg ")
139 AddOneRow (myDataSet. Tables (0), "Cnblogs.com/babyt", "BMP file", PicPath & "Coup30.bmp ")
140
141 'push this DataSet to the report
142 crReportDocument. SetDataSource (myDataSet)
143
144 ''to pass the Report to the browser
145 CrystalReportViewer1.ReportSource = crReportDocument
146End Sub
147 '************************************** ***********************************
148 '** letter count: AddOneRow
149 '** input:
150 'byref tbl As DataTable: The table to be operated on. Note that it is ByRef.
151 'byval c1 As String the value of the first field
152 'byval c1 As String the value of the Second Field
153 'byval c1 As String the value of the third field. Note that the complete image file name is passed in.
154 '** output: None
155 '** Function Description: Add a data row to the able
156 'This process mainly encapsulates the extraction of local files based on the file name and writes them to DataSet.
157 '** global variables:
158 '** call module:
159 '** OPERATOR: Babyt (atai)
160 '** Date:
161 '** modifier:
162 '** Date:
Version 163 '**: V2.0.0
164 '************************************** ***********************************
165 Public Sub AddOneRow () Sub AddOneRow (ByRef tblAs DataTable, ByVal c1 As String, ByVal c2 As String, ByVal c3 As String)
166Dim fs AsNew FileStream (c3, FileMode. Open) 'gets the text stream
167Dim br AsNew BinaryReader (fs) 'create Binary Reader
168Dim row As DataRow
169
170 create a new line
171

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.