public Bitmap Render(String str) { Bitmap bitmap = null; try { if (ZBarcode.EncodeAndBuffer(ref SymbolStruct, str, str.Length, this.Rotation) == 0) { // no error returned, create barcode preview bitmap = new Bitmap(SymbolStruct.bitmap_width, SymbolStruct.bitmap_height); byte[] bitmapBytes = new byte[SymbolStruct.bitmap_height * SymbolStruct.bitmap_width * 3]; Marshal.Copy(SymbolStruct.bitmap, bitmapBytes, 0, bitmapBytes.Length); // write image pixel-by-pixel int row, column, i = 0; int red, blue, green; for (row = 0; row < SymbolStruct.bitmap_height; row++) { for (column = 0; column < SymbolStruct.bitmap_width; column++) { red = bitmapBytes[i]; green = bitmapBytes[i + 1]; blue = bitmapBytes[i + 2]; bitmap.SetPixel(column, row, Color.FromArgb(red, green, blue)); i += 3; } } // reset bitmap ZBarcode.Clear(ref SymbolStruct); // ZBarcode.Clear() does not do this correctly in DLL, requires doing it manually... SymbolStruct.row_height = null; SymbolStruct.height = 0; } else { if (SymbolStruct.errtxt == null) { SymbolStruct.errtxt = "error!"; } bitmap = WriteErrorImage(SymbolStruct.errtxt); } } catch (Exception e) { // dll not found, application aborted bitmap = WriteErrorImage("Fatal error: a DLL is missing"); } return(bitmap); }
static Symbology() { try { SymbolStruct = (zint_symbol) // generate managed counterpart of struct Marshal.PtrToStructure(ZBarcode.Create(), typeof(zint_symbol)); } catch (Exception e) { // dll not found, application aborted } }