About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Tuesday, December 15, 2009

Converting XPS to Bitmap

One thing I had to do recently, for a SSRS report, was attach a 3 page pdf to a notice that is mailed out. Since, I can't just attach a pdf directly to the report, I figured I'll just take the images.

So, first thing I did was print the pdf to a XPS file. Now I just need to take that XPS file and convert it to some type of usable image that SSRS can recognize.

Found a solution on one of the MSDN message boards; however, I had to make some updates to it:

Code Snippet
  1. static public void SaveXpsPageToBitmap(string xpsFileName)
  2. {
  3.     DirectoryInfo di = new DirectoryInfo(xpsFileName);
  4.     XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
  5.     FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
  6.  
  7.     //DocumentReferenceCollection drc = docSeq.References;
  8.     for (int i = 0; i < docSeq.DocumentPaginator.PageCount; i++)
  9.     {
  10.         DocumentPage docPage = docSeq.DocumentPaginator.GetPage(i);
  11.         BitmapImage bitmap = new BitmapImage();
  12.         RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)docPage.Size.Width, (int)docPage.Size.Height, 96, 96, PixelFormats.Default);
  13.     
  14.         renderTarget.Render(docPage.Visual);
  15.  
  16.         BitmapEncoder encoder = new BmpBitmapEncoder();
  17.         encoder.Frames.Add(BitmapFrame.Create(renderTarget));
  18.  
  19.         FileStream pageOutStream = new FileStream(di.FullName.Substring(0,di.FullName.Length - 4) + "_Page_" + (i+1).ToString() + ".bmp", FileMode.Create, FileAccess.Write);
  20.         encoder.Save(pageOutStream);
  21.         pageOutStream.Close();
  22.     }
  23. }




So this program works nicely, only problem I currently have is that I have to convert these large size bitmaps (~5MB per page) to something more reasonable. Especially, since the SSRS deployment gives me a SOAP error because of the size. I'll probably just end up converting them to Jpeg later on.

Tuesday, December 01, 2009

Creating an Psion Tecklogix Scan Gun App

In 4 simple and easy to follow steps:

Step 1: Getting Psion Resources

You will have to register to get access to the page.
(https://teknet.psionteklogix.com/ptxCMS/Teknet.aspx?s=us&p=DevKits)

Download and install their "Mobile Devices SDK"


Step 2: Create Project

If you go to the "Smart Device" section, you will notices a "Psion Teklogix Device Project". This add the Psion TeklogixNet reference and also the PtxSdkCommon.dll in the root directory, which is a dependency when running your app on device.
















Step 3: Add Items to the Toolbar, if needed

Right-click on the Toolbar, and select Choose Items.
Browse to C:\Program Files\Psion Teklogix\Mobile Devices SDK V3.1\DotNet2 and select the PsionTeklogixNet.dll

This should add:




If not already done so, drag each of those items onto the form.

















The default app gives a good example, it uses the ScanCompleteEvent; which send in a string value representation of the barcode and places it text into a textbox for the user to see.








Step 4: Deploying
Code Snippet
  1. /* $Revision 1.1.1.1 */
  2. /* Copyright Psion Teklogix Inc. 2007 */
  3. /*
  4. * File: Form1.cs
  5. *
  6. * Description:
  7. * Implementation file for Form1 class in PtxApp1
  8. *
  9. */
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Drawing;
  16. using System.Runtime.InteropServices;
  17. using System.Text;
  18. using System.Windows.Forms;
  19. using PsionTeklogix.Barcode;
  20. using PsionTeklogix.Barcode.ScannerServices;
  21.  
  22. /*! <summary>
  23. Contains the class and functions related to PtxApp1 scanner application
  24. </summary>
  25. */
  26. namespace PtxApp1
  27. {
  28.     /*
  29.     * Form1
  30.     *
  31.     */
  32.     /// <summary>
  33.     /// The Form1 class generates the Graphical User Interface for PtxApp1.
  34.     /// </summary>
  35.     public partial class Form1 : Form
  36.     {
  37.         /*
  38.         * InitializeScanner
  39.         *
  40.         */
  41.         /// <summary>
  42.         /// Initialize components for PtxApp1.
  43.         /// </summary>
  44.         private void InitializeScanner()
  45.         {
  46.             scanner.Driver = scannerServicesDriver;
  47.             scanner.ScanCompleteEvent += new ScanCompleteEventHandler(scanner_ScanCompleteEvent);
  48.         }
  49.  
  50.         /*
  51.         * Form1
  52.         *
  53.         */
  54.         /// <summary>
  55.         /// Required for Windows Form designer support.
  56.         /// </summary>
  57.         public Form1()
  58.         {
  59.             try
  60.             {
  61.                 InitializeComponent();
  62.                 InitializeScanner();
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 MessageBox.Show("Failed to initialize component: " + ex.ToString());
  67.                 this.Close();
  68.             }
  69.         }
  70.  
  71.         /*
  72.         * Button1_Click
  73.         *
  74.         */
  75.         /// <summary>
  76.         /// This method is called when the scan button is clicked and will scan the
  77.         /// barcode.
  78.         /// </summary>b
  79.         /// <param name="sender">
  80.         /// The calling object that represents the user that sent the message.
  81.         /// </param>
  82.         /// <param name="e">
  83.         /// Contains event data of the Scan Button control
  84.         /// </param>
  85.         private void button1_Click(object sender, EventArgs e)
  86.         {
  87.             try
  88.             {
  89.                 scanner.Scan();
  90.             }
  91.             catch (Exception ex)
  92.             {
  93.                 MessageBox.Show("Scan error: " + ex.ToString());
  94.                 this.Close();
  95.             }
  96.         }
  97.        
  98.         /*
  99.         * Scanner1_ScanCompleteEvent
  100.         *
  101.         */
  102.         /// <summary>
  103.         /// This method is called when the scan complete event occurs. The method is
  104.         /// called by its respective handler and then displays a text representation
  105.         /// of the barcode on the display.
  106.         /// </summary>
  107.         /// <param name="sender">
  108.         /// The calling object that represents the user that sent the message.
  109.         /// </param>
  110.         /// <param name="e">
  111.         /// Contains event data once the scan is complete
  112.         /// </param>
  113.         delegate void scanner_ScanCompleteDelegate(object sender, ScanCompleteEventArgs e);
  114.  
  115.         private void scanner_ScanCompleteEvent(object sender, ScanCompleteEventArgs e)
  116.         {
  117.             if (!InvokeRequired)
  118.             {
  119.                 textBox1.Text = e.Text;
  120.             }
  121.             else
  122.             {
  123.                 Invoke(new scanner_ScanCompleteDelegate(scanner_ScanCompleteEvent),
  124.                 new object[] { sender, e });
  125.             }
  126.         }
  127.     }
  128. }
When deploying, remember to set the device to PtxPxa27c: ARMV4I_Release


P.S.
The size of the form shouldn't be bigger than 245 x 300