📄 form1.vb
字号:
Imports System.io
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.CrystalReportViewer1 = New CrystalDecisions.Windows.Forms.CrystalReportViewer
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(32, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'CrystalReportViewer1
'
Me.CrystalReportViewer1.ActiveViewIndex = -1
Me.CrystalReportViewer1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.CrystalReportViewer1.Location = New System.Drawing.Point(8, 40)
Me.CrystalReportViewer1.Name = "CrystalReportViewer1"
Me.CrystalReportViewer1.ReportSource = Nothing
Me.CrystalReportViewer1.Size = New System.Drawing.Size(680, 480)
Me.CrystalReportViewer1.TabIndex = 1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(704, 533)
Me.Controls.Add(Me.CrystalReportViewer1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
' here i have define a simple datatable inwhich image will recide
Dim dt As New DataTable
' object of data row
Dim drow As DataRow
' add the column in table to store the image of Byte array type
dt.Columns.Add("Image", System.Type.GetType("System.Byte[]"))
drow = dt.NewRow
' define the filestream object to read the image
Dim fs As FileStream
' define te binary reader to read the bytes of image
Dim br As BinaryReader
' check the existance of image
If File.Exists(AppDomain.CurrentDomain.BaseDirectory & "10157.Jpg") Then
' open image in file stream
fs = New FileStream(AppDomain.CurrentDomain.BaseDirectory & "10157.Jpg", FileMode.Open)
Else ' if phot does not exist show the nophoto.jpg file
fs = New FileStream(AppDomain.CurrentDomain.BaseDirectory & "NoPhoto.jpg", FileMode.Open)
End If
' initialise the binary reader from file streamobject
br = New BinaryReader(fs)
' define the byte array of filelength
Dim imgbyte(fs.Length) As Byte
' read the bytes from the binary reader
imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)))
drow(0) = imgbyte ' add the image in bytearray
dt.Rows.Add(drow) ' add row into the datatable
br.Close() ' close the binary reader
fs.Close() ' close the file stream
Dim rptobj As New CrystalReport1 ' object of crystal report
rptobj.SetDataSource(dt) ' set the datasource of crystalreport object
CrystalReportViewer1.ReportSource = rptobj 'set the report source
Catch ex As Exception
' error handling
MsgBox("Missing 10157.jpg or nophoto.jpg in application folder")
End Try
''' run the application to view image in report
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -