如何在窗体、报表或数据访问页中显示文件夹中的图像

原始 KB 编号: 285820

备注

需要专家编码、互操作性和多用户技能。 本文适用于 Microsoft Access 数据库 (.mdb/.accdb) 和 Microsoft Access 项目 (.adp) 。

摘要

有时,将图像存储在 Microsoft Access 表中是不切实际的。 如果你有许多图像,或者每个图像文件都很大,Microsoft Access 数据库文件的大小可能会迅速增加。

本文演示可用于:

  • 将文件路径和图像名称存储在表中。
  • 使用图像控件显示图像。
  • 如果没有可用的图像,则隐藏图像控件。
  • 提供有关图像的显示状态的反馈。

本文还包含示例 Visual Basic 脚本,可用于在数据访问页中显示图像。

备注

尽管此示例使用位图图像 (.bmp) ,但也可以使用其他图像类型,例如 .jpg、.pcx 和 .gif。

Microsoft 提供的编程示例仅用于进行说明,而不提供明示或默示担保。 这包括但不限于适销性或对特定用途的适用性的默示担保。 本文假设您熟悉正在演示的编程语言和用于创建和调试过程的工具。 Microsoft 支持工程师可以帮助解释特定过程的功能,但他们不会修改这些示例以提供新增功能或构建步骤以满足你的特定需要。

创建表以存储文件和路径数据

  1. 打开示例数据库、Northwind.mdb或示例项目 NorthwindCS.adp。

  2. 在 Northwind.mdb 或 NorthwindCS.adp 中创建下表。

    在 Northwind.mdb:

    控制台
    Table: tblImage
    ----------------------------
    Field Name: ImageID
    Data Type: AutoNumber
    Indexed: Yes (No Duplicates)
    
    Field Name: txtImageName
    Data Type: Text
    
    Table Properties: tblImage
    --------------------------
    PrimaryKey: ImageID
    

    在 NorthwindCS.adp 中:

    控制台
    Table: tblImage
    -----------------------
    Column Name: ImageID
    Datatype: Int
    Allow Nulls: Unchecked
    Identity: Yes
    
    Column Name: txtImageName
    Datatype: varchar
    
    Table Properties: ImageTable
    -------------------------------
    Primary Key Constraint: ImageID
    
  3. 在数据表视图中打开 tblImage 表,然后将位图文件的路径和名称添加到每个记录。 以下示例表显示了记录的外观:

    类型 示例
    绝对 (本地) C:\Windows\Zapotec.bmp
    绝对 (UNC 路径) \\Servername\sharename\Zapotec.bmp
    Relative Zapotec.bmp

创建自定义函数

  1. 创建新模块,然后粘贴或键入以下代码:

    VB
    Option Compare Database
    Option Explicit
    
    Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
    On Error GoTo Err_DisplayImage
    
    Dim strResult As String
    Dim strDatabasePath As String
    Dim intSlashLocation As Integer
    
    With ctlImageControl
     If IsNull(strImagePath) Then
         .Visible = False
         strResult = "No image name specified."
     Else
         If InStr(1, strImagePath, "\") = 0 Then
             ' Path is relative
             strDatabasePath = CurrentProject.FullName
             intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
             strDatabasePath = Left(strDatabasePath, intSlashLocation)
             strImagePath = strDatabasePath & strImagePath
         End If
         .Visible = True
         .Picture = strImagePath
         strResult = "Image found and displayed."
     End If
    End With
    
    Exit_DisplayImage:
     DisplayImage = strResult
     Exit Function
    
    Err_DisplayImage:
     Select Case Err.Number
         Case 2220       ' Can't find the picture.
             ctlImageControl.Visible = False
             strResult = "Can't find image in the specified name."
             Resume Exit_DisplayImage:
         Case Else       ' Some other error.
             MsgBox Err.Number & " " & Err.Description
             strResult = "An error occurred displaying image."
             Resume Exit_DisplayImage:
     End Select
    End Function
    
  2. 将模块另存为 Module1。

在窗体中使用自定义函数

  1. 创建基于 tblImage 表的以下新窗体。

    控制台
    Form: frmImage
    ----------------------
    Caption: Image Form
    RecordSource: tblImage
    
    Image Control
    ---------------------------------
    Name: ImageFrame
    Picture: "C:\Windows\Zapotec.bmp"
    
    Text box
    ----------------------
    Name: txtImageID
    ControlSource: ImageID
    
    Text box
    ---------------------------
    Name: txtImageName
    ControlSource: txtImageName
    
    Text box
    ---------------------------
    Name: txtImageNote
    ControlSource: <Blank>
    

    备注

    如果不希望路径显示在窗体中,可以将 控件的 txtImageName 属性设置为 VisibleFalse

  2. 在“ 视图 ”菜单上,单击“ 代码”,然后粘贴或键入以下代码:

    VB
    Option Compare Database
    Option Explicit
    
    Private Sub Form_AfterUpdate()
     CallDisplayImage
    End Sub
    
    Private Sub Form_Current()
     CallDisplayImage
    End Sub
    
    Private Sub txtImageName_AfterUpdate()
     CallDisplayImage
    End Sub
    
    Private Sub CallDisplayImage()
     Me!txtImageNote = DisplayImage(Me!ImageFrame, Me!txtImageName)
    End Sub
    
  3. 在窗体视图中打开 frmImage 窗体。 请注意,窗体显示每个记录的相应位图。 txtImageName如果字段为空或找不到图像,则会收到相应的消息,而不是图像帧。

在报表中使用自定义函数

  1. 创建基于 ImageTable 表的以下新报表。

    控制台
    Report: rptImage
    ----------------------
    Caption: Image Report
    RecordSource: tblImage
    
    Image Control
    ---------------------------------
    Name: ImageFrame
    Picture: "C:\Windows\Zapotec.bmp"
    
    Text box
    ----------------------
    Name: txtImageID
    ControlSource: ImageID
    
    Text box
    ---------------------------
    Name: txtImageName
    ControlSource: txtImageName
    
    Text box
    ---------------------------
    Name: txtImageNote
    ControlSource: <Blank>
    

    备注

    如果不希望路径显示在报表中,可以将 控件的 txtImageName 属性设置为 VisibleFalse

  2. 在“ 视图 ”菜单上,单击“ 代码”,然后粘贴或键入以下代码:

    VB
    Option Compare Database
    Option Explicit
    
    Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
     Me!txtImageNote = DisplayImage(Me!ImageFrame, Me!txtImageName)
    End Sub
    
  3. 在打印预览中打开 rptImage 报表。 请注意,报表显示每个记录的相应位图。 txtImageName如果字段为空或找不到图像,则会收到相应的消息,而不是图像帧。

复制数据访问页中的自定义函数

  1. 创建以下基于 tblImage 表的新数据访问页。

    控制台
    Data Access Page: dapImage
    -----------------------------
    Title: Image Data Access Page
    
    Image Control
    ---------------------------------
    ID: ImageFrame
    
    Text box
    ----------------------
    ID: txtImageID
    ControlSource: ImageID
    
    Text box
    ---------------------------
    ID: txtImageName
    ControlSource: txtImageName
    

    备注

    如果不希望路径显示在页面中,可以将 控件的 txtImageName 属性设置为 VisibilityHidden

  2. 在“工具”菜单上,指向“”,然后单击“Microsoft 脚本编辑器”。

  3. 将以下脚本添加到 HTML 文档的 HEAD 标记部分中 MSODSC 的 Current 事件。

    备注

    必须传入 参数才能触发事件。

    vbs
    <SCRIPT language=vbscript event=Current(oEventInfo) for=MSODSC>
    <!--
    ImageFrame.src=txtImageName.value
    -->
    </SCRIPT>
    
  4. 在页面视图中打开 dapImage 页。 请注意,页面显示每个记录的相应位图。 如果 txtImageName 字段为空,则显示控件图标。 如果找不到图像,图像控件中将显示 X 图标。

在窗体中使用 http:// 路径

若要在窗体中使用 http:// 路径,请使用 Web 浏览器控件 (shdocvw.dll) ,如下所示:

  1. 将 Microsoft Web Browser 控件添加到窗体,并将其命名为 WebBrowser。

  2. 将以下代码添加到模块:

    VB
    Public Function DisplayImageWeb(ctlBrowserControl As Control, _
    strImagePath As Variant)
    
    On Error GoTo Err_DisplayImage
    
    Dim strDatabasePath As String
    Dim intSlashLocation As Integer
    
    With ctlBrowserControl
     If IsNull(strImagePath) Then
     ElseIf Left(strImagePath, 4) = "http" Then
         .Navigate (strImagePath)
     Else
         If InStr(1, strImagePath, "\") = 0 Then
             ' Path is relative
             strDatabasePath = CurrentProject.FullName
             intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
             strDatabasePath = Left(strDatabasePath, intSlashLocation)
             strImagePath = strDatabasePath & strImagePath
         End If
         .Navigate (strImagePath)
     End If
    End With
    
    Exit_DisplayImage:
     Exit Function
    
    Err_DisplayImage:
     Select Case Err.Number
         Case Else
             MsgBox Err.Number & " " & Err.Description
             Resume Exit_DisplayImage:
     End Select
    End Function
    
  3. 在窗体后面添加以下代码:

    VB
    Option Compare Database
    Option Explicit
    
    Private Sub Form_AfterUpdate()
     CallDisplayImage
    End Sub
    
    Private Sub Form_Current()
     CallDisplayImage
    End Sub
    
    Private Sub txtImageName_AfterUpdate()
     CallDisplayImage
    End Sub
    
    Private Sub CallDisplayImage()
     DisplayImageWeb Me.WebBrowser9, Me.txtImageName
    End Sub