【问题标题】:Add margin to barcode image using C#使用 C# 为条形码图像添加边距
【发布时间】:2017-07-13 07:44:01
【问题描述】:

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

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

I created it.

但应该是这样的。

It should be like this.

代码很复杂。

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

    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));
    }

【问题讨论】:

    标签: c# barcode code39


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 2015-01-29
      • 2013-03-03
      • 2014-08-22
      • 1970-01-01
      相关资源
      最近更新 更多