我正在調整來自遠程服務器的自己的服務器上的圖像大小。這完美地工作,但對於一些圖像,它調整它完全錯誤。使用不同的DPI調整大小的ASP.NET圖像
但是例如該圖像中,工作非常並最終被非常小: http://www.topwedding.com/bigimage/Wedding%20Dresses/HS01116007/Floral-One-Shoulder-Organza-over-Satin-A-Line-Bridal-Gown-with-Pick-Ups.jpg
經檢驗它原來的第一圖像具有72個像素/英寸而第二個圖像具有951像素/英寸。
壞調整大小是一樣的問題,我收到了(見here)
但我認爲這是現在在我的代碼使用PixelWidth
和PixelHeight
性能解決,如在其他職位建議。
目標是讓產品概覽頁面上顯示200像素寬的縮略圖。我如何確保圖像最終尺寸相同?
完整代碼如下:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ResizeAndSaveFast(200, 200, "http://www.topwedding.com/bigimage/Wedding%20Dresses/HSTSKN510/Strapless-Mermaid-Appliqued-Wedding-Dress-with-Ruched-Bodice-and-Pick-Up-Skirt.jpg", Server.MapPath("images\") + "1.png")
ResizeAndSaveFast(200, 200, "http://www.topwedding.com/bigimage/Wedding%20Dresses/HS01116007/Floral-One-Shoulder-Organza-over-Satin-A-Line-Bridal-Gown-with-Pick-Ups.jpg", Server.MapPath("images\") + "2.png")
End Sub
Private Function ResizeAndSaveFast(ByVal maxWidth As Integer, ByVal maxHeight As Integer, ByVal imageURL As String, ByVal saveToPath As String) As Boolean
Dim imgRequest As WebRequest = WebRequest.Create(imageURL)
Dim imgResponse As WebResponse
imgResponse = imgRequest.GetResponse()
Dim streamPhoto As Stream = imgResponse.GetResponseStream()
Dim memStream As New MemoryStream
streamPhoto.CopyTo(memStream)
memStream.Position = 0
Dim bfPhoto As BitmapFrame = ReadBitmapFrame(memStream)
Dim newWidth, newHeight As Integer
Dim scaleFactor As Double
newWidth = bfPhoto.PixelWidth
newHeight = bfPhoto.PixelHeight
If bfPhoto.PixelWidth > maxWidth Or bfPhoto.PixelHeight > maxHeight Then
If bfPhoto.PixelWidth > maxWidth Then
scaleFactor = maxWidth/bfPhoto.PixelWidth
newWidth = Math.Round(bfPhoto.Width * scaleFactor, 0)
newHeight = Math.Round(bfPhoto.Height * scaleFactor, 0)
End If
If newHeight > maxHeight Then
scaleFactor = maxHeight/newHeight
newWidth = Math.Round(newWidth * scaleFactor, 0)
newHeight = Math.Round(newHeight * scaleFactor, 0)
End If
End If
Dim bfResize As BitmapFrame = FastResize(bfPhoto, newWidth, newHeight)
If bfResize Is Nothing Then Return False
Dim baResize As Byte() = ToByteArray(bfResize)
File.WriteAllBytes(saveToPath, baResize)
Return True
End Function
Arghh ...時而在代碼尋找年齡後,你只需想念這樣的東西......謝謝! – Flo
不客氣。 :-) – ConnorsFan