生成的图像未在报告打印中显示



我正在尝试使用rdlc在报告上打印出生成的条形码,但我得到的只是一个小的红色x。

这是我的对象。

    [XmlType]
[XmlRoot(IsNullable = false)]
[Serializable]
public class Voucher
{
    [XmlIgnore]
    public Bitmap BarcodeImg { get; set; }
    [XmlElement("BarcodeImg")]
    public byte[] BarcodeImgSerialized
    {
        get
        {
            if (BarcodeImg == null) return new byte[] { };
            using (var Stream = new MemoryStream())
            {
                BarcodeImg.Save(Stream, ImageFormat.Bmp);
                return Stream.ToArray();
            }
        }
        set
        {
            if (value == null) { BarcodeImg = new Bitmap(50, 50); }
            else
            {
                using (var Stream = new MemoryStream(value))
                {
                    BarcodeImg = new Bitmap(Stream);
                }
            }
        }
    }
    public string BarcodeStr { get; set; } = string.Empty;
    public Voucher() { }
}

在我的报告中,称为" report_voucher",我将凭单对象列为数据集。我的图像是名称"条形码",模拟型为图像/BMP,外部图像源,表达式为

=First(Fields!BarcodeImg.Value, "Voucher")

我还尝试了barcodeimgserialization。

这是我的表单代码...

public partial class Form_Main : Form
{
    LocalReport Report;
    List<Stream> Streams = new List<Stream>();
    public Form_Main()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        Voucher V = CreateVoucher();
        DataSet Set = CreateDataSet(V);
        this.RV_Main.ProcessingMode = ProcessingMode.Local;
        this.Report = RV_Main.LocalReport;
        Report.ReportPath = @"....Report_Voucher.rdlc";
        Report.DataSources.Add(new ReportDataSource("Voucher", Set.Tables[0]));
        var DeviceInfo =
            @"<DeviceInfo>
                <OutputFormat>EMF</OutputFormat>
                <PageWidth>8.5in</PageWidth>
                <PageHeight>11in</PageHeight>
                <MarginTop>0.25in</MarginTop>
                <MarginLeft>0.25in</MarginLeft>
                <MarginRight>0.25in</MarginRight>
                <MarginBottom>0.25in</MarginBottom>
            </DeviceInfo>";
        var Warnings = new Warning[] { };
        Report.EnableExternalImages = true;
        Report.Render("Image", DeviceInfo, CreateStream, out Warnings);
        Streams.ForEach(x => x.Position = 0);
        this.RV_Main.Refresh();
    }
    private DataSet CreateDataSet(Voucher Voucher)
    {
        var Set = new DataSet();
        var Serializer = new XmlSerializer(typeof(Voucher));
        using (var MStream = new MemoryStream())
        {
            Serializer.Serialize(MStream, Voucher);
            MStream.Seek(0, SeekOrigin.Begin);
            Set.ReadXml(MStream);
        }
        return Set;
    }
    private Voucher CreateVoucher()
    {
        var BarcodeStr = "12345678901234";
        var Writer = new BarcodeWriter
        {
            Format = BarcodeFormat.ITF,
            Options = new EncodingOptions
            {
                Height = 80,
                Width = 400,
                Margin = 0,
                PureBarcode = true,
            }
        };
        var Barcode = Writer.Write(BarcodeStr);
        var Voucher = new Voucher()
        {
            BarcodeStr = BarcodeStr,
            BarcodeImg = Barcode,
        };
        return Voucher;
    }
    private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
        var stream = new MemoryStream();
        Streams.Add(stream);
        return stream;
    }
    private void Btn_PrintReport_Click(object sender, EventArgs e)
    {
        if (this.Streams == null || this.Streams.Count == 0) { return; }
        var Document = new PrintDocument();
        Document.DefaultPageSettings.Landscape = true;
        if (!Document.PrinterSettings.IsValid) { return; }
        else
        {
            Document.PrintPage += Document_PrintPage;
            Document.Print();
        }
    }
    private void Document_PrintPage(object sender, PrintPageEventArgs e)
    {
        var Stream = this.Streams.First();
        var Page = new Metafile(Stream);          
        var AdjustedRect = new Rectangle(
            e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
            e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
            e.PageBounds.Width,
            e.PageBounds.Height);
        e.Graphics.FillRectangle(Brushes.White, AdjustedRect);
        e.Graphics.DrawImage(Page, AdjustedRect);
        Stream.Dispose();
        Page.Dispose();
    }
}

我已经尝试了令人讨厌的调整,以尝试使此条形码显示,从序列化完成,报告将其捕获为图像的变量(字节数组,对象,位图)等。,我读过的任何东西似乎是一个解决方案。有人知道这个修复吗?

解决方案是将表达式包裹在frombase64string呼叫中:

=System.Convert.FromBase64String(First(Fields!BarcodeImg.Value, "Voucher"));

最新更新