Crystal Report image from byte array not printing

2019-07-27 03:18发布

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.

2条回答
我命由我不由天
2楼-- · 2019-07-27 03:37

To be able to show the image on the report, when you pull the image Field of your DataSet, it has to be IBlobFieldObject in CR. I had a lot of trouble achieving this. Eventually i removed the DataSet from the report (through 'Database expert') and re-added it. Also make sure, that your Image field is set to byte[] in the DataSet.

查看更多
甜甜的少女心
3楼-- · 2019-07-27 03:46

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;
}
查看更多
登录 后发表回答