Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 19, 2025

Problem

The EAN-13 barcode implementation had critical quality issues that prevented reliable use:

  1. Unreadable numbers - The human-readable digits below the barcode were too small or blurry to read
  2. Scanner reading failures - Barcode scanners had difficulty reading the generated barcodes due to blurry bar edges

These issues were reported in [issue #XXX] with scanned images showing the poor quality output.

Root Cause

The original implementation in BarCodeEAN13.cs used GraphicsUnit.Millimeter for rendering, which caused several problems:

  • Fractional pixel positions: When bar positions were calculated in millimeters and didn't align to pixel boundaries, the graphics system applied anti-aliasing, creating gray pixels at bar edges
  • Blurry bars: Anti-aliased bars are problematic for barcode scanners which expect sharp black/white transitions
  • Font size issues: The font height calculation could result in extremely small text (< 8 pixels) that was difficult or impossible to read

Solution

This PR implements a pixel-perfect rendering approach:

1. Pixel-Based Rendering

Changed from GraphicsUnit.Millimeter to GraphicsUnit.Pixel for all drawing operations, ensuring precise control over pixel placement.

2. Graphics Quality Settings

Added explicit quality settings to prevent anti-aliasing on bars while preserving it for text:

g.InterpolationMode = Draw2.Drawing2D.InterpolationMode.NearestNeighbor;
g.SmoothingMode = Draw2.Drawing2D.SmoothingMode.None;
g.PixelOffsetMode = Draw2.Drawing2D.PixelOffsetMode.Half;

3. Pixel Alignment

Round all bar positions and widths to whole pixels to eliminate fractional positioning:

float x = (float)Math.Round(barWidthPixels * barCount);
float width = (float)Math.Round(barWidthPixels);

4. Improved Text Rendering

  • Set minimum font size to 8 pixels for guaranteed readability
  • Use TextRenderingHint.AntiAlias for smooth, readable text
  • Create fonts with GraphicsUnit.Pixel for consistent sizing

5. Proper Unit Conversion

Use the existing PixelConversions utility to properly convert millimeters to pixels based on the graphics context DPI.

Testing

Added comprehensive test suite in BarCodeEAN13Tests.cs:

  • Basic barcode generation test
  • Multiple size tests (200x150, 300x200, 400x250)
  • Designer image test
  • ZXing barcode scanner validation to ensure generated barcodes are scannable

Impact

  • BarCodeEAN13: Direct fix - produces crisp, scannable barcodes with readable text
  • BarCodeBookland: Indirect fix - automatically improved as it uses EAN-13 internally
  • Compatibility: Fully backward compatible - no changes to API, properties, or behavior
  • Performance: No performance impact - same number of drawing operations

Visual Comparison

Before: Bars had anti-aliased edges (gray pixels at boundaries), text was too small
After: Sharp black/white bar boundaries perfect for scanning, readable text with minimum 8-pixel font

The changes ensure that:

  • Barcode scanners can reliably read the barcodes (crisp bar edges)
  • Humans can read the numbers (larger, anti-aliased text)
  • Output quality is consistent across different DPI settings

Files Changed

  • RdlCri/BarCodeEAN13.cs - Core rendering improvements (64 lines modified)
  • ReportTests/BarCodeEAN13Tests.cs - New comprehensive test suite (148 lines added)

Total: 2 files changed, 191 insertions(+), 21 deletions(-)

Original prompt

This section details on the original issue you should resolve

<issue_title>Bar code EAN quality</issue_title>
<issue_description>We are trying to use the ean 13 bar code but we observed some issue.
First of all, the numbers are unreadale, but when print on pdf there's no issue.
The bar code seem correct but the bar code reader has some trouble to read. The bars look well defined .
We add some other text to show that there is not any printer quality issue.

Images attached ( acquired with scanner) .
barcodesmall
barcodesmall2

</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #182


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ability

Co-authored-by: majorsilence <656288+majorsilence@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issues with EAN 13 barcode readability Fix EAN-13 barcode rendering quality for better scanning and readability Oct 19, 2025
Copilot AI requested a review from majorsilence October 19, 2025 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bar code EAN quality

2 participants