AnyBitmap to System.Drawing.Bitmap in .NET Framework
En una aplicación de escritorio .NET Framework como WinForms, asignar un IronBarcode AnyBitmap directamente a un System.Drawing.Image o Bitmap falla en tiempo de compilación. Un caso común es enlazar un código de barras generado a un PictureBox, que solo acepta objetos System.Drawing.Image.
Cannot implicitly convert type 'IronSoftware.Drawing.AnyBitmap' to 'System.Drawing.Image'
El código a continuación provoca el error:
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
// These lines cause compile-time errors
Image myBarcodeImage = myBarcode.Image;
Bitmap myBarcodeBitmap = myBarcode.ToBitmap();
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
// These lines cause compile-time errors
Image myBarcodeImage = myBarcode.Image;
Bitmap myBarcodeBitmap = myBarcode.ToBitmap();
Dim myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8)
' These lines cause compile-time errors
Dim myBarcodeImage As Image = myBarcode.Image
Dim myBarcodeBitmap As Bitmap = myBarcode.ToBitmap()
Tanto BarcodeWriter.Image como .ToBitmap() devuelven un IronSoftware.Drawing.AnyBitmap. Ese tipo sí admite la conversión implícita a System.Drawing.Bitmap o Image, pero solo en .NET 6, .NET 7, .NET 8 o versiones superiores. En .NET Framework, los operadores implícitos y el puenteo de tipos internos no siempre se resuelven, especialmente cuando los tipos provienen de diferentes bibliotecas o capas de compatibilidad.
Solución
Option 1: Install System.Drawing.Common
Agregue el paquete NuGet System.Drawing.Common para que los operadores de conversión se resuelvan contra un ensamblado de dibujo consistente.
.NET CLI:
dotnet add package System.Drawing.Common
dotnet add package System.Drawing.Common
Consola del Administrador de Paquetes:
Install-Package System.Drawing.Common
Install-Package System.Drawing.Common
SDK-style .csproj:
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.0" />
Opción 2: Convertir a través de un flujo
Cuando prefiera no cambiar las referencias de paquetes, convierta el bitmap a través de un flujo compatible con GDI y entregue el resultado al PictureBox:
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
myBarcode.ResizeTo(400, 100); // Optional resizing
using (var ms = myBarcode.ToWindowsBitmapStream())
{
pictureBox1.Image = Image.FromStream(ms); // Works in .NET Framework
}
var myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8);
myBarcode.ResizeTo(400, 100); // Optional resizing
using (var ms = myBarcode.ToWindowsBitmapStream())
{
pictureBox1.Image = Image.FromStream(ms); // Works in .NET Framework
}
Dim myBarcode = BarcodeWriter.CreateBarcode("12345", BarcodeWriterEncoding.EAN8)
myBarcode.ResizeTo(400, 100) ' Optional resizing
Using ms = myBarcode.ToWindowsBitmapStream()
pictureBox1.Image = Image.FromStream(ms) ' Works in .NET Framework
End Using
ToWindowsBitmapStream() escribe el AnyBitmap en un formato de imagen compatible con GDI, y Image.FromStream analiza ese flujo en un System.Drawing.Image nativo.

