VintaSoft Barcode .NET SDK v10.0
Vintasoft.Barcode Namespace / BarcodeGraphicsRenderer Class
Members Example




In This Topic
    BarcodeGraphicsRenderer Class
    In This Topic
    Provides a base abstract class that renders barcode on System.Drawing.Graphics.
    Object Model
    BarcodeGraphicsRenderer ClassBarcodeElement Class
    Syntax
    'Declaration
     
    Public MustInherit Class BarcodeGraphicsRenderer 
       Inherits BarcodeRenderer
    'Usage
     
    Dim instance As BarcodeGraphicsRenderer
    public abstract class BarcodeGraphicsRenderer : BarcodeRenderer 
    public __gc abstract class BarcodeGraphicsRenderer : public BarcodeRenderer 
    public ref class BarcodeGraphicsRenderer abstract : public BarcodeRenderer 
    Example
    This example shows how to create barcode renderer that overrides standard rendering algorithms.
    ''' <summary>   
    ''' Generate QR barcode as an image.   
    ''' </summary>   
    ''' <param name="value">The value.</param>   
    ''' <param name="width">The image width, in pixels.</param>   
    ''' <param name="height">The image height, in pixels.</param>   
    Public Shared Function GenerateQRBarcodeImage(ByVal value As String, ByVal width As Integer, ByVal height As Integer) As Bitmap   
        Dim writer As New BarcodeWriter()   
        writer.Settings.Barcode = BarcodeType.QR   
        writer.Settings.Value = value   
        writer.Settings.Resolution = 96.0F   
        writer.Settings.Padding = 0   
        Dim quietZone As Integer = Math.Min(width, height) \ 20   
        writer.Settings.QuietZoneTop = quietZone   
        writer.Settings.QuietZoneLeft = quietZone   
        writer.Settings.QuietZoneRight = quietZone   
        writer.Settings.QuietZoneBottom = quietZone   
        Dim renderer As New BarcodeSimpleRenderer()   
        Return renderer.GetBarcodeAsBitmap(writer, width, height, UnitOfMeasure.Pixels)   
    End Function   
       
    ''' <summary>   
    ''' A simple renderer of matrix barcode.   
    ''' </summary>   
    ''' <remarks>   
    ''' This renderer draws the barcode search patterns and other barcode elements separately.   
    ''' </remarks>   
    Public Class BarcodeSimpleRenderer   
        Inherits BarcodeGraphicsRenderer   
       
    #Region "Fields"   
       
        ''' <summary>   
        ''' The brush that is used for filling the barcode elements.   
        ''' </summary>   
        Private _barcodeBrush As New SolidBrush(Color.Black)   
       
        ''' <summary>   
        ''' The pen that is used for drawing the barcode elements.   
        ''' </summary>   
        Private _barcodePen As New Pen(Color.Black, 1)   
       
    #End Region   
       
       
       
    #Region "Constructors"   
       
        ''' <summary>   
        ''' Initializes a new instance of the <see cref="BarcodeSimpleRenderer"/> class.   
        ''' </summary>   
        Public Sub New()   
            _barcodePen.LineJoin = LineJoin.Round   
        End Sub   
       
    #End Region   
       
       
       
    #Region "Properties"   
       
        ''' <summary>   
        ''' Gets or sets the color that is used for drawing the barcode.   
        ''' </summary>   
        <Category("Colors")> _   
        <Description("Color that is used for drawing the barcode.")> _   
        Public Property BarcodeColor() As Color   
            Get   
                Return _barcodeBrush.Color   
            End Get   
            Set(ByVal value As Color)   
                _barcodeBrush.Color = value   
                _barcodePen.Color = value   
            End Set   
        End Property   
       
    #End Region   
       
       
       
    #Region "Methods"   
       
        ''' <summary>   
        ''' Renders the barcode matrix element.   
        ''' </summary>   
        ''' <param name="element">The barcode matrix element to render.</param>   
        ''' <param name="x">The X-coordinate, where barcode matrix element must be rendered.</param>   
        ''' <param name="y">The Y-coordinate, where barcode matrix element must be rendered.</param>   
        Protected Overrides Sub RenderBarcodeMatrixElement(ByVal element As BarcodeMatrixElement, ByVal x As Integer, ByVal y As Integer)   
            ' customize drawing of search patterns   
       
            Dim isAztecSearchPattern As Boolean = False   
            If element Is BarcodeElements.AztecCompactSearchPattern Then   
                isAztecSearchPattern = True   
            ElseIf element Is BarcodeElements.AztecFullRangeSearchPattern Then   
                isAztecSearchPattern = True   
            End If   
       
            ' QR search pattern   
            If element Is BarcodeElements.QrSearchPattern Then   
                ' (x,y)   
                '    #######   
                '    #     #   
                '    # ### #   
                '    # ### #   
                '    # ### #   
                '    #     #   
                '    #######   
                '       (x+7,y+7)                   
                ' draw outer rectangle 7x7   
                Graphics.DrawRectangle(_barcodePen, x + 1.5F, y + 1.5F, 6, 6)   
                ' draw inner cell 3x3   
                Graphics.FillRectangle(_barcodeBrush, x + 3.75F, y + 3.75F, 1.5F, 1.5F)   
                Graphics.DrawRectangle(_barcodePen, x + 3.5F, y + 3.5F, 2.0F, 2.0F)   
                ' Aztec search pattern   
            ElseIf isAztecSearchPattern Then   
                '  Compact/Rune          Full Range   
                '                    (x,y)   
                '                       #############   
                ' (x,y)                 #           #   
                '    #########          # ######### #   
                '    #       #          # #       # #   
                '    # ##### #          # # ##### # #   
                '    # #   # #          # # #   # # #   
                '    # # # # #          # # # # # # #   
                '    # #   # #          # # #   # # #   
                '    # ##### #          # # ##### # #   
                '    #       #          # #       # #   
                '    #########          # ######### #   
                '         (x+9,y+9)     #           #   
                '                       #############   
                '                               (x+13,y+13)   
                ' draw outer rectangles: 13x13, 9x9, 5x5   
                Dim rectCount As Integer   
                If element Is BarcodeElements.AztecFullRangeSearchPattern Then   
                    rectCount = 3   
                Else   
                    rectCount = 2   
                End If   
                For i As Integer = 0 To rectCount - 1   
                    Graphics.DrawRectangle(_barcodePen, x + 0.5F, y + 0.5F, (rectCount - i) * 4, (rectCount - i) * 4)   
                    x += 2   
                    y += 2   
                Next   
       
                ' draw inner cell 1x1   
                Graphics.FillEllipse(_barcodeBrush, New Rectangle(x, y, 1, 1))   
                ' DataMatrix and Han Xin Code search patterns   
            ElseIf element.Name.Contains("Search Pattern") Then   
                ' draw as single pattern use graphics path   
                Using path As New GraphicsPath()   
                    For yc As Integer = 0 To element.Height - 1   
                        For xc As Integer = 0 To element.Width - 1   
                            If element.Matrix(yc, xc) Is BarcodeElements.BlackCell Then   
                                path.AddRectangle(New Rectangle(x + xc, y + yc, 1, 1))   
                            End If   
                        Next   
                    Next   
                    Graphics.FillPath(_barcodeBrush, path)   
                End Using   
            Else   
                MyBase.RenderBarcodeMatrixElement(element, x, y)   
            End If   
        End Sub   
       
        ''' <summary>   
        ''' Renders the barcode element.   
        ''' </summary>   
        ''' <param name="barcodeElement">The barcode element to render.</param>   
        ''' <param name="x">The X-coordinate, where barcode element must be rendered.</param>   
        ''' <param name="y">The Y-coordinate, where barcode element must be rendered.</param>   
        Protected Overrides Sub RenderBarcodeElement(ByVal barcodeElement As BarcodeElement, ByVal x As Integer, ByVal y As Integer)   
            If barcodeElement Is BarcodeElements.BlackCell Then   
                Graphics.FillEllipse(_barcodeBrush, New Rectangle(x, y, 1, 1))   
            Else   
                MyBase.RenderBarcodeElement(barcodeElement, x, y)   
            End If   
        End Sub   
       
        ''' <summary>   
        ''' Returns a <see cref="System.String" /> that represents this instance.   
        ''' </summary>   
        ''' <returns>   
        ''' A <see cref="System.String" /> that represents this instance.   
        ''' </returns>   
        Public Overrides Function ToString() As String   
            Return "Simple dots"   
        End Function   
       
    #End Region   
       
    End Class
    /// <summary>
    /// Generate QR barcode as an image.
    /// </summary>
    /// <param name="value">The value.</param>
    /// <param name="width">The image width, in pixels.</param>
    /// <param name="height">The image height, in pixels.</param>
    public static Bitmap GenerateQRBarcodeImage(string value, int width, int height)
    {
        BarcodeWriter writer = new BarcodeWriter();
        writer.Settings.Barcode = BarcodeType.QR;
        writer.Settings.Value = value;
        writer.Settings.Resolution = 96f;
        writer.Settings.Padding = 0;
        int quietZone = Math.Min(width, height) / 20;
        writer.Settings.QuietZoneTop = quietZone;
        writer.Settings.QuietZoneLeft = quietZone;
        writer.Settings.QuietZoneRight = quietZone;
        writer.Settings.QuietZoneBottom = quietZone;
        BarcodeSimpleRenderer renderer = new BarcodeSimpleRenderer();
        return renderer.GetBarcodeAsBitmap(writer, width, height, UnitOfMeasure.Pixels);
    }
                
    /// <summary>
    /// A simple renderer of matrix barcode.
    /// </summary>
    /// <remarks>
    /// This renderer draws the barcode search patterns and other barcode elements separately.
    /// </remarks>
    public class BarcodeSimpleRenderer : BarcodeGraphicsRenderer
    {
                
        #region Fields
                
        /// <summary>
        /// The brush that is used for filling the barcode elements.
        /// </summary>
        SolidBrush _barcodeBrush = new SolidBrush(Color.Black);
                
        /// <summary>
        /// The pen that is used for drawing the barcode elements.
        /// </summary>
        Pen _barcodePen = new Pen(Color.Black, 1);
                
        #endregion
                
                
                
        #region Constructors
                
        /// <summary>
        /// Initializes a new instance of the <see cref="BarcodeSimpleRenderer"/> class.
        /// </summary>
        public BarcodeSimpleRenderer()
        {
            _barcodePen.LineJoin = LineJoin.Round;
        }
                
        #endregion
                
                
                
        #region Properties
                
        /// <summary>
        /// Gets or sets the color that is used for drawing the barcode.
        /// </summary>
        [Category("Colors")]
        [Description("Color that is used for drawing the barcode.")]
        public Color BarcodeColor
        {
            get
            {
                return _barcodeBrush.Color;
            }
            set
            {
                _barcodeBrush.Color = value;
                _barcodePen.Color = value;
            }
        }
                
        #endregion
                
                
                
        #region Methods
                
        /// <summary>
        /// Renders the barcode matrix element.
        /// </summary>
        /// <param name="element">The barcode matrix element to render.</param>
        /// <param name="x">The X-coordinate, where barcode matrix element must be rendered.</param>
        /// <param name="y">The Y-coordinate, where barcode matrix element must be rendered.</param>
        protected override void RenderBarcodeMatrixElement(
            BarcodeMatrixElement element,
            int x,
            int y)
        {
            // customize drawing of search patterns
                
            bool isAztecSearchPattern = false;
            if (element == BarcodeElements.AztecCompactSearchPattern)
                isAztecSearchPattern = true;
            else if (element == BarcodeElements.AztecFullRangeSearchPattern)
                isAztecSearchPattern = true;
                
            // QR search pattern
            if (element == BarcodeElements.QrSearchPattern)
            {
                // (x,y)
                //    #######
                //    #     #
                //    # ### #
                //    # ### #
                //    # ### #
                //    #     #
                //    #######
                //       (x+7,y+7)                
                // draw outer rectangle 7x7
                Graphics.DrawRectangle(_barcodePen, x + 1.5f, y + 1.5f, 6, 6);
                // draw inner cell 3x3
                Graphics.FillRectangle(_barcodeBrush, x + 3.75f, y + 3.75f, 1.5f, 1.5f);
                Graphics.DrawRectangle(_barcodePen, x + 3.5f, y + 3.5f, 2f, 2f);
            }
            // Aztec search pattern
            else if (isAztecSearchPattern)
            {
                //  Compact/Rune          Full Range
                //                    (x,y)
                //                       #############
                // (x,y)                 #           #
                //    #########          # ######### #
                //    #       #          # #       # #
                //    # ##### #          # # ##### # #
                //    # #   # #          # # #   # # #
                //    # # # # #          # # # # # # #
                //    # #   # #          # # #   # # #
                //    # ##### #          # # ##### # #
                //    #       #          # #       # #
                //    #########          # ######### #
                //         (x+9,y+9)     #           #
                //                       #############
                //                               (x+13,y+13)
                // draw outer rectangles: 13x13, 9x9, 5x5
                int rectCount;
                if (element == BarcodeElements.AztecFullRangeSearchPattern)
                    rectCount = 3;
                else
                    rectCount = 2;
                for (int i = 0; i < rectCount; i++)
                {
                    Graphics.DrawRectangle(_barcodePen,
                        x + 0.5f, y + 0.5f,
                        (rectCount - i) * 4, (rectCount - i) * 4);
                    x += 2;
                    y += 2;
                }
                
                // draw inner cell 1x1
                Graphics.FillEllipse(_barcodeBrush, new Rectangle(x, y, 1, 1));
            }
            // DataMatrix and Han Xin Code search patterns
            else if (element.Name.Contains("Search Pattern"))
            {
                // draw as single pattern use graphics path
                using (GraphicsPath path = new GraphicsPath())
                {
                    for (int yc = 0; yc < element.Height; yc++)
                    {
                        for (int xc = 0; xc < element.Width; xc++)
                        {
                            if (element.Matrix[yc, xc] == BarcodeElements.BlackCell)
                                path.AddRectangle(new Rectangle(x + xc, y + yc, 1, 1));
                        }
                    }
                    Graphics.FillPath(_barcodeBrush, path);
                }
            }
            else
            {
                base.RenderBarcodeMatrixElement(element, x, y);
            }
        }
                
        /// <summary>
        /// Renders the barcode element.
        /// </summary>
        /// <param name="barcodeElement">The barcode element to render.</param>
        /// <param name="x">The X-coordinate, where barcode element must be rendered.</param>
        /// <param name="y">The Y-coordinate, where barcode element must be rendered.</param>
        protected override void RenderBarcodeElement(BarcodeElement barcodeElement, int x, int y)
        {
            if (barcodeElement == BarcodeElements.BlackCell)
            {
                Graphics.FillEllipse(_barcodeBrush, new Rectangle(x, y, 1, 1));
            }
            else
            {
                base.RenderBarcodeElement(barcodeElement, x, y);
            }
        }
                
        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return "Simple dots";
        }
                
        #endregion
                
    }
    Inheritance Hierarchy

    System.Object
       Vintasoft.Barcode.BarcodeRenderer
          Vintasoft.Barcode.BarcodeGraphicsRenderer

    Requirements

    Target Platforms: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also