将图像强制为固定大小

时间:2014-01-17 02:01:03

标签: .net vb.net image

我们下载或创建的所有图片都有不同的尺寸,大部分时间都不适合我们创建的应用。
有没有办法强制图片出现,例如A4尺寸?

对于我的具体问题,
enter image description here
这是应用程序的整个窗口,但图片在底部进一步延伸,这就是为什么我想设置边界的可能性更大,迫使它调整为 60%

这就是我调用图像的方式

Dim cm As New customImageOverlay(mainMap.Position)
cm.MarkerImage = New Bitmap(My.Resources.ground_floor_plan)
' before I make it appear to the map I want to do something like this
' cm.size = form.clientrectagle - 40% - just something in my mind..
    objects.Markers.Add(cm)
' this is at form  load event, I am just adding the image as an overlay
' so there are no pictureboxes

1 个答案:

答案 0 :(得分:1)

您可能需要计算图像的大小,然后创建它的缩略图:

Friend Function GetImageThumb(ByVal orgBmp As BitMap, 
                        ByVal w as Int32, h as Int32) As Bitmap

    Dim thumb As New Bitmap(w, h)

    Using g As Graphics = Graphics.FromImage(thumb)
        g.DrawImage(orgBmp , 0, 0, w + 1, h + 1)
    End Using
    Return thumb
End Function

更重要的可能是缩放。确定是否要根据宽度或高度调整大小,然后根据该值计算新大小。

    ' get new Height Scaled from a set Width:
    Friend Function ScaledHeight(ByVal w As Integer, orgSize as Size) As Integer
        Dim scale As Single = CSng(orgSize.Height / orgSize.Width)
        Return CInt(w * scale)
    End Function

    ' get new Width scaled from the Height:
    Friend Function ScaledWidth(ByVal h As Integer, orgSize as Size) As Integer
        Dim scale As Single = CSng(orgSize.Width / orgSize.Height )
        Return CInt(h * scale)
    End Function

查看原始图像,找出你想要的新尺寸,然后通过缩放宽度或高度(通常是高度,对我来说)确定拇指的大小,然后使用新的创建缩略图大小

修改

除非您要显示的所有图像具有相同的尺寸,否则它并非真正“固定”,通常情况并非如此。当屏幕具有与图像不同的纵横比(W:H)时,缩放尤其重要。

对于60%的屏幕大小(伪代码,你将不得不解决一些问题):

 Dim bmp as Bitmap = New Bitmap(My.Resources.ground_floor_plan)

 Dim orgSize As Size = bmp.Size

 ' scale to 60% of width
 Dim newWidth As Integer = (thisForm.Width * .6)
 ' maybe:
 'Dim newWidth As Integer = (My.Computer.Screen.WorkingArea.Width * .6)
 Dim newWidth As Int32 = ScaledHeight(newWidth, orgSize )

 Dim thumb as Bitmap = GetImageThumb(bmp, newWidth, newHeight)

 cm.MarkerImage = thumb
 objects.Markers.Add(cm)
 bmp.Dispose