Click here to Skip to main content
15,906,335 members
Articles / Programming Languages / Visual Basic

Pocket PC: Get image from Web Service using VB.NET

Rate me:
Please Sign up or sign in to vote.
3.07/5 (9 votes)
20 Apr 2005CPOL 105.6K   30   29
Allows a Web Service to send a picture from SQL Server to the Pocket PC.

Introduction

If you ever wanted your Pocket PC to show images from your SQL Server database, this is what you want. You don't have to store the pictures on your PPC. Using a Web Service you can now send the picture through the Web Service to your PPC.

Here's the code

First in your Web Service, add this Public method:

VB
<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

Make sure it is compiled and the function returns a BLOB of letters and numbers.

On your Pocket PC (VB.NET), just add a form with a PictureBox named PictureBox1 and your picture is now there.

VB
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Line of code missing ? Pin
cehrnow26-Apr-05 6:49
susscehrnow26-Apr-05 6:49 
GeneralRe: Line of code missing ? Pin
VbGuru61326-Apr-05 6:52
VbGuru61326-Apr-05 6:52 
GeneralRe: Line of code missing ? Pin
cehrnow26-Apr-05 6:57
susscehrnow26-Apr-05 6:57 
GeneralRe: Line of code missing ? Pin
VbGuru61326-Apr-05 6:59
VbGuru61326-Apr-05 6:59 
GeneralRe: Line of code missing ? Pin
VbGuru61326-Apr-05 7:00
VbGuru61326-Apr-05 7:00 
GeneralRe: Image Format Pin
cehrnow26-Apr-05 7:12
susscehrnow26-Apr-05 7:12 
GeneralRe: Image Format Pin
VbGuru61326-Apr-05 7:14
VbGuru61326-Apr-05 7:14 
GeneralRe: Line of code missing ? Pin
VbGuru61326-Apr-05 7:07
VbGuru61326-Apr-05 7:07 
Set strStream = New ADODB.Stream

strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile sFileName
RS.Fields("Picture").Value = strStream.Read

This is what i used to get the picture in the Database. It has to be Binary!!!!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.