Ejemplo n.º 1
0
        public static string Barcode(string strImgID, Int64 iformat, string strSessionID, ref string strResult)
        {
            try
            {
                if (strImgID == null || strImgID.Trim() == "")
                {
                    throw new Exception("No barcode exist.");
                }
                Bitmap _bitmap = BarcodeMode.DoBarcode(strImgID, iformat, strSessionID, ref strResult);

                string strFileName = BarcodeAccess.GetNextFileIndex(strImgID, strSessionID);
                string strFullPath = BarcodeAccess.GetUploadFolder() + System.IO.Path.DirectorySeparatorChar + strSessionID + System.IO.Path.DirectorySeparatorChar + strFileName;

                _bitmap.Save(strFullPath, GetImageFormat(strImgID));

                if (_bitmap != null)
                {
                    _bitmap.Dispose();
                }
                return("Images/Upload/" + strSessionID + "/" + strFileName);
            }
            catch (Exception exp)
            {
                if (exp.Message.Contains("No barcode found.") == true)
                {
                    return(CreateError(strImgID, "No barcode found.", strSessionID) + ";" + exp.Message);
                }
                else
                {
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
 private static string CreateError(string strImgID, string strErrorMessage, string strSessionID)
 {
     try
     {
         return(BarcodeMode.CreateErrorInner(strImgID, strErrorMessage, strSessionID));
     }
     catch (Exception)
     {
         return(BarcodeMode.CreateErrorInner(null, strErrorMessage, strSessionID));
     }
 }
Ejemplo n.º 3
0
        private static string CreateErrorInner(string strImgID, string strErrorMessage, string strSessionID)
        {
            Bitmap _bitmap     = BarcodeMode.CreateErrorImg(strImgID, "No barcode found.", strSessionID);
            string strFileName = BarcodeAccess.GetNextFileIndex(strImgID, strSessionID);
            string strFullPath = BarcodeAccess.GetUploadFolder() + System.IO.Path.DirectorySeparatorChar + strSessionID + System.IO.Path.DirectorySeparatorChar + strFileName;

            _bitmap.Save(strFullPath, GetImageFormat(strImgID));
            if (_bitmap != null)
            {
                _bitmap.Dispose();
            }
            return("Images/Upload/" + strSessionID + "/" + strFileName);
        }
Ejemplo n.º 4
0
        private static Bitmap DoBarcode(string strImgID, Int64 format, string strSessionID, ref string strResult)
        {
            strResult = "";
            using (Bitmap bitmap = new Bitmap(BarcodeAccess.GetImgPathByName(strImgID, strSessionID)))
            {
                DateTime beginTime = DateTime.Now;

                BarcodeResult[] listResult = BarcodeMode.GetBarcode(bitmap, format, strSessionID);
                DateTime        afterTime  = DateTime.Now;
                TimeSpan        midTime    = afterTime - beginTime;
                if (listResult == null || listResult.Length == 0)
                {
                    throw new BarcodeException("No barcode found. Total time spent: " + midTime.TotalSeconds.ToString("F3") + " seconds.<br/>");
                }
                strResult = "Total barcode(s) found: " + listResult.Length + ". Total time spent: " + midTime.TotalSeconds.ToString("F3") + " seconds.<br/><br/>";
                Bitmap _bitmap = new Bitmap(bitmap.Width, bitmap.Height);
                using (Graphics g = Graphics.FromImage(_bitmap))
                {
                    g.DrawImage(bitmap, new Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, bitmap.Width, bitmap.Height, GraphicsUnit.Pixel);
                    float fsize = bitmap.Width / 80f;
                    if (fsize < 12)
                    {
                        fsize = 12;
                    }
                    using (Font font = new Font("Times New Roman", fsize, FontStyle.Bold))
                    {
                        int i = 1;
                        foreach (BarcodeResult Item in listResult)
                        {
                            strResult = strResult + "&nbsp&nbspBarcode " + i + ":<br/>";
                            strResult = strResult + "&nbsp&nbsp&nbsp&nbspPage: 1" + "<br/>";
                            strResult = strResult + "&nbsp&nbsp&nbsp&nbspType: " + Item.BarcodeFormat + "<br/>";
                            strResult = strResult + "&nbsp&nbsp&nbsp&nbspValue: " + Item.BarcodeText + "<br/>";
                            strResult = strResult + "&nbsp&nbsp&nbsp&nbspRegion: {left: " + Item.ResultPoints[0].X
                                        + ", Top: " + Item.ResultPoints[0].Y
                                        + ", Width: " + Item.BoundingRect.Width
                                        + ", Height: " + Item.BoundingRect.Height + "}<br/><br/>";
                            List <Point> listPoint = new List <Point>();
                            PointF       tmp       = new PointF(00, 40);
                            if (Item.ResultPoints.Length > 0)
                            {
                                tmp.X = (float)Item.ResultPoints[0].X;
                                tmp.Y = (float)Item.ResultPoints[0].Y;
                            }
                            SizeF sizeFT = g.MeasureString("[" + i + "]" + Item.BarcodeText, font);
                            if (tmp.Y + sizeFT.Height > _bitmap.Height)
                            {
                                tmp.Y = tmp.Y - 30 > 0 ? tmp.Y - 30 : 0;
                            }
                            if (sizeFT.Width + tmp.X > _bitmap.Width)
                            {
                                tmp.X = 0;
                            }
                            g.FillRectangle(Brushes.White, new RectangleF(new PointF(tmp.X, tmp.Y), sizeFT));
                            g.DrawString("[" + i + "]" + Item.BarcodeText, font, Brushes.Red, tmp.X, tmp.Y);

                            i++;
                        }
                    }
                }
                return(_bitmap);
            }
        }