第一步:创建一个类,该类的结构来自数据集、数据表或其他,实例化类时赋值(图片返回用二进制流)
class Class2
{
private string m_name;
private System.Drawing.Image m_pic;
public Class2(string name, System.Drawing.Image pic)
{
m_name = name;
m_pic = pic;
}
public string Name
{
get
{
return m_name;
}
}
public byte[] Pic
{
get
{
//二进制流
System.IO.MemoryStream ms = new System.IO.MemoryStream();
m_pic.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.GetBuffer();
//反向
//MemoryStream ms = new MemoryStream(byt);
//Image img = Image.FromStream(ms);
}
}
}
第二步:创建数据集,菜单-》数据-》添加新数据集-》对象-》(选择对象)
【数据集,布局完成后可删除】
第三步:从工程中添加-》添加-》添加新建项-》报表
用新建的数据集,布局,填充报表(图片设置属性Source为Database,MIMEType为image/jpeg)
第四步:从工具箱创建“BindingSource”,在下一步中设置属性Class2BindingSource.DataSource
第五步:在事件中添加代码
Class2 print = new Class2("汽车",pictureBox2.Image );
//this.Class2BindingSource.DataSource = typeof(Test0.Class2);
this.reportViewer1.LocalReport.EnableExternalImages = true; //打开图片支持
this.reportViewer1.LocalReport.ReportEmbeddedResource = @"Test0.Report1.rdlc"; //加载报表
this.reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
Class2BindingSource.DataSource = print;
this.reportViewer1.ZoomMode = ZoomMode.Percent;
this.reportViewer1.ZoomPercent = 100;
第六步:类列表(集合)生成与打印
1、修改数据源对象
List<Class2> print = new List<Class2>();
print.Add(new Class2("汽车", 10000, pictureBox1.Image, MyTab));
print.Add(new Class2("火车", 10000, pictureBox1.Image, MyTab));
print.Add(new Class2("飞机", 10000, pictureBox1.Image, MyTab));
print.Add(new Class2("轮船", 10000, pictureBox1.Image, MyTab));
this.Class2BindingSource.DataSource = typeof(List<Test0.Class2>);
2、在Test0.Report1.rdlc中添加表格,添加字段