PublicClass CenterPictureClass CenterPicture Inherits System.Windows.Forms.UserControl Windows 窗体设计器生成的代码#Region " Windows 窗体设计器生成的代码 " PublicSub New()SubNew() MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub 'UserControl 重写 dispose 以清理组件列表。 ProtectedOverloadsOverridesSub Dispose()Sub Dispose(ByVal disposing AsBoolean) If disposing Then IfNot (components IsNothing) Then components.Dispose() EndIf EndIf MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 <System.Diagnostics.DebuggerStepThrough()>PrivateSub InitializeComponent()Sub InitializeComponent() ' 'CenterPicture ' Me.Name ="CenterPicture" Me.Size =New System.Drawing.Size(272, 192) End Sub #End Region Private m_MapImage As Image PublicProperty 图像()Property 图像() As Image Get Return m_MapImage EndGet Set(ByVal Value As Image) m_MapImage = Value Me.Invalidate() EndSet End Property Dim m_BasePoint As PointF Dim m_Size As SizeF Dim m_Scale AsDouble PrivateSub UserControl1_Paint()Sub UserControl1_Paint(ByVal sender AsObject, ByVal e As System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint IfNot (m_MapImage IsNothing) Then Dim mapScale AsDouble= m_MapImage.Size.Height / m_MapImage.Size.Width Dim myScale AsDouble=Me.Size.Height /Me.Size.Width If mapScale > myScale Then m_BasePoint =New PointF(CSng((Me.Size.Width -Me.Size.Height / mapScale) /2), 0) m_Size =New SizeF(CSng(Me.Size.Height / mapScale), CSng(Me.Size.Height)) m_Scale =CSng(Me.Size.Height / m_MapImage.Size.Height) Else m_BasePoint =New PointF(0, CSng((Me.Size.Height -Me.Size.Width * mapScale) /2)) m_Size =New SizeF(CSng(Me.Width), CSng(Me.Size.Width * mapScale)) m_Scale =CSng(Me.Size.Width / m_MapImage.Size.Width) EndIf e.Graphics.DrawImage(m_MapImage, New RectangleF(m_BasePoint, m_Size)) EndIf End Sub End Class