利用数据集在水晶报表中显示图像
生活随笔
收集整理的這篇文章主要介紹了
利用数据集在水晶报表中显示图像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
描述 該 C# .NET Windows 程序演示了如何創建數據集,并將圖像添加到數據集,以及在運行時將數據集傳遞到子報表。
文件列表 - bin\Debug\Canada.jpg
- bin\Debug\Germany.jpg
- bin\Debug\Japan.jpg
- bin\Debug\USA.jpg
- App.ico
- AssemblyInfo.cs
- CrystalReport1.cs
- CrystalReport1.rpt
- DynamicImage.csproj
- DynamicImage.csproj.user
- DynamicImage.sln
- Form1.cs
- Form1.resx
- Readme.txt
- Steps.txt
步驟 * 開始一個新項目/創建數據集及其模式 - 新建項目
- 轉到 Form1.cs 后置的代碼
- Imports System.Data/System.IO
- 創建函數 "CreateData",以創建數據集: DataSet CreateData()
??? {
??????? DataSet data = new DataSet();
??????? data.Tables.Add("Images");
??????? data.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
??????? data.Tables[0].Columns.Add("img", System.Type.GetType("System.Byte[]"));
??????? data.WriteXmlSchema(Directory.GetCurrentDirectory() + "\\DynamicImage.xsd");
??? } - 創建函數 "CreateReport",以調用 CreateData 創建數據集模式: void CreateReport()
??? {
??????? CreateData();
??? } - 在構造函數中調用 CreateReport public Form1()
??? {
??????? //
??????? // Required for Windows Form Designer support
??????? //
??????? InitializeComponent(); //
??????? // TODO: Add any constructor code after InitializeComponent call
??????? //
??????? CreateReport();
??? } - 構造并執行程序/將在 Bin\Debug 文件夾中創建 DynamicImage.xsd。
* 設計報表 - 項目->添加新項
- 選擇 Crystal Report,并單擊“打開”
- 選擇“作為空白報表”,并單擊“確定”
- 右擊任意空白處,選擇“數據庫->添加/刪除數據庫”
- 展開 ODBC (RDO),選擇 Xtreme Sample Database,并單擊“完成”。
- 展開表,雙擊 Customer
- 單擊“確定”
- 將 Customer Name 和 Last Year's Sales 拖放到詳細資料
- 右擊任意空白處,插入->子報表
- 將子報表放置在 Last Year's Sales 旁邊
- 選擇“創建子報表”,并將子報表命名為 "Flags",單擊“報表專家”
- 擴展“更多數據源”,選擇 ADO.NET (XML)
- 找到 DynamicImage.xsd,并單擊“完成”
- 雙擊 Images
- 單擊“下一步”,雙擊 img,單擊“完成”
- 單擊“鏈接”選項卡
- 雙擊 Country,并單擊“確定”
- 調整子報表的大小
- 在子報表上雙擊,以打開子報表
- 刪除報表頁眉 b 和報表頁腳 b
- 右擊 -> 關閉子報表
* 回到代碼,并編寫 Crystal 代碼 - 將 CrystalReportViewer 控件拖放到窗體 Form1
- 選擇 CrystalReportViewer1,F4(屬性)
- 改變 Dock 屬性,填充
- 查看代碼
- 注釋掉 WriteXmlSchema(因為只在設計報表時需要數據集模式文件)
- 在 CreateData 函數中組裝數據集,并將其返回 void AddImageRow(DataTable tbl, string name, string filename)
??? {
??????? FileStream fs = new FileStream(filename, FileMode.Open);
??????? BinaryReader br = new BinaryReader(fs);
??????? DataRow row;
??????? row = tbl.NewRow();
??????? row[0] = name;
??????? row[1] = br.ReadBytes((int)br.BaseStream.Length);
??????? tbl.Rows.Add(row);
??????? br = null;
??????? fs = null;
??? } DataSet CreateData()
??? {
??????? DataSet data = new DataSet();
??????? data.Tables.Add("Images");
??????? data.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
??????? data.Tables[0].Columns.Add("img", System.Type.GetType("System.Byte[]"));
??????? //data.WriteXmlSchema(Directory.GetCurrentDirectory() + "\\DynamicImage.xsd");
??????? AddImageRow(data.Tables[0], "USA", Directory.GetCurrentDirectory() + "\\USA.jpg");
??????? AddImageRow(data.Tables[0], "Canada", Directory.GetCurrentDirectory() + "\\Canada.jpg");
??????? AddImageRow(data.Tables[0], "Germany", Directory.GetCurrentDirectory() + "\\Germany.jpg");
??????? AddImageRow(data.Tables[0], "Japan", Directory.GetCurrentDirectory() + "\\Japan.jpg");
??????? return (data);
??? }
- 創建報表文檔,將數據集傳遞到子報表,并將報表綁定到水晶報表查看器: void CreateReport()
??? {
??????? CrystalReport1 cr = new CrystalReport1();
??????? cr.OpenSubreport("Flags").SetDataSource(CreateData());
??????? crystalReportViewer1.ReportSource = cr;
??? } Form1.cs using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO; namespace DynamicImage
{
??????? /// <summary>
??????? /// Summary description for Form1.
??????? /// </summary>
??????? public class Form1 : System.Windows.Forms.Form
??????? {
??????????????? private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
??????????????? /// <summary>
??????????????? /// Required designer variable.
??????????????? /// </summary>
??????????????? private System.ComponentModel.Container components = null; // 過程: AddImageRow
??????????????? //?????? 讀取圖像文件,并將其添加到數據集的表中
??????????????? //
??????????????? //?? [in]??? tbl???????? 數據表
??????????????? //?????????? country???? 國家名
??????????????? //?????????? filename??? 圖像的文件名
??????????????? //
??????????????? void AddImageRow(DataTable tbl, string name, string filename)
??????????????? {
??????????????????????? FileStream fs = new FileStream(filename, FileMode.Open); // 創建文件流
??????????????????????? BinaryReader br = new BinaryReader(fs);??? // 創建二進制讀取器
??????????????????????? DataRow row; // 創建一個新的數據行
??????????????????????? row = tbl.NewRow(); // 設置 country 字段和 image 字段
??????????????????????? row[0] = name;
??????????????????????? row[1] = br.ReadBytes((int)br.BaseStream.Length); // 將數據行添加到表中
??????????????????????? tbl.Rows.Add(row); // 清除
??????????????????????? br = null;
??????????????????????? fs = null;
??????????????? } // 函數: CreateData
??????????????? //?????? 創建數據集,包含一個表,表有兩個字段:Country (string), 和 img (blob/byte[])
??????????????? //?????? 為表添加四條記錄
??????????????? //
??????????????? DataSet CreateData()
??????????????? {
??????????????????????? DataSet data = new DataSet(); // 將表 'Images' 添加到數據集
??????????????????????? data.Tables.Add("Images"); // 添加兩個字段
??????????????????????? data.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
??????????????????????? data.Tables[0].Columns.Add("img", System.Type.GetType("System.Byte[]")); // 創建數據集模式(該模式用于設計報表)
??????????????????????? // 報表創建以后,不再需要模式文件
??????????????????????? //data.WriteXmlSchema(Directory.GetCurrentDirectory() + "\\DynamicImage.xsd"); // 添加四行
??????????????????????? AddImageRow(data.Tables[0], "USA", Directory.GetCurrentDirectory() + "\\USA.jpg");
??????????????????????? AddImageRow(data.Tables[0], "Canada", Directory.GetCurrentDirectory() + "\\Canada.jpg");
??????????????????????? AddImageRow(data.Tables[0], "Germany", Directory.GetCurrentDirectory() + "\\Germany.jpg");
??????????????????????? AddImageRow(data.Tables[0], "Japan", Directory.GetCurrentDirectory() + "\\Japan.jpg"); return (data);
??????????????? } // 過程: CreateReport
??????????????? //?????? 創建報表,并傳遞數據集
??????????????? //
??????????????? void CreateReport()
??????????????? {
??????????????????????? // 創建報表
??????????????????????? CrystalReport1 cr = new CrystalReport1(); // 將數據集(通過調用函數 CreateData 創建的)傳遞到子報表 "Flags"
??????????????????????? cr.OpenSubreport("Flags").SetDataSource(CreateData()); // 將報表文檔傳遞到查看器
??????????????????????? crystalReportViewer1.ReportSource = cr;
??????????????? } public Form1()
??????????????? {
??????????????????????? //
??????????????????????? // Required for Windows Form Designer support
??????????????????????? //
??????????????????????? InitializeComponent(); //
??????????????????????? // TODO: Add any constructor code after InitializeComponent call
??????????????????????? //
??????????????????????? CreateReport();
??????????????? }
?
??????????????? 后面的部分省略……
文件列表 - bin\Debug\Canada.jpg
- bin\Debug\Germany.jpg
- bin\Debug\Japan.jpg
- bin\Debug\USA.jpg
- App.ico
- AssemblyInfo.cs
- CrystalReport1.cs
- CrystalReport1.rpt
- DynamicImage.csproj
- DynamicImage.csproj.user
- DynamicImage.sln
- Form1.cs
- Form1.resx
- Readme.txt
- Steps.txt
步驟 * 開始一個新項目/創建數據集及其模式 - 新建項目
- 轉到 Form1.cs 后置的代碼
- Imports System.Data/System.IO
- 創建函數 "CreateData",以創建數據集: DataSet CreateData()
??? {
??????? DataSet data = new DataSet();
??????? data.Tables.Add("Images");
??????? data.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
??????? data.Tables[0].Columns.Add("img", System.Type.GetType("System.Byte[]"));
??????? data.WriteXmlSchema(Directory.GetCurrentDirectory() + "\\DynamicImage.xsd");
??? } - 創建函數 "CreateReport",以調用 CreateData 創建數據集模式: void CreateReport()
??? {
??????? CreateData();
??? } - 在構造函數中調用 CreateReport public Form1()
??? {
??????? //
??????? // Required for Windows Form Designer support
??????? //
??????? InitializeComponent(); //
??????? // TODO: Add any constructor code after InitializeComponent call
??????? //
??????? CreateReport();
??? } - 構造并執行程序/將在 Bin\Debug 文件夾中創建 DynamicImage.xsd。
* 設計報表 - 項目->添加新項
- 選擇 Crystal Report,并單擊“打開”
- 選擇“作為空白報表”,并單擊“確定”
- 右擊任意空白處,選擇“數據庫->添加/刪除數據庫”
- 展開 ODBC (RDO),選擇 Xtreme Sample Database,并單擊“完成”。
- 展開表,雙擊 Customer
- 單擊“確定”
- 將 Customer Name 和 Last Year's Sales 拖放到詳細資料
- 右擊任意空白處,插入->子報表
- 將子報表放置在 Last Year's Sales 旁邊
- 選擇“創建子報表”,并將子報表命名為 "Flags",單擊“報表專家”
- 擴展“更多數據源”,選擇 ADO.NET (XML)
- 找到 DynamicImage.xsd,并單擊“完成”
- 雙擊 Images
- 單擊“下一步”,雙擊 img,單擊“完成”
- 單擊“鏈接”選項卡
- 雙擊 Country,并單擊“確定”
- 調整子報表的大小
- 在子報表上雙擊,以打開子報表
- 刪除報表頁眉 b 和報表頁腳 b
- 右擊 -> 關閉子報表
* 回到代碼,并編寫 Crystal 代碼 - 將 CrystalReportViewer 控件拖放到窗體 Form1
- 選擇 CrystalReportViewer1,F4(屬性)
- 改變 Dock 屬性,填充
- 查看代碼
- 注釋掉 WriteXmlSchema(因為只在設計報表時需要數據集模式文件)
- 在 CreateData 函數中組裝數據集,并將其返回 void AddImageRow(DataTable tbl, string name, string filename)
??? {
??????? FileStream fs = new FileStream(filename, FileMode.Open);
??????? BinaryReader br = new BinaryReader(fs);
??????? DataRow row;
??????? row = tbl.NewRow();
??????? row[0] = name;
??????? row[1] = br.ReadBytes((int)br.BaseStream.Length);
??????? tbl.Rows.Add(row);
??????? br = null;
??????? fs = null;
??? } DataSet CreateData()
??? {
??????? DataSet data = new DataSet();
??????? data.Tables.Add("Images");
??????? data.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
??????? data.Tables[0].Columns.Add("img", System.Type.GetType("System.Byte[]"));
??????? //data.WriteXmlSchema(Directory.GetCurrentDirectory() + "\\DynamicImage.xsd");
??????? AddImageRow(data.Tables[0], "USA", Directory.GetCurrentDirectory() + "\\USA.jpg");
??????? AddImageRow(data.Tables[0], "Canada", Directory.GetCurrentDirectory() + "\\Canada.jpg");
??????? AddImageRow(data.Tables[0], "Germany", Directory.GetCurrentDirectory() + "\\Germany.jpg");
??????? AddImageRow(data.Tables[0], "Japan", Directory.GetCurrentDirectory() + "\\Japan.jpg");
??????? return (data);
??? }
- 創建報表文檔,將數據集傳遞到子報表,并將報表綁定到水晶報表查看器: void CreateReport()
??? {
??????? CrystalReport1 cr = new CrystalReport1();
??????? cr.OpenSubreport("Flags").SetDataSource(CreateData());
??????? crystalReportViewer1.ReportSource = cr;
??? } Form1.cs using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO; namespace DynamicImage
{
??????? /// <summary>
??????? /// Summary description for Form1.
??????? /// </summary>
??????? public class Form1 : System.Windows.Forms.Form
??????? {
??????????????? private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
??????????????? /// <summary>
??????????????? /// Required designer variable.
??????????????? /// </summary>
??????????????? private System.ComponentModel.Container components = null; // 過程: AddImageRow
??????????????? //?????? 讀取圖像文件,并將其添加到數據集的表中
??????????????? //
??????????????? //?? [in]??? tbl???????? 數據表
??????????????? //?????????? country???? 國家名
??????????????? //?????????? filename??? 圖像的文件名
??????????????? //
??????????????? void AddImageRow(DataTable tbl, string name, string filename)
??????????????? {
??????????????????????? FileStream fs = new FileStream(filename, FileMode.Open); // 創建文件流
??????????????????????? BinaryReader br = new BinaryReader(fs);??? // 創建二進制讀取器
??????????????????????? DataRow row; // 創建一個新的數據行
??????????????????????? row = tbl.NewRow(); // 設置 country 字段和 image 字段
??????????????????????? row[0] = name;
??????????????????????? row[1] = br.ReadBytes((int)br.BaseStream.Length); // 將數據行添加到表中
??????????????????????? tbl.Rows.Add(row); // 清除
??????????????????????? br = null;
??????????????????????? fs = null;
??????????????? } // 函數: CreateData
??????????????? //?????? 創建數據集,包含一個表,表有兩個字段:Country (string), 和 img (blob/byte[])
??????????????? //?????? 為表添加四條記錄
??????????????? //
??????????????? DataSet CreateData()
??????????????? {
??????????????????????? DataSet data = new DataSet(); // 將表 'Images' 添加到數據集
??????????????????????? data.Tables.Add("Images"); // 添加兩個字段
??????????????????????? data.Tables[0].Columns.Add("Country", System.Type.GetType("System.String"));
??????????????????????? data.Tables[0].Columns.Add("img", System.Type.GetType("System.Byte[]")); // 創建數據集模式(該模式用于設計報表)
??????????????????????? // 報表創建以后,不再需要模式文件
??????????????????????? //data.WriteXmlSchema(Directory.GetCurrentDirectory() + "\\DynamicImage.xsd"); // 添加四行
??????????????????????? AddImageRow(data.Tables[0], "USA", Directory.GetCurrentDirectory() + "\\USA.jpg");
??????????????????????? AddImageRow(data.Tables[0], "Canada", Directory.GetCurrentDirectory() + "\\Canada.jpg");
??????????????????????? AddImageRow(data.Tables[0], "Germany", Directory.GetCurrentDirectory() + "\\Germany.jpg");
??????????????????????? AddImageRow(data.Tables[0], "Japan", Directory.GetCurrentDirectory() + "\\Japan.jpg"); return (data);
??????????????? } // 過程: CreateReport
??????????????? //?????? 創建報表,并傳遞數據集
??????????????? //
??????????????? void CreateReport()
??????????????? {
??????????????????????? // 創建報表
??????????????????????? CrystalReport1 cr = new CrystalReport1(); // 將數據集(通過調用函數 CreateData 創建的)傳遞到子報表 "Flags"
??????????????????????? cr.OpenSubreport("Flags").SetDataSource(CreateData()); // 將報表文檔傳遞到查看器
??????????????????????? crystalReportViewer1.ReportSource = cr;
??????????????? } public Form1()
??????????????? {
??????????????????????? //
??????????????????????? // Required for Windows Form Designer support
??????????????????????? //
??????????????????????? InitializeComponent(); //
??????????????????????? // TODO: Add any constructor code after InitializeComponent call
??????????????????????? //
??????????????????????? CreateReport();
??????????????? }
?
??????????????? 后面的部分省略……
轉載于:https://www.cnblogs.com/freetest/archive/2006/08/10/472872.html
總結
以上是生活随笔為你收集整理的利用数据集在水晶报表中显示图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 再发:VS怎么会这个样子的?
- 下一篇: 上海Oracle高峰会感悟