0

我正在用 C# 创建一个 Code39 条形码图像,我需要为此图像添加边距。

这是我创建的第一张图片。

我创造了它。

但它应该是这样的。

应该是这样的。

代码很复杂。

此代码正在创建条码条、数字和标题字符串。

    public byte[] Code39(string code, int barSize, bool showCodeString, string title, string fontFile)
    {
        // Create stream....
        MemoryStream ms = new MemoryStream();
        FontFamilyName = "Free 3 of 9";//ConfigurationSettings.AppSettings["BarCodeFontFamily"];
        FontFileName = fontFile;//@"C:\Documents and Settings\narottam.sharma\Desktop\Barcode\WSBarCode\Code39Font\FREE3OF9.TTF";// ConfigurationSettings.AppSettings["BarCodeFontFile"];
        FontSize = barSize;
        ShowCodeString = showCodeString;
        if (title + "" != "")
            Title = title;
        Bitmap objBitmap = GenerateBarcode(code);
        objBitmap.Save(ms, ImageFormat.Png);

        //return bytes....
        return ms.GetBuffer();
    }

    public Bitmap GenerateBarcode(string barCode)
    {
        int bcodeWidth = 0;
        int bcodeHeight = 0;
        // Get the image container...
        Bitmap bcodeBitmap = CreateImageContainer(barCode, ref bcodeWidth, ref bcodeHeight);
        Graphics objGraphics = Graphics.FromImage(bcodeBitmap);

        // Fill the background          
        objGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bcodeWidth, bcodeHeight));
        int vpos = 0;
        // Draw the title string
        if (_titleString != null)
        {
            objGraphics.DrawString(_titleString, _titleFont, new SolidBrush(Color.Black), XCentered((int)_titleSize.Width, bcodeWidth), vpos);
            vpos += (((int)_titleSize.Height) + _itemSepHeight);
        }
        // Draw the barcode
        objGraphics.DrawString(barCode, Code39Font, new SolidBrush(Color.Black), XCentered((int)_barCodeSize.Width, bcodeWidth), vpos);


        // Draw the barcode string
        if (_showCodeString)
        {
            vpos += (((int)_barCodeSize.Height));
            objGraphics.DrawString(barCode, _codeStringFont, new SolidBrush(Color.Black), XCentered((int)_codeStringSize.Width, bcodeWidth), vpos);
        }
        // return the image...                                  
        return bcodeBitmap;
    }

    private Bitmap CreateImageContainer(string barCode, ref int bcodeWidth, ref int bcodeHeight)
    {
        Graphics objGraphics;

        // Create a temporary bitmap...
        Bitmap tmpBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
        objGraphics = Graphics.FromImage(tmpBitmap);

        // calculate size of the barcode items...
        if (_titleString != null)
        {
            _titleSize = objGraphics.MeasureString(_titleString, _titleFont);
            bcodeWidth = (int)_titleSize.Width;
            bcodeHeight = (int)_titleSize.Height + _itemSepHeight;
        }
        _barCodeSize = objGraphics.MeasureString(barCode, Code39Font);
        bcodeWidth = Max(bcodeWidth, (int)_barCodeSize.Width);
        bcodeHeight += (int)_barCodeSize.Height;
        if (_showCodeString)
        {
            _codeStringSize = objGraphics.MeasureString(barCode, _codeStringFont);
            bcodeWidth = Max(bcodeWidth, (int)_codeStringSize.Width);
            bcodeHeight += (_itemSepHeight + (int)_codeStringSize.Height);
        }
        // dispose temporary objects...
        objGraphics.Dispose();
        tmpBitmap.Dispose();
        return (new Bitmap(bcodeWidth, bcodeHeight, PixelFormat.Format32bppArgb));
    }
4

1 回答 1

0

您可以修改objGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, bcodeWidth, bcodeHeight));调用来源GenerateBarcode并使宽度更大。

var margin = VALUE NEEDED;
objGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(margin, 0, bcodeWidth + margin, bcodeHeight));
// Draw additiona text to the left
objGraphics.DrawString(ADDITIONAL_TEXT, _titleFont, new SolidBrush(Color.Black), new RectangleF(0, 0, margin, bcodeHeight)); //you can modify the rectangle area as needed
于 2017-07-13T08:01:52.350 回答