【发布时间】:2017-09-15 14:04:07
【问题描述】:
下面是我的代码
绘制方法如下: 现在,当我生成代码时,它会替换现有的条形码,并且不会在图片框上放置新的条形码。
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个条码
【问题讨论】:
-
您每次都用一张新图片替换当前图片。一个图片框只能容纳一张图片。
-
@bwoogie 是的,我知道,我想要打印条形码的替代解决方案。
-
为什么不在
CreateBitmap中创建一个循环?即让DrawEan13Barcode返回yPosition,然后将其传递给DrawEan13Barcode的下一个调用作为凝视y -
那么,继续显示多个条码图像。你知道如何显示一个。现在执行 N 次。如果您不想理解您发布的代码,请聘请开发人员来帮助您。
-
@IInspectable 理论上听起来很简单,我自己就是开发人员...问题来自显示位置,我无法管理我尝试创建位图图像但转储图像为空白的位置...我使用了 bitmap.getthumbnails 图像
标签: c# .net windows winforms barcode