Signature Converter

by Jeff Doolittle 23. December 2008 16:27

For a mobile application I'm working on, I needed a simple signature capture control. I found just such a control at the Code Project (http://www.codeproject.com/KB/mobile/SignatureCapture_PocketPC.aspx). The code will capture a signature and actually convert it into bitmap coordinates. This will allow me to store much less data rather than full images. But at some point I'm going to want to convert these bitmap coordinates back into an image. And unfortunately, the signature capture project only provides Java code for performing this process.

Well, if anyone else is interested, here is the Java code ported to C#. My next step will be to extend this class so that it isn't directly dependent on File System IO. But this should be enough to get anyone started who'd like to use the Signature Capture control from CodeProject, but wants to use .NET and C# to convert the bitmap coordinates back into an image.

using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Drawing; using System.Drawing.Imaging; namespace Agilification { public class SignatureConverter { const int baseX = 25; const int baseY = 25; public static void Convert(String inputTxtFile, String outputGraphicsFile) { List<String[]> v = readFile(inputTxtFile); Hashtable ht = getMinMax(v); int minX = (Int32)ht["minX"]; int maxX = (Int32)ht["maxX"]; int minY = (Int32)ht["minY"]; int maxY = (Int32)ht["maxY"]; int width = maxX - minX + baseX; int height = maxY - minY + baseY; Image image = new Bitmap(width, height); Graphics g2d = Graphics.FromImage(image); var b = new SolidBrush(Color.White); g2d.FillRectangle(b, 0, 0, width, height); drawImage(g2d, v, minX, minY); g2d.Dispose(); image.Save(outputGraphicsFile, ImageFormat.Jpeg); } private static void drawImage(Graphics g2d, List<String[]> v, int minX, int minY) { for (int j = 0; j < v.Count; j++) { String[] points = (String[])v[j]; int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0; for (int i = 0; i < 4; i++) { x1 = Int32.Parse(points[0]) - minX + (baseX / 2); x2 = Int32.Parse(points[2]) - minX + (baseX / 2); y1 = Int32.Parse(points[1]) - minY + (baseY / 2); y2 = Int32.Parse(points[3]) - minY + (baseY / 2); } var p = new Pen(Color.Black); g2d.DrawLine(p, x1, y1, x2, y2); } } private static Hashtable getMinMax(List<String[]> v) { Hashtable ht = new Hashtable(); int minX = 9999; int maxX = 0; int minY = 9999; int maxY = 0; for (int j = 0; j < v.Count; j++) { String[] points = (String[])v[j]; for (int i = 0; i < 4; i++) { minX = (Int32.Parse(points[0]) < minX) ? Int32.Parse(points[0]) : minX; minX = (Int32.Parse(points[2]) < minX) ? Int32.Parse(points[2]) : minX; maxX = (Int32.Parse(points[0]) > maxX) ? Int32.Parse(points[0]) : maxX; maxX = (Int32.Parse(points[2]) > maxX) ? Int32.Parse(points[2]) : maxX; minY = (Int32.Parse(points[1]) < minY) ? Int32.Parse(points[1]) : minY; minY = (Int32.Parse(points[3]) < minY) ? Int32.Parse(points[3]) : minY; maxY = (Int32.Parse(points[1]) > maxY) ? Int32.Parse(points[1]) : maxY; maxY = (Int32.Parse(points[3]) > maxY) ? Int32.Parse(points[3]) : maxY; } } ht.Add("minX", minX); ht.Add("maxX", maxX); ht.Add("minY", minY); ht.Add("maxY", maxY); return ht; } private static List<String[]> readFile(String fileName) { List<String[]> v = new List<String[]>(); var reader = new StreamReader(fileName); String record = null; while ((record = reader.ReadLine()).Trim().Length > 0) { String[] points = new String[4]; points = record.Split(' '); v.Add(points); } return v; } [MTAThread] static void Main(String[] args) { try { String inputTextFile = ".\\Sign.txt"; String outputImageFile = ".\\Sign.jpg"; Convert(inputTextFile, outputImageFile); } catch (Exception ex) { Console.WriteLine(" exception: " + ex.ToString()); } } } }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Software Development

My Visual Studio Color Settings

by Emilio Cavazos 8. December 2008 02:59

I am really surprised that there isn't a community site for sharing Visual Studio settings.  If there is one, let me know because I've looked and couldn't find anything.  I thought it would be nice to share my font and color settings for the people interested.  It's a version of Ragnarok Grey that I tweaked a little to fit my personal taste.  I uploaded some screen shots below to give you a preview.   The download only contains font and color settings so it will not change any of your other VS settings.  You should still back up your current settings just to be on the safe side. 

Dark-Colors.zip (1.97 kb)

You can find more color settings by Tomas Restrepo here including the original.

C#



HTML

JavaScript

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Software Development

ToFormattedList Extension Method

by Emilio Cavazos 1. December 2008 14:54

Forgive me if this incorrect, but I believe the ToFormattedList() IEnumerable extension method was part of the early ASP.NET MVC previews.  If you’ve used the method then you are aware of its usefulness.  You pass in a format string (ex. “<li>{0}</li>”) and you get back a string of all the objects that were in your IEnumerable individually formatted ("very nice" -Borat).  This is perfect if all the objects in your IEnumerable have an acceptable ToString() method. What if you are working with POCOs or business objects and you would like to use a property instead of an objects ToString() value?  This is exactly the scenario that prompted me to create a ToFormattedList() extension method for IEnumerable<T>.  

To satisfy this new requirement I needed to add a new parameter.  My version of ToFormattedList() takes a Func<T, string> parameter to access the appropriate property (we need the func, we gotta have that func).  Below is the simple code that makes this work and example of its use.

Code

    public static string ToFormattedList<T>(this IEnumerable<T> list, string format, Func<T, string> func) {
        var s = "";

        foreach (T item in list) {
            s += string.Format(format, func.Invoke(item));
        }

        return s;
    }

Usage

    var output = companies.ToFormattedList("<li>{0}</li>", c => c.Name);

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Software Development

Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO


Subscribe

Jeff is reading...