印刷と保存example
[Chapter 8-2]では、印刷機能について追加していきます。
印刷と保存example [Capter 8-1]のex08に追加していきます。
Program開発環境は、Visual Basic.NET 2010です。
Visual Sutdioの使い方は、一通り問題なく操作でき、一度はGUIのprogramを作成したことがある中級者を対象にしています。 したがって、vb.netの操作に関しては、端折ります。
Program Coding
印刷機能を追加するために、MapWindowのreportsオブジェクトを使います。
まずは、参照の追加で、MapWindowsInterface.dllを追加します。
これで、準備OKです。
Form design
Main Form
Print Form
Coding
それでは、Print Formからcodingを始めていきます。
印刷実行の作成
印刷機能をはじめから実装するのは、面倒なので、MapWindowのreports機能を使うのですが、まず、そのオブジェクト構成をオブジェクトブラウザでreportsの確認をしておきましょう。
今回は、Mapだけを印刷するようにcodingします。
使用するのは、Function GetScreenPicture(BoundBox As MapWinGIS.Extents) As MapWinGIS.Imageです。
その他のFunctionは使用しないのですが、定義しておく必要があります。
まずは、Importsと変数宣言をしておきます。ついでにLoad時も実装しておきます。
Imports AxMapWinGIS Imports MapWinGIS Imports MapWindow Imports MapWindow.Interfaces Imports MapWinUtility Public Class frmPrintOut Implements MapWindow.Interfaces.Reports Public t_map As New AxMapWinGIS.AxMap Private Sub frmPrintOut_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load PrintDocument.DocumentName = "TEST" PrintDocument.DefaultPageSettings.Landscape = True End Sub ~~~ ~~~ End Class
次に、先ほどのオブジェクトブラウザで確認したMapWindow Interfaces Reportに関するfunctionを実装します。
#Region "MapWindow Interfaces Report" Public Function GetNorthArrow() As System.Drawing.Image Implements MapWindow.Interfaces.Reports.GetNorthArrow Return Nothing End Function Public Function GetLegendSnapshot(ByVal LayerHandle As Integer, ByVal imgWidth As Integer) As System.Drawing.Image Implements MapWindow.Interfaces.Reports.GetLegendLayerSnapshot Return Nothing End Function Public Function GetLegendSnapshot2(ByVal VisibleLayersOnly As Boolean, ByVal imgWidth As Integer) As System.Drawing.Image Implements MapWindow.Interfaces.Reports.GetLegendSnapshot Return Nothing End Function Public Function GetLegendSnapshotHQ(ByVal LayerHandle As Integer, ByVal Width As Integer, ByVal Columns As Integer, ByVal FontFamily As String, ByVal MinFontSize As Integer, ByVal MaxFontSize As Integer, ByVal UnderlineLayerTitles As Boolean, ByVal BoldLayerTitles As Boolean) As System.Drawing.Image Implements MapWindow.Interfaces.Reports.GetLegendSnapshotHQ Return Nothing End Function Public Function GetLegendSnapshotBreakHQ(ByVal LayerHandle As Integer, ByVal Category As Integer, ByVal Width As Integer, ByVal Height As Integer) As System.Drawing.Image Implements Interfaces.Reports.GetLegendSnapshotBreakHQ Return Nothing End Function <CLSCompliant(False)> _ Public Function GetScaleBar(ByVal MapUnits As MapWindow.Interfaces.UnitOfMeasure, ByVal ScalebarUnits As MapWindow.Interfaces.UnitOfMeasure, ByVal MaxWidth As Integer) As System.Drawing.Image Implements MapWindow.Interfaces.Reports.GetScaleBar Return Nothing End Function Public Function GetScaleBar(ByVal MapUnits As String, ByVal ScalebarUnits As String, ByVal MaxWidth As Integer) As System.Drawing.Image Implements MapWindow.Interfaces.Reports.GetScaleBar Return Nothing End Function <CLSCompliant(False)> _ Public Function GetScreenPicture(ByVal BoundBox As MapWinGIS.Extents) As MapWinGIS.Image Implements MapWindow.Interfaces.Reports.GetScreenPicture Try Return CType(t_map.SnapShot(BoundBox), MapWinGIS.Image) Catch ex As System.Exception MsgBox(ex.Message) Return Nothing End Try End Function #End Region
使わないfunctionは戻り値がnothingでとりあえず作成しておきます。
t_map.SnapShot(BoundBox)がMap上のimageを作成している部分になります。
次にPrintDocumentのPrintPage eventの実装を行います。
PrintDocument_PrintPage event
mapのimageをBitmapに変換してそのbitmapの幅と高さを計算して、用紙に割り付けます。
Private Sub PrintDocument_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage 'print map image document Dim cvter As New MapWinUtility.ImageUtils Dim MapImage As Drawing.Bitmap = CType(cvter.IPictureDispToImage( GetScreenPicture(t_map.Extents).Picture), Drawing.Bitmap) 'get the drawing graphic Dim g As System.Drawing.Graphics = e.Graphics Dim DrawingWidth As Integer Dim DrawingHeight As Integer DrawingWidth = e.MarginBounds.Right - e.MarginBounds.Left DrawingHeight = (e.MarginBounds.Bottom) - (e.MarginBounds.Top) 'calculate the image size Dim MapImageWidth As Integer Dim MapImageHeight As Integer If (MapImage.Width > MapImage.Height) Then If (MapImage.Width > DrawingWidth) Then MapImageWidth = DrawingWidth MapImageHeight = MapImage.Height - (MapImage.Width - DrawingWidth) Else MapImageWidth = MapImage.Width MapImageHeight = MapImage.Height End If ElseIf (MapImage.Height > MapImage.Width) Then If (MapImage.Height > DrawingHeight) Then MapImageHeight = DrawingHeight MapImageWidth = MapImage.Width - (MapImage.Height - DrawingHeight) Else MapImageWidth = MapImage.Width MapImageHeight = MapImage.Height End If End If 'draw the mapImage Dim MapBounds As System.Drawing.Rectangle MapBounds = New Rectangle(CInt(e.MarginBounds.Left + (DrawingWidth - MapImageWidth) / 2), CInt(e.MarginBounds.Top + ((DrawingHeight - MapImageHeight) / 2)), MapImageWidth, MapImageHeight) g.DrawImage(MapImage, MapBounds) End Sub Private Function IPictureDispToImage(ByVal img As stdole.IPictureDisp) As System.Drawing.Image Dim cvter As New MapWinUtility.ImageUtils Return cvter.IPictureDispToImage(img) End Function
g.DrawImage(MapImage, MapBounds)でSystem.Drawing.Graphicsに作成します。
プリンター設定button
プリンター設定button click時のcodeは、
Private Sub btnPrintProperties_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintProperties.Click PrintDialog.Document = PrintDocument PrintDialog.ShowDialog() End Sub
な感じで作成します。
プレビューbutton
プレビューbutton click時のcodeは、
Private Sub btnPreview_Click(sender As System.Object, e As System.EventArgs) Handles btnPreview.Click Try PrintPreviewDialog1.Document = PrintDocument PrintPreviewDialog1.Size = New Size(800, 600) PrintPreviewDialog1.ShowDialog(Me) Catch ex As System.Drawing.Printing.InvalidPrinterException MsgBox("プリンターが設定されていません。プレビューが表示できません。", MsgBoxStyle.Exclamation, "プリンターエラー") Exit Sub Catch ex As System.Exception MsgBox(ex) End Try End Sub
な感じで作成します。
印刷実行button
印刷実行button click時のcodeは、
Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click Try PrintDocument.Print() Me.Close() Catch ex As System.Drawing.Printing.InvalidPrinterException MsgBox("プリンターが設定されていません。", MsgBoxStyle.Exclamation, "印刷エラー") Exit Sub End Try End Sub
な感じで作成します。
Main Formのcodeing
印刷実行のbutton click event発生時のcodingは、
Private Sub ToolStripButton7_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton7.Click If AxMap1.NumLayers > 0 Then Dim pintform As New frmPrintOut pintform.t_map = AxMap1 pintform.ShowDialog() End If End Sub
Programの実行
今回のまとめ
では、今回のまとめを
-
MapWindow.Interfacesを使って印刷の実装を行いました。
今回で[Chapter 8]は終わります。次回[Chapter 9]では、簡単なGeoProcessingを付け加えてみましょう。
vb.net2010だけでなく、C#でも、visual studio2008でも動作することができます。
簡単に自前のソフトウェアにちょっとしたGIS機能を付け加えるには良いと思います。
ex082のsource codeを公開しますので、活用してください。
ただし、あくまでの自分用のサンプルですので、バグなどがある可能性が高いので、原文のままの使用は避けてください。
サンプルコードおよびサンプルデータを使って、お使いのPCの不具合が生じても一切責任は持てませんので、あくまでも自己責任にて使用してください。
Exercise
今回のProgramを少し発展させて以下の項目を付け加えてみてください。
-
凡例やNorthArrowのあわせて印刷できるようにcodeを追加する。
最近のコメント