YouTube | Facebook | X(Twitter) | RSS

RubberEnvelope で指定した範囲のイメージを画像ファイルに出力する方法

2016/9/1 (木)

private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
{
    int dpi = 96;      //出力解像度(96 で Windows 標準 DPI と一致)
    double ratio = 1;    //出力ピクセルサイズの比率
    IEnvelope pEnvelope = axMapControl1.TrackRectangle();   //マウス ドラッグでEnvelopeを取得
 
    IActiveView pActiveView = axMapControl1.ActiveView;
    IOutputRasterSettings pOutputRasterSettings = (IOutputRasterSettings)pActiveView.ScreenDisplay.DisplayTransformation;
    pOutputRasterSettings.ResampleRatio = 1;     //ラスタのピクセル比率を1に変更
     
    IExport pExport = new ExportJPEGClass();
    pExport.ExportFileName = @"D:\Workspace\test_JPEGExport1.jpg";  //保存場所
    pExport.Resolution = dpi;    //出力解像度
 
    //出力画像のシンボル サイズを変更させる場合は基準縮尺を変更(ディスプレイと一致の場合は 0 を指定)
    axMapControl1.Map.ReferenceScale = 0;   
 
    //RubberBand の範囲をディスプレイ解像度に変換
    int rectLeft, rectTop, rectRight, rectBottom;
    pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(pEnvelope.UpperLeft, out rectLeft, out rectTop);
    pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(pEnvelope.LowerRight, out rectRight, out rectBottom);
 
    //MapControlの範囲で出力する場合
    //tagRECT pRECT = pActiveView.ExportFrame;
    //pRECT.left = 0;
    //pRECT.top = 0;
    //pRECT.right = (int)(pRECT.right * ratio);
    //pRECT.bottom = (int)(pRECT.bottom * ratio);
 
    //RubberBand の範囲を出力する場合
    tagRECT pRECT;
    pRECT.left = 0;
    pRECT.top = 0;
    pRECT.right = (int)((rectRight - rectLeft) * ratio);
    pRECT.bottom = (int)((rectBottom - rectTop) * ratio);
 
 
    //IEnvelope pVisibleBounds = pActiveView.Extent;  //MapControlの範囲で出力する場合
    IEnvelope pVisibleBounds = pEnvelope;
    IEnvelope pPixelBounds = new EnvelopeClass();
    pPixelBounds.PutCoords(pRECT.left, pRECT.top, pRECT.right, pRECT.bottom);
    pExport.PixelBounds = pPixelBounds;
 
    int hDc = pExport.StartExporting();
     
    pActiveView.Output(hDc, (int)pExport.Resolution, pRECT, pVisibleBounds, null);
    pExport.FinishExporting();
    pExport.Cleanup();
 
    pActiveView.Draw(hDc, null);
 
    MessageBox.Show("Done");
}
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
     
    Dim pMxApplication As IMxApplication
    Set pMxApplication = Application
     
    Dim pMxDocument As IMxDocument
    Set pMxDocument = ThisDocument
     
    Dim pRubberBand As IRubberBand
    Set pRubberBand = New RubberEnvelope
     
    Dim pEnvelope As IEnvelope
    Set pEnvelope = pRubberBand.TrackNew(pMxApplication.Display, Nothing)   'マウス ドラッグでEnvelopeを取得
             
    Dim pActiveView As IActiveView
    Set pActiveView = pMxDocument.ActiveView
     
    Dim pOutputRasterSettings As IOutputRasterSettings
    Set pOutputRasterSettings = pActiveView.ScreenDisplay.DisplayTransformation
    pOutputRasterSettings.ResampleRatio = 1 'ラスタのピクセル比率を1に変更
     
    Dim pExport As IExport
    Set pExport = New ExportJPEG
    pExport.ExportFileName = "D:\Workspace\test_JPEGExport1.jpg"    '保存場所
    pExport.Resolution = 96                                        '解像度
     
    Dim pRECT As tagRECT
    pRECT = pActiveView.ExportFrame
    pRECT.Left = 0
    pRECT.Top = 0
    pRECT.Right = pRECT.Right * pExport.Resolution / 96     'ディスプレイが96dpi設定の場合
    pRECT.bottom = pRECT.bottom * pExport.Resolution / 96
     
    Dim pVisibleBounds As IEnvelope
    Set pVisibleBounds = pEnvelope
 
    Dim pPixelBounds As IEnvelope
    Set pPixelBounds = New Envelope
 
    pPixelBounds.PutCoords pRECT.Left, pRECT.Top, pRECT.Right, pRECT.bottom
    pExport.PixelBounds = pPixelBounds
     
    Dim hDc As OLE_HANDLE
    hDc = pExport.StartExporting
     
    pActiveView.Output hDc, pExport.Resolution, pRECT, pVisibleBounds, Nothing
     
    pExport.FinishExporting
    pExport.Cleanup
     
    pActiveView.Extent = pEnvelope
     
     
    pActiveView.Draw hDc, Nothing
     
    MsgBox "Done"
     
        pActiveView.Refresh
 
End Sub
  • この記事を書いた人

羽田 康祐

伊達と酔狂のGISエンジニア。GIS上級技術者、Esri認定インストラクター、CompTIA CTT+ Classroom Trainer、潜水士、PADIダイブマスター、四アマ。WordPress は 2.1 からのユーザーで歴だけは長い。 代表著書『"地図リテラシー入門―地図の正しい読み方・描き方がわかる』 GIS を使った自己紹介はこちら。ESRIジャパン(株)所属、元青山学院大学非常勤講師を兼務。日本地図学会第31期常任委員。発言は個人の見解です。

-プログラミング, ArcGIS
-, ,