using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.RazorPages;using IronBarCode;public class IndexModel : PageModel{ [BindProperty] public IFormFile? UploadedFile { get; set; } public string? BarcodeResult { get; set; } public string? ErrorMessage { get; set; } public string? ImageDataUrl { get; set; } public voidOnGet() { } public async Task<IActionResult> OnPostUploadAsync() { if (UploadedFile == null || UploadedFile.Length == 0) {ErrorMessage = "Please select an image file."; returnPage(); } try { using var ms = new MemoryStream(); awaitUploadedFile.CopyToAsync(ms); byte[] imageBytes = ms.ToArray(); // Store image as base64 for preview display string base64 = Convert.ToBase64String(imageBytes);ImageDataUrl = $"data:{UploadedFile.ContentType};base64,{base64}"; // Read barcode from uploaded image bytes var results = BarcodeReader.Read(imageBytes); if (results != null && results.Count() > 0) {BarcodeResult = string.Join("\n", results.Select(r => r.Value)); } else {ErrorMessage = "No barcode detected in the uploaded image."; } } catch (Exception ex) {ErrorMessage = $"Error processing image: {ex.Message}"; } returnPage(); }}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using IronBarCode;
public class IndexModel : PageModel
{
[BindProperty]
public IFormFile? UploadedFile { get; set; }
public string? BarcodeResult { get; set; }
public string? ErrorMessage { get; set; }
public string? ImageDataUrl { get; set; }
public void OnGet()
{
}
public async Task<IActionResult> OnPostUploadAsync()
{
if (UploadedFile == null || UploadedFile.Length == 0)
{
ErrorMessage = "Please select an image file.";
return Page();
}
try
{
using var ms = new MemoryStream();
await UploadedFile.CopyToAsync(ms);
byte[] imageBytes = ms.ToArray();
// Store image as base64 for preview display
string base64 = Convert.ToBase64String(imageBytes);
ImageDataUrl = $"data:{UploadedFile.ContentType};base64,{base64}";
// Read barcode from uploaded image bytes
var results = BarcodeReader.Read(imageBytes);
if (results != null && results.Count() > 0)
{
BarcodeResult = string.Join("\n",
results.Select(r => r.Value));
}
else
{
ErrorMessage = "No barcode detected in the uploaded image.";
}
}
catch (Exception ex)
{
ErrorMessage = $"Error processing image: {ex.Message}";
}
return Page();
}
}
ImportsMicrosoft.AspNetCore.MvcImportsMicrosoft.AspNetCore.Mvc.RazorPagesImportsIronBarCodeImportsSystem.IOImportsSystem.Threading.TasksPublic Class IndexModelInheritsPageModel <BindProperty> Public Property UploadedFileAsIFormFile Public Property BarcodeResultAsString Public Property ErrorMessageAsString Public Property ImageDataUrlAsString Public Sub OnGet() End Sub PublicAsync Function OnPostUploadAsync() AsTask(OfIActionResult) IfUploadedFileIs NothingOrElseUploadedFile.Length = 0 ThenErrorMessage = "Please select an image file." ReturnPage() End IfTryUsing ms As New MemoryStream()AwaitUploadedFile.CopyToAsync(ms) Dim imageBytes AsByte() = ms.ToArray() ' Store image as base64 for preview display Dim base64 AsString = Convert.ToBase64String(imageBytes)ImageDataUrl = $"data:{UploadedFile.ContentType};base64,{base64}" ' Read barcode from uploaded image bytes Dim results = BarcodeReader.Read(imageBytes) If results IsNot NothingAndAlso results.Count() > 0 ThenBarcodeResult = String.Join(vbLf, results.Select(Function(r) r.Value)) ElseErrorMessage = "No barcode detected in the uploaded image." End IfEndUsingCatch ex AsExceptionErrorMessage = $"Error processing image: {ex.Message}"EndTry ReturnPage() End FunctionEnd Class
Imports Microsoft.AspNetCore.Mvc
Imports Microsoft.AspNetCore.Mvc.RazorPages
Imports IronBarCode
Imports System.IO
Imports System.Threading.Tasks
Public Class IndexModel
Inherits PageModel
<BindProperty>
Public Property UploadedFile As IFormFile
Public Property BarcodeResult As String
Public Property ErrorMessage As String
Public Property ImageDataUrl As String
Public Sub OnGet()
End Sub
Public Async Function OnPostUploadAsync() As Task(Of IActionResult)
If UploadedFile Is Nothing OrElse UploadedFile.Length = 0 Then
ErrorMessage = "Please select an image file."
Return Page()
End If
Try
Using ms As New MemoryStream()
Await UploadedFile.CopyToAsync(ms)
Dim imageBytes As Byte() = ms.ToArray()
' Store image as base64 for preview display
Dim base64 As String = Convert.ToBase64String(imageBytes)
ImageDataUrl = $"data:{UploadedFile.ContentType};base64,{base64}"
' Read barcode from uploaded image bytes
Dim results = BarcodeReader.Read(imageBytes)
If results IsNot Nothing AndAlso results.Count() > 0 Then
BarcodeResult = String.Join(vbLf, results.Select(Function(r) r.Value))
Else
ErrorMessage = "No barcode detected in the uploaded image."
End If
End Using
Catch ex As Exception
ErrorMessage = $"Error processing image: {ex.Message}"
End Try
Return Page()
End Function
End Class
public stringReadBarCode(string imageDataBase64){ // Decode the base64 image data var splitObject = imageDataBase64.Split(','); byte[] imageByteData = Convert.FromBase64String( (splitObject.Length > 1) ? splitObject[1] : splitObject[0]); // Read barcode directly from byte array var results = BarcodeReader.Read(imageByteData); return $"{DateTime.Now}: Barcode is ({results.First().Value})";}
public string ReadBarCode(string imageDataBase64)
{
// Decode the base64 image data
var splitObject = imageDataBase64.Split(',');
byte[] imageByteData = Convert.FromBase64String(
(splitObject.Length > 1) ? splitObject[1] : splitObject[0]);
// Read barcode directly from byte array
var results = BarcodeReader.Read(imageByteData);
return $"{DateTime.Now}: Barcode is ({results.First().Value})";
}
Public Function ReadBarCode(imageDataBase64 AsString) AsString ' Decode the base64 image data Dim splitObject = imageDataBase64.Split(","c) Dim imageByteData AsByte() = Convert.FromBase64String( If(splitObject.Length > 1, splitObject(1), splitObject(0))) ' Read barcode directly from byte array Dim results = BarcodeReader.Read(imageByteData) Return $"{DateTime.Now}: Barcode is ({results.First().Value})"End Function
Public Function ReadBarCode(imageDataBase64 As String) As String
' Decode the base64 image data
Dim splitObject = imageDataBase64.Split(","c)
Dim imageByteData As Byte() = Convert.FromBase64String(
If(splitObject.Length > 1, splitObject(1), splitObject(0)))
' Read barcode directly from byte array
Dim results = BarcodeReader.Read(imageByteData)
Return $"{DateTime.Now}: Barcode is ({results.First().Value})"
End Function
public IActionResultOnPostGenerate(){ var barcode = BarcodeWriter.CreateBarcode( "https://ironsoftware.com", BarcodeEncoding.QRCode); byte[] barcodeBytes = barcode.ToPngBinaryData(); returnFile(barcodeBytes, "image/png", "generated-barcode.png");}
public IActionResult OnPostGenerate()
{
var barcode = BarcodeWriter.CreateBarcode(
"https://ironsoftware.com", BarcodeEncoding.QRCode);
byte[] barcodeBytes = barcode.ToPngBinaryData();
return File(barcodeBytes, "image/png", "generated-barcode.png");
}
Public Function OnPostGenerate() AsIActionResult Dim barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com", BarcodeEncoding.QRCode) Dim barcodeBytes AsByte() = barcode.ToPngBinaryData() ReturnFile(barcodeBytes, "image/png", "generated-barcode.png")End Function
Public Function OnPostGenerate() As IActionResult
Dim barcode = BarcodeWriter.CreateBarcode("https://ironsoftware.com", BarcodeEncoding.QRCode)
Dim barcodeBytes As Byte() = barcode.ToPngBinaryData()
Return File(barcodeBytes, "image/png", "generated-barcode.png")
End Function