The tools and technologies we used
Frame work: .Net 4.0
Technologies: WPF, WCF
Language: C#.Net
Database: SQL Compact Edition 4.0
Tools: Visual Studio 2012, SQL Server 2008 R2 Management Studio,Expressions Blend.
Operating System: Windows 8 Pro
Dll’s: DirectShow , PDFjet
Windows 8 operating system got two version, RT and Pro. Windows RT is a different version from the legacy operating system. These differences marks from the application development level till the user interface. So the legacy applications developed for Windows OS doesn’t work with Windows RT devices. It requires a different methodology for developing applications for Windows RT devices. Here we are going to build an app for Windows 8 Pro devices which will run both legacy and RT applications.
Advantages of WPF over Windows Forms
To built the interface we are using WPF (Windows Presentation Foundation), it has an added advantage over Windows Forms due to the availability of Pages. The interface of the application is controlled by the xaml file , the position, alignment, look and feel of the controls we used in the application can be edited by editing these xaml files. Xaml file structure is similar to xml files. Each window of the application contains 2 files, an xaml file and a code behind file. As we said before the xaml file controls the overall look and feel of the window and the code behind file is responsible for the user interaction between the window and the user.
Features of NavigationWindow
If we are building a survey application it will be far better to use Pages as in a Web Page than windows. It is because as a window is unique and it needs to be open and closed for its operations whereas in pages any number of pages can be collectively attached to a single window and the traversal between these pages can be done without closing another page technically. Also pages can remember there states also. So once we get back to a page we had already attempted the state of the page remains the same. The values we have entered in the textbox’s and values selected in dropdown list will remain there until we explicitly close the window which hosts there pages. The window we used to host the pages is a bit different from the normal window, these windows are known as NavigationWindow. It has a source attribute where we could give the address of the first page and it will display the page when the NavigationWindow is loaded. From there we could add pages back to back starting from the first page and it will traverse in the respective order. We use NavigationService to traverse between pages.
[php]
Uri uri = new Uri("Page_Name", UriKind.Relative);
NavigationService.Navigate(uri);
[/php]
Survey applications needs to store data locally, we use SQL Compact Edition 4.0 to create and equip our database. These .sdf files are copied along with the application to the target machines so that they could work anywhere without the overhead of machine dependant connection strings and all. To use compact edition we need to import the System.Data.SqlServerCe dll to the application’s references.
SqlCeConnection(): creates a new instance of database connection.
SqlCeCommand(): create a command object where we could assign sql queries.
SqlCeDataReader(): create a reader object which returns the result fetched form the database.
SqlCeExecuteNonQuery(), SqlCeExecuteReader(), SqlCeExecuteScalar() : Used to execute the queries stored in the command object as per the requirement.
Accessing the tablet camera is a bit tedious process since the SDK’s doesn’t ship with built in functions to access the device camera so we are using Direct Show to access the device camera.
Directshow can be downloaded from this link http://www.aforgenet.com/
Download the dll’s and add them to the current project. You also need to add them to the code file.
[php]
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
[/php]
Initially we need the information on the available capture devices attached to the device. To do so we need to initialize two objects from Aforge.
[php]
VideoCaptureDevice <final>;
FilterInfoCollection <collection>;
collection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
[/php]
collection will now have all the capture devices present in the device.
We could populate a drop down list using collection.
[php]
foreach (FilterInfo devices in collection)
{
comboBox1.Items.Add(devices.Name);
}
[/php]
Code to show the camera feed to an image control
[php]
final = new VideoCaptureDevice(collection[device].MonikerString);
final.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
final.Start();
void final_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
}
void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
System.Drawing.Image imgforms = (Bitmap)eventArgs.Frame.Clone();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
MemoryStream ms = new MemoryStream();
imgforms.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
ms.Seek(0, SeekOrigin.Begin);
bi.StreamSource = ms;
bi.EndInit();
bi.Freeze();
//Freezing the bitmapImage to avoid cross thread operation and then calling the UI thread using the Dispatcher to update the ‘Image’ WPF control called frameholder
Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
{
image1.Source = bi; // image1 is the image control in the WPF window.
}));
}
[/php]
Report Generation in PDF
Next we will move on to report generation phase. Every survey application requires a report to be generated in the end. No dynamic pdf making tools are available with WPF but a number of third party software’s are available for the same purpose.
http://pdfjet.com/
http://sharppdf.sourceforge.net/
http://itextpdf.com/
http://report.sourceforge.net/
http://sourceforge.net/projects/clown/
These libraries include functions to dynamically create a pdf file from scratch or edit an existing pdf file.
Initialization of pdf file object
[php]
string pdfFile = <filename>;
FileStream fos = new FileStream(pdfFile, FileMode.Create);
BufferedStream bos = new BufferedStream(fos);
PDF pdf = new PDF(bos);
[/php]
Now create pages for the pdf file
[php]
PDFjet.NET.Page <page_name> = new PDFjet.NET.Page(pdf, A4.PORTRAIT);
page_name will be used to put data into the pdf file.
[/php]
Insert data with PDFJET
Insert a text into pdf file
[php]
public static void putText(Font f, PDFjet.NET.Page page, string str, float x, float y, int color)
{
TextLine text = new TextLine(f, str);
text.SetPosition(x, y);
text.SetColor(color);
text.DrawOn(page);
}
[/php]
Insert an image into pdf file
[php]
public static void putImage(string filename, PDFjet.NET.Page page, PDF pdf, float x, float y)
{
FileStream fis2 = new FileStream(filename, FileMode.Open, FileAccess.Read);
PDFjet.NET.Image image2 = new PDFjet.NET.Image(pdf, fis2, ImageType.JPG);
image2.SetPosition(x, y);
image2.DrawOn(page);
}
[/php]
Drawing with PDFJET
Draw a Line
[php]
public static void putLine(double wid, int x1, int y1, int x2, int y2, Page page)
{
PDFjet.NET.Line headline = new PDFjet.NET.Line();
headline.SetWidth(wid);
headline.SetStartPoint(x1, y1);
headline.SetEndPoint(x2, y2);
headline.DrawOn(page);
}
[/php]
Draw a Box
[php]
public static void putBox(double xmax, double ymax, double x, double y, int color, Page page)
{
Box colorBox = new Box();
colorBox.SetSize(xmax, ymax);
colorBox.SetLineWidth(1);
colorBox.SetPosition(x, y);
colorBox.SetColor(color);
colorBox.SetFillShape(true);
colorBox.DrawOn(page);
}
[/php]