LEADTOOLS ナレッジベース

[RasterImageViewer] 画像を拡大・縮小表示する際に、画像の中央をコントロールの中心に表示できますか?

画像を拡大・縮小表示する際に、画像の中央をRasterImageViewerコントロールの中心に表示するには、RasterImageViewer.CenterAtPointメソッドで拡大・縮小した画像の中心を設定します。

サンプルコード(VB.NET)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    RasterImageViewer1.ScaleFactor = RasterImageViewer1.ScaleFactor + 0.5
    RasterImageViewer1.ScrollPosition = New Point(0, 0)

    RasterImageViewer1.CenterAtPoint(New Point(CInt(RasterImageViewer1.Image.ImageWidth * _
                                                       RasterImageViewer1.ScaleFactor / 2), _
                                                       CInt(RasterImageViewer1.Image.ImageHeight * _
                                                       RasterImageViewer1.ScaleFactor / 2)))

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    RasterImageViewer1.ScaleFactor = RasterImageViewer1.ScaleFactor - 0.5
    RasterImageViewer1.ScrollPosition = New Point(0, 0)

    RasterImageViewer1.CenterAtPoint(New Point(CInt(RasterImageViewer1.Image.ImageWidth * _
                                                       RasterImageViewer1.ScaleFactor / 2), _
                                                       CInt(RasterImageViewer1.Image.ImageHeight * _
                                                       RasterImageViewer1.ScaleFactor / 2)))

End Sub

サンプルコード(C#)

private void Button1_Click(object sender, System.EventArgs e)
{
    rasterImageViewer1.ScaleFactor = rasterImageViewer1.ScaleFactor + 0.5;
    rasterImageViewer1.ScrollPosition = new Point(0, 0);

    rasterImageViewer1.CenterAtPoint(new Point((int)(rasterImageViewer1.Image.ImageWidth *
                                                       rasterImageViewer1.ScaleFactor / 2),
                                                       (int)(rasterImageViewer1.Image.ImageHeight *
                                                       rasterImageViewer1.ScaleFactor / 2)));
}

private void Button2_Click(object sender, System.EventArgs e)
{
    rasterImageViewer1.ScaleFactor = rasterImageViewer1.ScaleFactor - 0.5;
    rasterImageViewer1.ScrollPosition = new Point(0, 0);

    rasterImageViewer1.CenterAtPoint(new Point((int)(rasterImageViewer1.Image.ImageWidth *
                                                       rasterImageViewer1.ScaleFactor / 2),
                                                       (int)(rasterImageViewer1.Image.ImageHeight *
                                                       rasterImageViewer1.ScaleFactor / 2)));
}

【注記】
上記はSizeModeプロパティが「Normal」に設定されている場合を想定しています。
SizeModeプロパティが「Fit」や「FitWidth」などNormal以外の場合には、Fitに適正な水平および垂直の倍率で表示されます。
このため、Normal以外の場合には、CenterAtPointメソッドにおいてScaleFactorプロパティではなく、CurrentXScaleFactor/CurrentYScaleFactorプロパティを使用して、現在の水平/垂直倍率を掛けるようにしてください。

サンプルコード(VB.NET)

RasterImageViewer1.CenterAtPoint(New Point(CInt(RasterImageViewer1.Image.ImageWidth * _
                                           RasterImageViewer1.CurrentXScaleFactor / 2), _
                                       CInt(RasterImageViewer1.Image.ImageHeight * _
                                           RasterImageViewer1.CurrentYScaleFactor / 2)))

サンプルコード(C#)

rasterImageViewer1.CenterAtPoint(new Point((int)(rasterImageViewer1.Image.ImageWidth *
                                               rasterImageViewer1.CurrentXScaleFactor / 2),
                                               (int)(rasterImageViewer1.Image.ImageHeight *
                                               rasterImageViewer1.CurrentYScaleFactor / 2)));

 

関連情報

0 コメント

記事コメントは受け付けていません。