EchoTex_Payroll/HRM.BO/Common/BarcodeHelper.cs
2024-10-14 10:01:49 +06:00

99 lines
3.9 KiB
C#

using Ease.Core.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
namespace HRM.BO
{
public class BarcodeHelper
{
#region Private Functions
private static void CropImage(int x, int y, int width, int height, string barcodePath, string barcode)
{
string imagePath = barcodePath + barcode + ".png";
Bitmap croppedImage;
// Here we capture the resource - image file.
using (var originalImage = new Bitmap(imagePath))
{
Rectangle crop = new Rectangle(x, y, width, height);
// Here we capture another resource.
croppedImage = originalImage.Clone(crop, originalImage.PixelFormat);
} // Here we release the original resource - bitmap in memory and file on disk.
// At this point the file on disk already free - you can record to the same path.
croppedImage.Save(imagePath, ImageFormat.Png);
// It is desirable release this resource too.
croppedImage.Dispose();
}
#endregion
public static string GenerateBarcode(EnumBarcodeFor barcodeType, string employeeNo, int id)
{
if (employeeNo == null)
{
employeeNo = string.Empty;
}
return employeeNo.Trim().PadLeft(5, '0') + ((int)barcodeType).ToString().PadLeft(3, '0') + id.ToString().Trim().PadLeft(4, '0');
}
public static string GenerateBarcodeImage(string generatePath, string barcode)
{
try
{
//BarcodeSettings.ApplyKey("23423432");
//BarcodeSettings settings = new BarcodeSettings();
//settings.Type = BarCodeType.Codabar;
//settings.Unit = GraphicsUnit.Pixel;
//settings.ShowTextOnBottom = true;
//settings.ResolutionType = ResolutionType.UseDpi;
////input data
//settings.Data = barcode;
////string barcodeData = string.Empty;
////barcodeData = "Letter Type : " + lr.Purpose.ToString().Replace("_", " ") + "\n" + "Letter Issued For : " + SessionManager.CurrentEmployee.Name + "\n" + "Letter Issuance Date : " + DateTime.Now.ToString("dd MMM yyyy") + "\n" + "Letter Barcode Status : Verified";
////data = barcodeData;
////settings.Data = data;
////set fore color
//settings.ForeColor = Color.Black;
////set back color
//settings.BackColor = Color.White;
//settings.X = 1;// barWidth;
//settings.Y = 5;
//settings.BarHeight = 30;
//settings.QRCodeECL = QRCodeECL.L;
//BarCodeGenerator generator = new BarCodeGenerator(settings);
//System.Drawing.Image QRbarcode = generator.GenerateImage();
// QRbarcode.Save(Server.MapPath("~//LetterTempFolder//") + barcode + ".png", System.Drawing.Imaging.ImageFormat.Png);
string fullPath = generatePath + barcode + ".png";
if (File.Exists(fullPath))
{
File.Delete(fullPath);
}
//QRbarcode.Save(fullPath, System.Drawing.Imaging.ImageFormat.Png);
// CropImage(0, 14, 152, 50, generatePath, barcode);
return fullPath;
}
catch (Exception ex)
{
StringBuilder sb = new StringBuilder();
while (ex != null)
{
sb.AppendFormat("{0} -->", ex.Message);
ex = ex.InnerException;
}
throw new Exception(string.Format("Error generating Barcode. message: {0}", sb.ToString()));
}
}
}
}