我正在调整来自远程服务器的我自己的服务器上的图像。这很有效,但对于某些图像,它完全错误地调整了它。
但是,例如这个图像,工作非常糟糕,最终真的很小: 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
答案 0 :(得分:1)
您当前的代码在计算中仍使用bfPhoto.Width
和bfPhoto.Height
(对于"宽度"比例因子)。您可以尝试以下仅使用bfPhoto.PixelWidth
和bfPhoto.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