iTextSharp.text.pdf.Barcode128.CreateDrawingImage C# (CSharp) Method

CreateDrawingImage() public method

public CreateDrawingImage ( System foreground, System background ) : System.Drawing.Image
foreground System
background System
return System.Drawing.Image
        public override System.Drawing.Image CreateDrawingImage(System.Drawing.Color foreground, System.Drawing.Color background) {
            String bCode;
            if (codeType == CODE128_RAW) {
                int idx = code.IndexOf('\uffff');
                if (idx >= 0)
                    bCode = code.Substring(0, idx);
                else
                    bCode = code;
            }
            else {
                bCode = GetRawText(code, codeType == CODE128_UCC);
            }
            int len = bCode.Length;
            int fullWidth = (len + 2) * 11 + 2;
            byte[] bars = GetBarsCode128Raw(bCode);
            int height = (int)barHeight;
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(fullWidth, height);
            for (int h = 0; h < height; ++h) {
                bool print = true;
                int ptr = 0;
                for (int k = 0; k < bars.Length; ++k) {
                    int w = bars[k];
                    System.Drawing.Color c = background;
                    if (print)
                        c = foreground;
                    print = !print;
                    for (int j = 0; j < w; ++j)
                        bmp.SetPixel(ptr++, h, c);
                }
            }
            return bmp;
        }

Usage Example

Ejemplo n.º 1
2
    /// <summary>
    /// this version outputs gif to web page
    /// ProcessRequest is intrinsic function of hppthandler, do not change the name
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest(HttpContext context)
    {
        //for a custom httphandler make sure it's referenced in web.config in httpHandlers
        //<add verb="GET" path="*barcode.gif" type="barcode_handler" validate ="false" />
        //
        if(context.Request["code"] != null){

            string _code = wwi_security.DecryptString(context.Request["code"].ToString(),"publiship"); //code to use
            int _wd = 120; //context.Request["wd"] != null ? wwi_func.vint(context.Request["wd"].ToString()) : 120; //width
            int _ht = 30; //context.Request["ht"] != null ? wwi_func.vint(context.Request["ht"].ToString()) : 30; //height

            Barcode128 _bc = new Barcode128();
            _bc.CodeType = Barcode.CODE128;
            _bc.ChecksumText = true;
            _bc.GenerateChecksum = true;
            _bc.Code = _code;

            //draws directly to web page with no code underneath bar
            //System.Drawing.Bitmap _bm = new System.Drawing.Bitmap(_bc.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White));
            //_bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

            //draws with actual code underneath bar
            //create bitmap from System.Drawing library, add some height for actual code underneath
            Bitmap _bm = new Bitmap(_wd, _ht + 10);
            //provide this, else the background will be black by default
            Graphics _gr = Graphics.FromImage(_bm);
            
            _gr.PageUnit = GraphicsUnit.Pixel;
            _gr.Clear(Color.White); 
            //draw the barcode
            _gr.DrawImage(_bc.CreateDrawingImage(Color.Black, System.Drawing.Color.White), new Point(0,0));
            //place text underneath - if you want the placement to be dynamic, calculate the point based on size of the image
            System.Drawing.Font _ft = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
            SizeF _sz = _gr.MeasureString(_code, _ft); 
            //position text
            _wd = (_wd - (int)_sz.Width) / 2;
            
            StringFormat _sf = new StringFormat();
            _sf.Alignment = StringAlignment.Center;
            _sf.LineAlignment = StringAlignment.Center;

            _gr.DrawString(_code, _ft, SystemBrushes.WindowText,_wd,_ht, _sf);
            //output as gif to web page,  can also save it to external file
            _bm.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
        }//end if
    }
All Usage Examples Of iTextSharp.text.pdf.Barcode128::CreateDrawingImage