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

时间:2017-12-01 20:15:22

标签: c# image printing rdlc

我正在尝试使用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”的报告中,我将Voucher对象列为数据集。我的图像名称为“条形码”,MIMEType为图像/ BMP,外部图像源,表达式为

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

我也尝试过BarcodeImgSerialized。

这是我的表单代码......

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();
    }
}

我尝试了一些令人讨厌的调整,试图让这个条形码出现,从序列化的方式,报告捕获的图像变量(字节数组,对象,位图)等。我读过的任何内容似乎都没有解决。有谁知道修复此问题?

1 个答案:

答案 0 :(得分:0)

解决方案是将表达式包装在FromBase64String调用中:

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