Skip to content

Instantly share code, notes, and snippets.

@mgaffigan
Created June 17, 2017 17:24
Show Gist options
  • Save mgaffigan/1ff9d201f5c700e12aee1846aa9d00a2 to your computer and use it in GitHub Desktop.
Save mgaffigan/1ff9d201f5c700e12aee1846aa9d00a2 to your computer and use it in GitHub Desktop.
Example showing a barcode renderer which uses GET parameters to render images
using dmx = IEC16022Sharp;
using gdii = System.Drawing.Imaging;
using gdi = System.Drawing;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.ServiceModel.Web;
using zen = Zen.Barcode;
namespace StackOverflowAnswers.BarcodeRendering
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple,
AddressFilterMode = AddressFilterMode.Any)]
[ServiceContract]
[DataContractFormat]
public class BarcodeRendererService : IDisposable
{
public BarcodeRendererService()
{
// TODO: setup servicehost
}
public void Dispose()
{
// shut down servicehost
}
[OperationContract, WebGet(UriTemplate = "2d/{type}?moduleSize={moduleSize}&data={data}&dataType={dataType}&milsPerModule={milsPerModule}")]
public Stream Render2dBarcode(string type, string moduleSize, string data, string dataType, string milsPerModule)
{
if (string.IsNullOrWhiteSpace(type))
throw new ArgumentNullException("type");
int moduleSizeInt = 1;
if (!string.IsNullOrWhiteSpace(moduleSize))
{
if (!int.TryParse(moduleSize, out moduleSizeInt))
{
throw new ArgumentException("moduleSize");
}
if (moduleSizeInt > 50 || moduleSizeInt < 1)
{
throw new ArgumentException("moduleSize");
}
}
float milsPerModuleDouble = 13f;
if (!string.IsNullOrWhiteSpace(milsPerModule))
{
if (!float.TryParse(milsPerModule, out milsPerModuleDouble))
{
throw new ArgumentException("moduleSize");
}
if (milsPerModuleDouble > 5000 || milsPerModuleDouble < 1)
{
throw new ArgumentException("moduleSize");
}
}
if (type.Equals("datamatrix", StringComparison.InvariantCultureIgnoreCase))
{
return RenderDatamatrix(moduleSizeInt, data, dataType, milsPerModuleDouble);
}
throw new NotSupportedException("Unknown type");
}
private Stream RenderDatamatrix(int moduleSizeInt, string data, string dataType, float milsPerModuleDouble)
{
if (string.IsNullOrWhiteSpace(dataType))
{
dataType = "ASCII";
}
dmx.EncodingType encType = (dmx.EncodingType)Enum.Parse(typeof(dmx.EncodingType), dataType, true);
dmx.DataMatrix barcode;
if (encType != dmx.EncodingType.Binary)
{
barcode = new dmx.DataMatrix(data, encType);
}
else // Binary
{
byte[] dataBin;
Guid guid;
if (Guid.TryParse(data, out guid))
{
dataBin = guid.ToByteArray();
}
else
{
dataBin = Convert.FromBase64String(data);
}
barcode = new dmx.DataMatrix(dataBin, 0, 0, dmx.EncodingType.Binary);
}
var ms = new MemoryStream();
var newSize = new gdi.Size(barcode.Image.Width * moduleSizeInt, barcode.Image.Height * moduleSizeInt);
using (var newImage = new gdi.Bitmap(newSize.Width, newSize.Height))
{
using (var gfx = gdi.Graphics.FromImage(newImage))
{
gfx.InterpolationMode = gdi.Drawing2D.InterpolationMode.NearestNeighbor;
gfx.PixelOffsetMode = gdi.Drawing2D.PixelOffsetMode.HighQuality;
gfx.DrawImage(barcode.Image, 0, 0, newSize.Width, newSize.Height);
}
var worldSize = new gdi.SizeF(
barcode.Image.Width * milsPerModuleDouble / 1000f,
barcode.Image.Height * milsPerModuleDouble / 1000f);
var resolution = new gdi.SizeF(
newSize.Width / worldSize.Width,
newSize.Height / worldSize.Height);
newImage.SetResolution(resolution.Width, resolution.Height);
newImage.Save(ms, gdii.ImageFormat.Png);
ms.Position = 0;
}
WebOperationContext.Current.OutgoingResponse.ContentType = "image/png";
return ms;
}
[OperationContract, WebGet(UriTemplate = "1d/{type}?moduleWidth={moduleWidth}&height={height}&data={data}&milsPerModule={milsPerModule}")]
public Stream Render1dBarcode(string type, string moduleWidth, string height, string data, string milsPerModule)
{
if (string.IsNullOrWhiteSpace(type))
throw new ArgumentNullException("type");
int moduleWidthInt = 1;
if (!string.IsNullOrWhiteSpace(moduleWidth))
{
if (!int.TryParse(moduleWidth, out moduleWidthInt))
{
throw new ArgumentException("moduleWidth");
}
if (moduleWidthInt > 50 || moduleWidthInt < 1)
{
throw new ArgumentException("moduleWidth");
}
}
int heightInt = 25;
if (!string.IsNullOrWhiteSpace(height))
{
if (!int.TryParse(height, out heightInt))
{
throw new ArgumentException("height");
}
if (heightInt > 1000 || heightInt < 1)
{
throw new ArgumentException("height");
}
}
float milsPerModuleDouble = 10f;
if (!string.IsNullOrWhiteSpace(milsPerModule))
{
if (!float.TryParse(milsPerModule, out milsPerModuleDouble))
{
throw new ArgumentException("moduleSize");
}
if (milsPerModuleDouble > 5000 || milsPerModuleDouble < 1)
{
throw new ArgumentException("moduleSize");
}
}
gdi.Bitmap barcode;
if (type.Equals("code128", StringComparison.InvariantCultureIgnoreCase))
{
barcode = (gdi.Bitmap)zen.BarcodeDrawFactory.Code128WithChecksum.Draw(data, heightInt, moduleWidthInt);
}
else if (type.Equals("code39", StringComparison.InvariantCultureIgnoreCase))
{
barcode = (gdi.Bitmap)zen.BarcodeDrawFactory.Code39WithoutChecksum.Draw(data, heightInt, moduleWidthInt);
}
else throw new NotSupportedException("Unknown type");
var worldSize = new gdi.SizeF(
(barcode.Width / moduleWidthInt) * milsPerModuleDouble / 1000f,
heightInt * milsPerModuleDouble / 1000f);
var resolution = new gdi.SizeF(
barcode.Width / worldSize.Width,
barcode.Height / worldSize.Height);
barcode.SetResolution(resolution.Width, resolution.Height);
var ms = new MemoryStream();
barcode.Save(ms, gdii.ImageFormat.Png);
ms.Position = 0;
WebOperationContext.Current.OutgoingResponse.ContentType = "image/png";
return ms;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment