Search code examples
c#crystal-reports

Crystal Report image from byte array not printing


I have a Crystal Report with a class as a data source. I have a byte array which I am passing a bitmap to but it isn't printing on the Crystal Report. Please see my code below.

var d = new Label();
var eanCreator = new CreateEan();

var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
    bf.Serialize(ms, eanCreator.createBitmap(1.5f, "1234567890"));
    var byteArray = ms.ToArray();

    var ld = new LabelData
    {
        PartNumber = "123",
        EanData = byteArray
    };
    d.SetDataSource(new List<LabelData> {ld});

    d.PrintOptions.PrinterName = @"\\SERVER\Printer";
    d.PrintToPrinter(1, false, 0, 0);
}

The print comes out, all data except the image is present. I am using a class to create an EAN barcode, this part renders correctly to an image file, but just won't recognise it within Crystal Reports.


Solution

  • This method is similar to your code. I use this method all the time to send an image to Crystal Reports without problems.

    public static byte[] ConvertImageToByte(Image Value)
    {
        if (Value != null)
        {
            MemoryStream fs = new MemoryStream();
            ((Bitmap)Value).Save(fs, ImageFormat.Jpeg);          
            byte[] retval= fs.ToArray(); 
            fs.Dispose();
            return retval;
        }
        return null;
    }