Mobile Line Of Business

Richard Jones (MVP)

  Home  |   Contact  |   Syndication    |   Login
  202 Posts | 0 Stories | 36 Comments | 0 Trackbacks

News

Welcome to the Mobile Line Of Business Blog

Tag Cloud


Article Categories

Archives

Post Categories

About

image

 

I’ve just been porting a Windows Mobile (.Net CF) application over to the full Net Framework.   I needed to provide a quick way of doing signature capture.

I was pointed by a friend (thanks Ross),  to look at hosting the WPF InkCanvas control on my Windows Forms, form.   I just dropped a Panel (Panel1) on my form and used the following code -

 System.Windows.Controls.InkCanvas inkbox = null;
        
        public frmpleasesign()
        {
            InitializeComponent();
 
            inkbox = new System.Windows.Controls.InkCanvas();
 
            inkbox.Name = "myinbox";
 
 
            ElementHost elementHost = new ElementHost();
 
            elementHost.Dock = DockStyle.None;
 
            elementHost.Width = panel1.Width;
 
            elementHost.Height = panel1.Height;
 
            elementHost.Child = inkbox;
 
            panel1.Controls.Add(elementHost);
 
        }

 

To save the signature out to a byte array, that can be saved to my database I used, the following code -

 

 public static byte[] SignatureToBitmapBytes(InkCanvas icSignature)
        {
 
            int margin = (int)icSignature.Margin.Left;
 
            int width = (int)icSignature.ActualWidth - margin;
 
            int height = (int)icSignature.ActualHeight - margin;
 
 
 
            //render ink to bitmap 
 
            RenderTargetBitmap rtb =
            new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
 
            rtb.Render(icSignature);
 
 
 
 
            BmpBitmapEncoder encoder = new BmpBitmapEncoder();
 
            encoder.Frames.Add(BitmapFrame.Create(rtb));
 
           byte[] bitmapBytes;
 
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
 
                ms.Position = 0;
               bitmapBytes = ms.ToArray(); 
            }
            return bitmapBytes;
        }
Technorati Tags: ,
posted on Tuesday, July 28, 2009 8:34 AM