Recently I had to render barcodes in a WPF client and had to be able to print them as well.
On this project we were using DevExpress controls, but the Barcode control (at least till version 13.1) was only available for reporting. There was no ‘regular’ WPF control to display barcodes on screen.
I first tried using a Barcode font. I found the website of John T Barton with lots of info about barcodes. Downloaded the latest version of the EAN128 font from the website of Grand Zebu, but unfortunately I ran into problems with the first barcodes I tried (“MSN12345” and “MSN00000”). One of the characters created by the code from the site (to calculate checksum and some compression) wasn’t supported by the font.
After some extra research on the internet I quickly came across the Zen Barcode Rendering framework on Codeplex. This free framework looked like the answer to my problems/requirements.
Unfortunately, it targeted ASP.NET, SSRS and WinForms, but no WPF. So …. no luck there. But, as the framework is an open source project, I downloaded the code and started investigating if it would be possible to make some modifications so it could be used by WPF clients.
After some investigations and trials I gave it a go and started reworking it for WPF:
- Changed measurements (sizes and positions) from int to double
- Changed drawing methods to use a DrawingContext instead of a Graphics object
- Removed the 2D stuff (QR and PDF417), including the corresponding resources, which reduced the size of the DLL from 12MB to less than 50KB!
- I added some extra code to render the barcode in the center of the drawing area (at least as far as the HorizontalAlignment is concerned)
- and maybe some other changes I don’t remember any more…
Anyway, after an hour or two, I was able to render barcodes in WPF, display them on screen and print them.
The technique I used was:
- Create a class that inherited from FrameworkElement.
- Override OnRender
- Catch the DrawingContext, determine the drawingSize (ActualWidth, ActualHeight) of the control
- (Optionally do some calculations for position and size of the barcode)
- Call a method from the Zen.Barcode.Core dll to render the Barcode, and voila: the code is displayed on your WPF-window.
- For printing I used a DrawingVisual-object which returns a DrawingContext when you call RenderOpen() on it.
- This DrawingContext can then be used to draw on using the same method.
- The DrawingVisual-object can be sent to a PrintDialog-object (you can even omit the ShowDialog call, set the PrintTicket and PrintQueue info yourself, and do a silent print without dialog)
This is the code for such a barcode control:
Just drop it on your WPF Window, set or bind the Barcode property, and voila, you have yourself a working WPF barcode control!
To print it you can use the same code to draw:
Check the code in this Sample project