作成日: 2020/04/15 最終更新日: 2021/08/20
文書種別
使用方法
詳細
レポート上に表示する画像を動的に変更する方法は、レポートの形式によって異なります。
詳細については、製品ヘルプの以下のトピックをご参照ください。
よくある質問 - セクションレポート - レイアウト - 「画像を動的に読み込む」
なお、DetailのFormatイベントでPicture.Imageプロパティを設定することもできますが、画像のファイルパスがデータソースに含まれている場合などは、アンバウンドフィールドを経由して設定したほうが確実にデータと同期できます。基本的にはこちらの方法を推奨します。
◆サンプルコード(C#)
◆サンプルコード(VB.NET)
セクションレポートの場合
System.Drawing.Imageクラスのメソッドを使用することで、Pictureコントロールに動的に画像を読み込むことが可能です。詳細については、製品ヘルプの以下のトピックをご参照ください。
よくある質問 - セクションレポート - レイアウト - 「画像を動的に読み込む」
なお、DetailのFormatイベントでPicture.Imageプロパティを設定することもできますが、画像のファイルパスがデータソースに含まれている場合などは、アンバウンドフィールドを経由して設定したほうが確実にデータと同期できます。基本的にはこちらの方法を推奨します。
◆サンプルコード(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 = new Bitmap(System.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 = New Bitmap(System.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)
関連情報
旧文書番号
85276