The tools and technologies we used
Frame work: .Net 4.0
Technologies: WPF, WCF
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.
Uri uri = new Uri("Page_Name", UriKind.Relative);
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.
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.
collection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
collection will now have all the capture devices present in the device.
We could populate a drop down list using collection.
foreach (FilterInfo devices in collection)
Code to show the camera feed to an image control
final = new VideoCaptureDevice(collection[device].MonikerString);
final.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
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();
MemoryStream ms = new MemoryStream();
bi.StreamSource = ms;
//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
image1.Source = bi; // image1 is the image control in the WPF window.
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.
These libraries include functions to dynamically create a pdf file from scratch or edit an existing pdf file.
Initialization of pdf file object
string pdfFile = <filename>;
FileStream fos = new FileStream(pdfFile, FileMode.Create);
BufferedStream bos = new BufferedStream(fos);
PDF pdf = new PDF(bos);
Now create pages for the pdf file
PDFjet.NET.Page <page_name> = new PDFjet.NET.Page(pdf, A4.PORTRAIT);
page_name will be used to put data into the pdf file.
Insert data with PDFJET
Insert a text into pdf file
public static void putText(Font f, PDFjet.NET.Page page, string str, float x, float y, int color)
TextLine text = new TextLine(f, str);
Insert an image into pdf file
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);
Drawing with PDFJET
Draw a Line
public static void putLine(double wid, int x1, int y1, int x2, int y2, Page page)
PDFjet.NET.Line headline = new PDFjet.NET.Line();
Draw a Box
public static void putBox(double xmax, double ymax, double x, double y, int color, Page page)
Box colorBox = new Box();