txtStatus.Text = 'Signature saved!';

 writer.Close();

 isoStream.Close();

}

Define the DrawSignature() subroutine so that the signature can be reproduced from a string representing a collection of lines:

//---draws the signature---

private void DrawSignature(string value) {

 _lines = new List<List<Point>>();

 //---split into individual lines---

 string[] lines = value.Split(' ');

 //---for each individual line---

 for (int i = 0; i <= lines.Length - 2; i++) {

  //---split into individual points---

  string[] ps = lines[i].Split('|');

  _points = new List<Point>();

  //---for each point---

  for (int j = 0; j <= ps.Length - 2; j++) {

   string[] xy = ps[j].Split(',');

   _points.Add(new Point(

    (Convert.ToDouble(xy[0])),

    Convert.ToDouble(xy[1])));

  }

  _lines.Add(_points);

 }

 //---draws the signature---

 for (int line = 0; line <= _lines.Count - 1; line++) {

  _points = (List<Point>)_lines[line];

  for (int i = 1; i <= _points.Count - 1; i++) {

   Line sline = new Line() {

    X1 = _points[i - 1].X,

    Y1 = _points[i - 1].Y,

    X2 = _points[i].X,

    Y2 = _points[i].Y,

    StrokeThickness = 2,

    Stroke = new SolidColorBrush(Colors.Black)

   };

   SigPad.Children.Add(sline);

  }

 }

}

Code the MouseLeftButtonDown event handler for the Load button so that the series of signature lines can be loaded from isolated storage:

//---Load button---

void btnLoad_MouseLeftButtonDown(

 object sender, MouseButtonEventArgs e) {

 IsolatedStorageFile isoStore =

  IsolatedStorageFile.GetUserStoreForApplication();

 IsolatedStorageFileStream isoStream =

  new IsolatedStorageFileStream('IsoStoreFile.txt',

  FileMode.Open, isoStore);

 StreamReader reader = new StreamReader(isoStream);

 //---read all lines from the file---

 string lines = reader.ReadToEnd();

 //---draws the signature---

 DrawSignature(lines);

 txtStatus.Text = 'Signature loaded!';

 reader.Close();

 isoStream.Close();

}

Code the MouseLeftButtonDown event handler for the Clear button so that the signature can be cleared from the drawing pad:

//---Clear button---

void btnClear_MouseLeftButtonDown(

 object sender, MouseButtonEventArgs e) {

 _lines = new List<List<Point>>();

 _points = new List<Point>();

 //---iteratively clear all the signature lines---

 int totalChild = SigPad.Children.Count - 2;

 for (int i = 0; i <= totalChild; i++) {

  SigPad.Children.RemoveAt(1);

 }

 txtStatus.Text = 'Signature cleared!';

}

Press F5 to test the application. You can now sign and then save the signature. You can also load the saved signature (see Figure 19-72).

Figure 19-72

Saving the Signature to Web Services

One of these signatures isn't a lot of good unless you can send it to a Web Service. This section shows you how to do that.

Using the same project created in the previous section, add a new Web Site project to the current solution (see Figure 19-73).

Figure 19-73

Select ASP.NET Web Site, and name the project SignatureWebSite.

Add a new Web Service item to the Web Site project, and use its default name of WebService.asmx (see Figure 19-74).

Figure 19-74

In the WebService.cs file, add the following lines:

using System;

using System.Collections;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

using System.IO;

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату