65.9K
CodeProject 正在变化。 阅读更多。
Home

Pocket PC:使用 VB.NET 从 Web Service 获取图像

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.07/5 (9投票s)

2005年4月20日

CPOL
viewsIcon

107363

允许 Web Service 将 SQL Server 中的图片发送到 Pocket PC。

引言

如果您希望您的 Pocket PC 显示来自 SQL Server 数据库的图像,那么这就是您需要的。您无需将图片存储在 PPC 上。使用 Web Service,您可以将图片通过 Web Service 发送到您的 PPC。

这是代码

首先在您的 Web Service 中,添加此 Public 方法

<WebMethod()>
Public Function GetPicture(ByVal RowID As Long) As Byte()
    Dim Con As SqlClient.SqlConnection
    Dim DA As SqlClient.SqlDataAdapter
    Dim SQL As String
    Dim BA As Byte()
    Dim SC As New SqlCommand
    SQL = "SELECT Picture FROM Pictures WHERE  RowID = " & RowID 
    Con = New SqlConnection("User ID=YourID;password=YourPassword;" & _
                 "Data Source=SQLSERVER;Initial Catalog=DatabaseName")
    SC.Connection = Con
    SC.Connection.Open()
    SC.CommandType = CommandType.Text
    SC.CommandText = SQL
    BA = CType(SC.ExecuteScalar(), Byte())
    SC.Connection.Close()
    SC.Dispose()
    Return BA
End Function

确保已编译,并且该函数返回一个由字母和数字组成的 BLOB

在您的 Pocket PC (VB.NET) 上,只需添加一个包含名为 PictureBox1PictureBox 的窗体,您的图片现在就在那里了。

Public Sub SetPicBox(ByVal ImageArray As Byte())
    Dim ArraySize As New Integer
    ArraySize = ImageArray.GetUpperBound(0)
    Dim fs As New System.IO.FileStream("tmp.gif", 
        System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
    ' don't ask me why, but I had to add 1 here for this to work
    fs.Write(ImageArray, 0, ArraySize + 1) 
    fs.Close()
    PictureBox1.Image = New Bitmap("tmp.gif")
End Sub
Private Sub frmPicture_Load(ByVal sender As Object, 
           ByVal e As System.EventArgs) Handles MyBase.Load
    SetPicBox(YourService.GetPicture(23))
End Sub
© . All rights reserved.