2017-06-13 93 views
0

是否存在將圖像保存爲集合的簡單方法210px高度但寬度將成比例。例如。如果圖像自然是420 x 600的話,那就調整/另存爲210 X 300。我正在使用的代碼是VB和當前設置保存爲210x210 ..將圖像保存爲120高度x比例寬度 - VB

Dim returnImage As System.Drawing.Image = Image.FromFile("D:\domains\example.com\httpdocs\catalog\images\" & imageFilename) 
    Dim thumb As System.Drawing.Image = FixedSize(returnImage, 210, 210) 

回答

1

我相信你想要什麼要做的是保持長寬比,這是我使用的代碼:

Public Function ScaleImage(ByVal OldImage As System.Drawing.Image, ByVal TargetHeight As Integer, ByVal TargetWidth As Integer) As System.Drawing.Image 

    Dim NewHeight As Integer = TargetHeight 
    Dim NewWidth As Integer = NewHeight/OldImage.Height * OldImage.Width 

    If NewWidth > TargetWidth Then 
     NewWidth = TargetWidth 
     NewHeight = NewWidth/OldImage.Width * OldImage.Height 
    End If 

    Return New Bitmap(OldImage, NewWidth, NewHeight) 

End Function 
+0

非常有幫助謝謝。我不需要IF語句,因爲我希望圖像的高度爲210,但對寬度沒有限制,所以我將其解決了。非常感謝! – Eggybread

相關問題