作成日: 2024/04/10 最終更新日: 2024/05/10
文書種別
使用方法
詳細
セクションレポートの場合
GrapeCity.ActiveReports.Document.Drawing.ImageクラスのFromStreamメソッドで画像を読み込み、Pictureコントロールに設定することで、動的に画像を表示できます。Pictureを配置しているセクションのFormatイベントでPicture.Imageプロパティを設定する方法でも画像を表示できますが、画像のファイルパスがデータソースに含まれている場合などは、アンバウンドフィールドを経由して設定したほうが確実にデータと同期できます。基本的にはこちらの方法を推奨します。
◆サンプルコード(C#)
◆サンプルコード(C#)
private void SectionReport1_DataInitialize(object sender, EventArgs e)
{
// アンバウンドフィールドを追加する
this.Fields.Add("image1");
this.picture1.DataField = "image1";
}
private void SectionReport1_FetchData(object sender, FetchEventArgs eArgs)
{
// データフィールドから画像のファイルパスを取得する
var filepath = this.Fields["filepath"].Value.ToString();
// アンバウンドフィールドに画像を設定する
using (var fs = new FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
this.Fields["image1"].Value = GrapeCity.ActiveReports.Document.Drawing.Image.FromStream(fs);
}
}
◆サンプルコード(VB.NET)
Private Sub SectionReport1_DataInitialize(sender As Object, e As EventArgs) Handles MyBase.DataInitialize
' アンバウンドフィールドを追加する
Me.Fields.Add("image1")
Me.Picture1.DataField = "image1"
End Sub
Private Sub SectionReport1_FetchData(sender As Object, eArgs As FetchEventArgs) Handles MyBase.FetchData
' データフィールドから画像のファイルパスを取得する
Dim filepath As String = Me.Fields("filepath").Value.ToString()
' アンバウンドフィールドに画像を設定する
Using fs As New FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Me.Fields("image1").Value = GrapeCity.ActiveReports.Document.Drawing.Image.FromStream(fs)
End Using
End Sub
ページレポート/RDLレポートの場合
パラメータを経由して画像のバイト配列をレポートに渡すことで画像を動的に変更することができます。
◆Imageコントロールのプロパティ設定例
| Source | Database |
| Value | =Parameters!ReportParameter1.Value |
◆サンプルコード (C#)
byte[] bs;
using (var fs = new FileStream("画像ファイルパス", FileMode.Open, FileAccess.Read))
{
bs = new byte[fs.Length];
fs.Read(bs, 0, bs.Length);
}
var pRep = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo("PageReport1.rdlx"));
var pDoc = new GrapeCity.ActiveReports.Document.PageDocument(pRep);
pDoc.Parameters["ReportParameter1"].CurrentValue = bs;
this.viewer1.LoadDocument(pDoc);
◆サンプルコード (VB.NET)
Dim bs() As Byte
Using fs As New FileStream("画像ファイルパス", FileMode.Open, FileAccess.Read)
ReDim bs(fs.Length)
fs.Read(bs, 0, bs.Length)
End Using
Dim pRep As New GrapeCity.ActiveReports.PageReport(New System.IO.FileInfo("PageReport1.rdlx"))
Dim pDoc As New GrapeCity.ActiveReports.Document.PageDocument(pRep)
pDoc.Parameters("ReportParameter1").CurrentValue = bs
Me.Viewer1.LoadDocument(pDoc)