A FileResult sends generated file or binary content in an HTTP response. In an ASP.NET Core endpoint, Results.File accepts encoded bytes and a content type. section 3.1.2
Returning a barcode image
A route can generate a barcode and return it as JPEG data: app.MapGet("/barcode/{text}", (string text) => { var barcode = new Barcode(); barcode.ImageFormat = ImageFormat.Jpeg; _ = barcode.Encode(TYPE.CODE128, text, Color.Black, Color.White); return Results.File(barcode.Encoded_Image_Bytes, "image/jpeg"); }); The content type must match the generated image format, and barcode input restrictions may require validation. section 3.1.2
using BarcodeLib; #1
using System.Drawing; #2
using System.Drawing.Imaging; #3
app.MapGet("/barcode/{text}", (string text) => { #4
var barcode = new Barcode(); #5
barcode.ImageFormat = ImageFormat.Jpeg; #6
_ = barcode.Encode( #7
TYPE.CODE128, #8
text, #9
Color.Black, #10
Color.White); #11
return Results.File( #12
barcode.Encoded_Image_Bytes, #13
"image/jpeg"); #14
});
The listing shows the complete endpoint flow: configure BarcodeLib, encode request text, and return image bytes with an image/jpeg content type. section 3.1.2
