【问题标题】:How to find center point of image?如何找到图像的中心点?
【发布时间】:2014-02-04 13:28:37
【问题描述】:

我想将裁剪控件放在图片框图像的中心。我试过下面的代码

Dim oCropControl As new CropControl
Dim oControlLocation As Point

oControlLocation = New Point(peImageViewer.Width / 2, peImageViewer.Height / 2)
oCropControl.Location = New Point(oControlLocation.X, oControlLocation.Y)

但这效果不佳.. :( 裁剪控制显示在底部。

提前致谢!!

【问题讨论】:

  • oControlLocation 是什么?
  • 这是我的作物控制的定位点。

标签: vb.net image-processing crop


【解决方案1】:

假设它们都是同一个控件的父级,您可以这样做:

Dim rect1 As Rectangle = Me.myPictureBox.Bounds
Dim rect2 As Rectangle = Me.myCropControl.Bounds

rect2.X = CInt(rect1.X + ((rect1.Width / 2) - (rect2.Width / 2)))
rect2.Y = CInt(rect1.Y + ((rect1.Height / 2) - (rect2.Height / 2)))

Me.myCropControl.Bounds = rect2
Me.myCropControl.BringToFront()

示例

Public Class Form1

    Public Sub New()
        Me.InitializeComponent()
        Me.Size = New Size(400, 400)
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.myButton = New Button() With {.Location = New Point(3, 3), .Text = "ALIGN!"}
        Me.myCropControl = New Label() With {.Bounds = New Rectangle(245, 263, 60, 60), .BackColor = Color.Blue, .ForeColor = Color.White, .Text = "CROP", .TextAlign = ContentAlignment.MiddleCenter}
        Me.myPictureBox = New PictureBox() With {.Bounds = New Rectangle(23, 56, 246, 143), .BackColor = Color.Red}
        Me.Controls.AddRange({Me.myButton, Me.myCropControl, Me.myPictureBox})
    End Sub

    Private Sub Align(sender As Object, e As EventArgs) Handles myButton.Click
        Dim rect1 As Rectangle = Me.myPictureBox.Bounds
        Dim rect2 As Rectangle = Me.myCropControl.Bounds
        rect2.X = CInt(rect1.X + ((rect1.Width / 2) - (rect2.Width / 2)))
        rect2.Y = CInt(rect1.Y + ((rect1.Height / 2) - (rect2.Height / 2)))
        Me.myCropControl.Bounds = rect2
        Me.myCropControl.BringToFront()
    End Sub

    Private WithEvents myButton As Button
    Private myCropControl As Label
    Private myPictureBox As PictureBox

End Class

【讨论】:

  • 你好.. 谢谢你的帮助.. :) 我更详细地描述了你.. 我有尺寸 oControlSize = New Size(150, 150) 的裁剪控制,我想放置我的裁剪控制在图片框控件的中心。是的,它们都是同一个控件的父级。我正在做 oCropControl.Location = New Point(rect2.X, rect2.Y)。但我想它给了我图片框中心作物控制的左上角。但我想在中心控制作物。希望你能理解我的问题!
  • @TithiPatel 如果这不起作用那么,a) 控件 是同一控件的父级,或者 b) 图片不在图片框内居中。
  • 是的,你是对的。我在计算后为我的作物控制分配尺寸,所以它不起作用......但现在它像魅力一样工作! :) 非常感谢您的帮助..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 2019-09-24
相关资源
最近更新 更多