-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageStreamProvider.vb
More file actions
60 lines (49 loc) · 1.55 KB
/
ImageStreamProvider.vb
File metadata and controls
60 lines (49 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Imports System.IO
Imports System.Data
Imports DevExpress.Office.Services
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Namespace WordProcessingMailMerge
Public Class ImageStreamProvider
Implements IUriStreamProvider
Private Shared ReadOnly prefix As String = "dbimg://"
Private table As DataTable
Private columnName As String
Public Sub New(ByVal sourceTable As DataTable, ByVal imageColumn As String)
Me.table = sourceTable
Me.columnName = imageColumn
End Sub
Public Function GetStream(ByVal uri As String) As Stream Implements IUriStreamProvider.GetStream
' Parse the retrieved URI string
uri = uri.Trim()
If Not uri.StartsWith(prefix) Then
Return Nothing
End If
' Remove the prefix from the retrieved URI string
Dim strId As String = uri.Substring(prefix.Length).Trim()
Dim id As Integer = Nothing
' Check if the string contains the primary key
If Not Integer.TryParse(strId, id) Then
Return Nothing
End If
' Retrieve the row that corresponds
' with the key
Dim row As DataRow = table.Rows.Find(id)
If row Is Nothing Then
Return Nothing
End If
' Convert the image string from this row
' to a byte array
Dim bytes() As Byte = TryCast(Convert.FromBase64String(TryCast(row(columnName), String)), Byte())
If bytes Is Nothing Then
Return Nothing
End If
' Return the MemoryStream with an image
Dim memoryStream As New MemoryStream(bytes)
Return memoryStream
End Function
End Class
End Namespace