2016-06-21 75 views
0

我正在調整來自遠程服務器的自己的服務器上的圖像大小。這完美地工作,但對於一些圖像,它調整它完全錯誤。使用不同的DPI調整大小的ASP.NET圖像

該圖像完美地工作: http://www.topwedding.com/bigimage/Wedding%20Dresses/HSTSKN510/Strapless-Mermaid-Appliqued-Wedding-Dress-with-Ruched-Bodice-and-Pick-Up-Skirt.jpg

但是例如該圖像中,工作非常並最終被非常小: 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

但我認爲這是現在在我的代碼使用PixelWidthPixelHeight性能解決,如在其他職位建議。

目標是讓產品概覽頁面上顯示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 

回答

1

您當前的代碼仍然在其計算使用bfPhoto.WidthbfPhoto.Height(對於「寬度」比例因子)。你可以試試下面的代碼只使用bfPhoto.PixelWidthbfPhoto.PixelHeight,同時也可以確保在縮略圖大小不超過規定的最大寬度和高度:

Private Function ResizeAndSaveFast(ByVal maxWidth As Integer, ByVal maxHeight As Integer, ByVal imageURL As String, ByVal saveToPath As String) As Boolean 

    ... 

    Dim bfPhoto As BitmapFrame = ReadBitmapFrame(memStream) 

    Dim scaleFactorWidth As Double = Math.Min(1.0, maxWidth/bfPhoto.PixelWidth) 
    Dim scaleFactorHeight As Double = Math.Min(1.0, maxHeight/bfPhoto.PixelHeight) 
    Dim scaleFactor As Double = Math.Min(scaleFactorWidth, scaleFactorHeight) 
    Dim newWidth As Integer = Math.Round(bfPhoto.PixelWidth * scaleFactor, 0) 
    Dim newHeight As Integer = Math.Round(bfPhoto.PixelHeight * scaleFactor, 0) 

    Dim bfResize As BitmapFrame = FastResize(bfPhoto, newWidth, newHeight) 

    ... 

End Function 
+0

Arghh ...時而在代碼尋找年齡後,你只需想念這樣的東西......謝謝! – Flo

+0

不客氣。 :-) – ConnorsFan