ページレポート/RDLレポートではファイルパスを使用してファイルシステム上のリソースを取得できますが、ときどき、データベースなどの特殊なソースにリソースが保存されていることがあります。ページレポート/RDLレポートでは、カスタムリソースロケーターを使用して、レポートに必要なリソースをどのような場所からも読み込むことができます。このチュートリアルは「カスタムリソースロケーター」のサンプルを基にしており、ユーザーのMy Picturesディレクトリから画像をロードする方法を示します。
このトピックでは以下のタスクを行います。
チュートリアルを完了すると、次のようなレポートが作成されます。
Visual StudioプロジェクトにActiveReportsを追加する
レポートのレイアウトを作成する
| プロパティ名 | プロパティの値 |
|---|---|
| Name | Image1 |
| Location | 0.1in, 0.1in |
| Size | 2.8in, 2.8in |
| Value | MyPictures:Penguins.jpg |
| プロパティ名 | プロパティの値 |
|---|---|
| Name | Image2 |
| Location | 3.1in, 0.1in |
| Size | 2.8in, 2.8in |
| Value | MyPictures:Desert.jpg |
新しいMyPicturesLocatorクラスを追加する
Visual Basic
| Visual Basicコード(一番上に貼り付けます) |
コードのコピー
|
|---|---|
Imports System Imports System.Drawing Imports GrapeCity.ActiveReports.Extensibility Imports System.Globalization Imports System.IO Imports System.Runtime.InteropServices |
|
| Visual Basicコード(クラス内に貼り付けます) |
コードのコピー
|
|---|---|
Inherits ResourceLocator
Private Const UriSchemeMyImages As String = "MyPictures:"
' リソースを取得して返します。
Public Overrides Function GetResource(resourceInfo As ResourceInfo) As Resource
Dim name As String = resourceInfo.Name
If name Is Nothing OrElse name.Length = 0 Then
Throw New ArgumentException("The name of resource to be obtained should be non-empty string.", "name")
End If
Dim uri As New Uri(name)
Dim stream As Stream = GetPictureFromSpecialFolder(name)
If stream Is Nothing Then
stream = New MemoryStream()
End If
Return New Resource(stream, uri)
End Function
' Public Picturesフォルダーから指定した画像を返します。
Private Shared Function GetPictureFromSpecialFolder(path As String) As Stream
Dim startPathPos As Integer = UriSchemeMyImages.Length
If startPathPos >= path.Length Then
Return Nothing
End If
Dim pictureName As String = path.Substring(startPathPos)
Dim myPicturesPath As String = Environment.GetEnvironmentVariable("public") & "\Pictures\Sample Pictures"
If Not myPicturesPath.EndsWith("\") Then
myPicturesPath += "\"
End If
Dim picturePath As String = System.IO.Path.Combine(myPicturesPath, pictureName)
If Not File.Exists(picturePath) Then
Return Nothing
End If
Dim stream As New MemoryStream()
Try
Dim picture As Image = Image.FromFile(picturePath)
picture.Save(stream, picture.RawFormat)
stream.Position = 0
Catch generatedExceptionName As OutOfMemoryException
' ファイルが有効な画像でないか、GDI+がこの画像をサポートしていません。
Return Nothing
Catch generatedExceptionName As ExternalException
Return Nothing
End Try
Return stream
End Function
|
|
C#
| C#コード(一番上に貼り付けます) |
コードのコピー
|
|---|---|
using System; using System.Drawing; using System.Globalization; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; using GrapeCity.ActiveReports.Extensibility; using your_project_name.Properties; |
|
| C#コード(Usingステートメントの下に貼り付けます) |
コードのコピー
|
|---|---|
namespace your_project_name
{
// My Picturesフォルダーでリソースを検索します。
internal sealed class MyPicturesLocator : ResourceLocator
{
private const string UriSchemeMyImages = "MyPictures:";
// リソースを取得して返します。
public override Resource GetResource(ResourceInfo resourceInfo)
{
string name = resourceInfo.Name;
if (name == null || name.Length == 0)
{
throw new ArgumentException("The name of resource to be obtained should be non-empty string.", "name");
}
Uri uri = new Uri(name);
Stream stream = GetPictureFromSpecialFolder(name);
if (stream == null)
{
stream = new MemoryStream();
}
return new Resource(stream, uri);
}
// Public Picturesフォルダーから指定した画像を返します。
private static Stream GetPictureFromSpecialFolder(string path)
{
int startPathPos = UriSchemeMyImages.Length;
if (startPathPos >= path.ToString().Length)
{
return null;
}
string pictureName = path.ToString().Substring(startPathPos);
string myPicturesPath = Environment.GetEnvironmentVariable("public") + "\\Pictures\\Sample Pictures";
if (!myPicturesPath.EndsWith("\\")) myPicturesPath += "\\";
string picturePath = Path.Combine(myPicturesPath, pictureName);
if (!File.Exists(picturePath)) return null;
MemoryStream stream = new MemoryStream();
try
{
Image picture = Image.FromFile(picturePath);
picture.Save(stream, picture.RawFormat);
stream.Position = 0;
}
catch (OutOfMemoryException) // ファイルが有効な画像でないか、GDI+がこの画像をサポートしていません。
{
return null;
}
catch (ExternalException)
{
return null;
}
return stream;
}
}
}
|
|
PreviewFormを作成する
| プロパティ名 | プロパティの値 |
|---|---|
| Name | PreviewForm |
| Text | プレビューフォーム |
| Size | 1015, 770 |
| プロパティ名 | プロパティの値 |
|---|---|
| Name | reportPreview1 |
| Dock | Fill |
Visual Basic
| Visual Basicコード(Importsステートメントの下に貼り付けます) |
コードのコピー
|
|---|---|
Imports GrapeCity.ActiveReports.Document Imports System.IO Imports GrapeCity.ActiveReports |
|
| Visual Basicコード(Loadイベント内に貼り付けます) |
コードのコピー
|
|---|---|
Dim reportData As Stream = [GetType]().Assembly.GetManifestResourceStream("your_project_name.DemoReport.rdlx")
reportData.Position = 0
Dim reader As New StreamReader(reportData)
Dim def As New PageReport(reader)
def.ResourceLocator = New MyPicturesLocator()
Dim runtime As New PageDocument(def)
reportPreview1.ReportViewer.LoadDocument(runtime)
|
|
C#
| C#コード(Usingステートメントの下に貼り付けます) |
コードのコピー
|
|---|---|
using GrapeCity.ActiveReports.Document; using System.IO; using GrapeCity.ActiveReports; |
|
| C#コード(Loadイベント内に貼り付けます) |
コードのコピー
|
|---|---|
string myPicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
Stream reportData = GetType().Assembly.GetManifestResourceStream("your_project_name.DemoReport.rdlx");
reportData.Position = 0;
StreamReader reader = new StreamReader(reportData);
PageReport def = new PageReport(reader);
def.ResourceLocator = new MyPicturesLocator();
PageDocument runtime = new PageDocument(def);
reportPreview1.ReportViewer.LoadDocument(runtime);
|
|