BarcodeLib – ported to Compact Framework
Print This Post
I got a request for a barcode image generator for Windows Mobile. Fortunately there are some libs out there including source code and a decided to test to port barcodelib by Brad Barnhill hosted at the famous CodeProject site (http://www.codeproject.com/KB/graphics/BarcodeLibrary.aspx).
How was it done
I downloaded the code, started a new SmartDevice Class Library project in VS2005 and copied the source files into the project dir and started add the existing code files. I did not start with the original project files, as they were created with VS2008 and uses full framework in version 3.5.
After some builds, code changes, adding conditional compiles and one cfhelper class, the class compiled fine for Compact Framework 2. I had to remove (conditional compile options) most of the BarcodeXML stuff, as these are not? portable that easy to CF2.
using (Pen pen = new Pen(ForeColor, iBarWidth))
{
#if !WindowsCE
pen.Alignment = PenAlignment.Right;
#endif
while (pos < Encoded_Value.Length)
{
if (Encoded_Value[pos] == '1')
#if !WindowsCE
g.DrawLine(pen, new Point(pos * iBarWidth + shiftAdjustment, 0), new Point(pos * iBarWidth + shiftAdjustment, Height));
#else
cfHelper.myDrawLine(g, pen, new Point(pos * iBarWidth + shiftAdjustment, 0), new Point(pos * iBarWidth + shiftAdjustment, Height));
#endif
and here is my helper:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace BarcodeLib
{
static class cfHelper
{
public static void myDrawLine(Graphics g, Pen p, Point p1, Point p2)
{
g.DrawLine(p, p1.X, p1.Y, p2.X, p2.Y);
}
}
}
Then I added a BarcodeLibTest smart device Windows Application project using the new BarcodeLib and was able to get barcodes printed on screen and save the images to files.
Download Source: DOWNLOAD:BarcodeLibCF2 - (Hits: 921, size: 304.48 KB)









Is all you changed the DrawLine calls? If so Ill make this change to make it compatible with the Compact Framework.
Yes, I had to change the DrwaLine and PenAlignment code lines AND commented out all XML stuff. That’s all I had to change.
Great to have ou reading this.
BTW: to be able to use one source code base, you have to use the #if conditional lines in the code files and create separate project files for smartdevice target. If you have forms in a project, you have to create separate forms for smartdevice projects.
Regards
Josef