Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Picture SMS Using the .NET Compact Framework 2.0 : Page 3

Got a client that's asking to send messages via SMS? If so, you know there's no easy way to do it without third-party software. Learn how to build your own image-sending SMS application using only the .NET Compact Framework.


advertisement
Sending Messages
First write the code to send messages. Double-click on the Select File button and code the following so that users can select a file to attach to the SMS (see Listing 2).

When a file has been selected, the ConvertFileContentToBase64() function is called so that it can convert the file content into base64 encoding. It is then compressed to potentially reduce its size and encoded back into base64 encoding again. A check is then made to see if there is a reduction in file size. If there is, the compressed base64 image is used, else the original base64 encoding of the image data is used instead.

The ConvertFileContentToBase64() function is defined as in Listing 3, and the DisplayStatus() subroutine is defined as follows:



Public Sub DisplayStatus(ByVal str As String) txtStatus.Text = str & vbCrLf & txtStatus.Text Application.DoEvents() End Sub

Next, double-click on the Send SMS menu item and code it like in Listing 4.

Here, you try to compress the text (if any) and see if there is a reduction in data size. As usual, if there is no reduction in size, you will use the original uncompressed text. Else, use the compressed text. After that, prompt the user to confirm sending the message. The message containing the text and image will then be sent.

Receiving Messages
When a picture SMS message is received, you need to intercept it and take the appropriate action to decode its content.

Code the following in the Form1_Load event:

Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load msgInterceptor = _ New MessageInterceptor( _ InterceptionAction.NotifyAndDelete, _ True) '---set the filter for the message--- msgInterceptor.MessageCondition = _ New MessageCondition( _ MessageProperty.Body, _ MessagePropertyComparisonType.StartsWith, _ "<*>", True) '---set the event handler for the message interceptor--- AddHandler msgInterceptor.MessageReceived, _ AddressOf smsInterceptor_MessageReceived End Sub

Basically, you are intercepting all incoming SMS messages whose body begins with the <*> string. When messages are intercepted, the smsInterceptor_MessageReceived() subroutine is called, which is defined as shown in Listing 5.

When a message is intercepted, you first cast the message into a SmsMessage object so that you can retrieve the details of the message (such as the sender phone number, message content, etc). You then decode the body of the message and extract the text and image accordingly.

The two delegates and the two subroutines for displaying the text and image content are defined as follows:

Private Delegate Sub delDisplayText(ByVal str As String) Private Sub DisplayText(ByVal str As String) Try txtMessageReceived.Text = str Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Delegate Sub delDisplayImage(ByVal img As Byte()) Private Sub DisplayImage(ByVal img As Byte()) Try Dim ms As MemoryStream = New MemoryStream(img) PictureBox1.Image = New Bitmap(ms) Catch ex As Exception MsgBox(ex.ToString) End Try End Sub

The ConvertBase64ToByteArray() function converts a base64 string into a byte array:

Private Function ConvertBase64ToByteArray( _ ByVal base64string As String) As Byte() Dim binaryData() As Byte Try binaryData = _ System.Convert.FromBase64String(base64string) Catch ex As Exception Console.WriteLine("Error decoding content") Return (Nothing) End Try Return binaryData End Function



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap