在Windows窗体中打印多个条形码应用程序.NET C#

时间:2017-09-15 14:04:07

标签: c# .net windows winforms barcode

enter image description here

下面是我的代码

绘制方法如下: 现在,当我生成代码时,它会替换现有的条形码,并且不会在图片框上放置新的条形码。

System.Drawing.Graphics g = this.pictureBox1.CreateGraphics();
g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.SystemColors.Control), new Rectangle(x, y, 50, 50));
ean13.Scale = 1.0F;
ean13.DrawEan13Barcode(g, new System.Drawing.Point(a, a));
public void DrawEan13Barcode( System.Drawing.Graphics g, System.Drawing.Point pt )
{
    float width = this.Width * this.Scale;
    float height = this.Height * this.Scale;

    //  EAN13 Barcode should be a total of 113 modules wide.
    float lineWidth = width / 113f;

    // Save the GraphicsState.
    System.Drawing.Drawing2D.GraphicsState gs = g.Save( );

    // Set the PageUnit to Inch because all of our measurements are in inches.
    g.PageUnit = System.Drawing.GraphicsUnit.Millimeter;

    // Set the PageScale to 1, so a millimeter will represent a true millimeter.
    g.PageScale = 1;

    System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush( System.Drawing.Color.Black );

    float xPosition = 0;

    System.Text.StringBuilder strbEAN13 = new System.Text.StringBuilder( );
    System.Text.StringBuilder sbTemp = new System.Text.StringBuilder( );

    float xStart = pt.X;
    float yStart = pt.Y;
    float xEnd = 0;

    System.Drawing.Font font = new System.Drawing.Font( "Arial", this._fFontSize * this.Scale );

    // Calculate the Check Digit.
    this.CalculateChecksumDigit( );

    sbTemp.AppendFormat( "{0}{1}{2}{3}", 
        this.CountryCode,
        this.ManufacturerCode,
        this.ProductCode, 
        this.ChecksumDigit );


    string sTemp = sbTemp.ToString( );

    string sLeftPattern = "";

    // Convert the left hand numbers.
    sLeftPattern = ConvertLeftPattern( sTemp.Substring( 0, 7 ) );

    // Build the UPC Code.
    strbEAN13.AppendFormat( "{0}{1}{2}{3}{4}{1}{0}",
        this._sQuiteZone, this._sLeadTail,
        sLeftPattern,                   
        this._sSeparator,
        ConvertToDigitPatterns( sTemp.Substring( 7 ), this._aRight ) );

    string sTempUPC = strbEAN13.ToString( );

    float fTextHeight = g.MeasureString( sTempUPC, font ).Height;

    // Draw the barcode lines.
    for( int i = 0; i < strbEAN13.Length; i++ )
    {
        if( sTempUPC.Substring( i, 1 ) == "1" )
        {
            if( xStart == pt.X )
                xStart = xPosition;

            // Save room for the UPC number below the bar code.
            if( ( i > 12 && i < 55 ) || ( i > 57 && i < 101 ) )
                // Draw space for the number
                g.FillRectangle( brush, xPosition, yStart, lineWidth, height - fTextHeight );
            else
                // Draw a full line.
                g.FillRectangle( brush, xPosition, yStart, lineWidth, height );
        }

        xPosition += lineWidth;
        xEnd = xPosition;
    }

    // Draw the upc numbers below the line.
    xPosition = xStart - g.MeasureString( this.CountryCode.Substring( 0, 1 ), font ).Width;
    float yPosition = yStart + ( height - fTextHeight );

    // Draw 1st digit of the country code.
    g.DrawString( sTemp.Substring( 0, 1 ), font, brush, new System.Drawing.PointF( xPosition, yPosition ) );

    xPosition += ( g.MeasureString( sTemp.Substring( 0, 1 ), font ).Width + 43 * lineWidth ) -
        ( g.MeasureString( sTemp.Substring( 1, 6 ), font ).Width );

    // Draw MFG Number.
    g.DrawString( sTemp.Substring( 1, 6 ), font, brush, new System.Drawing.PointF( xPosition, yPosition ) );

    xPosition += g.MeasureString( sTemp.Substring( 1, 6 ), font ).Width + ( 11 * lineWidth );

    // Draw Product ID.
    g.DrawString( sTemp.Substring( 7 ), font, brush, new System.Drawing.PointF( xPosition, yPosition ) );

    // Restore the GraphicsState.
    g.Restore( gs );
}


public System.Drawing.Bitmap CreateBitmap( )
{
    float tempWidth = ( this.Width * this.Scale ) * 100 ;
    float tempHeight = ( this.Height * this.Scale ) * 100;

    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap( (int)tempWidth, (int)tempHeight );

    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage( bmp );
    this.DrawEan13Barcode( g, new System.Drawing.Point( 0, 0 ) );
    g.Dispose( );
    return bmp;
}

如果有其他方法可以做到这一点,那么要求是明确的

使用Windows窗体绘制和打印N个条形码

1 个答案:

答案 0 :(得分:-2)

您可以生成条形码并将其图像保存在数据库sql server中。 然后创建水晶报告并在水晶报告详细信息部分获取该字段。你会得到你需要的东西。