ASP.NET 使用不同的 DPI 调整图像大小
ASP.NET image resizing with different DPI
我正在我自己的服务器上调整来自远程服务器的图像的大小。这非常有效,但对于某些图像,它调整大小完全错误。
但例如这张图片,效果非常糟糕,最终变得非常小:
http://www.topwedding.com/bigimage/Wedding%20Dresses/HS01116007/Floral-One-Shoulder-Organza-over-Satin-A-Line-Bridal-Gown-with-Pick-Ups.jpg
经检查发现第一张图片有 72 Pixels/inch,第二张图片有 951 Pixels/Inch。
大小调整错误与我之前遇到的问题相同(参见 )
但我认为现在可以通过在我的代码中使用 PixelWidth
和 PixelHeight
属性来解决这个问题,正如另一个 post.
中所建议的
目标是让缩略图的宽度为 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
您当前的代码在其计算中仍然使用 bfPhoto.Width
和 bfPhoto.Height
(对于 "width" 比例因子)。您可以试试下面的代码,它只使用 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
我正在我自己的服务器上调整来自远程服务器的图像的大小。这非常有效,但对于某些图像,它调整大小完全错误。
但例如这张图片,效果非常糟糕,最终变得非常小: http://www.topwedding.com/bigimage/Wedding%20Dresses/HS01116007/Floral-One-Shoulder-Organza-over-Satin-A-Line-Bridal-Gown-with-Pick-Ups.jpg
经检查发现第一张图片有 72 Pixels/inch,第二张图片有 951 Pixels/Inch。
大小调整错误与我之前遇到的问题相同(参见
但我认为现在可以通过在我的代码中使用 PixelWidth
和 PixelHeight
属性来解决这个问题,正如另一个 post.
目标是让缩略图的宽度为 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
您当前的代码在其计算中仍然使用 bfPhoto.Width
和 bfPhoto.Height
(对于 "width" 比例因子)。您可以试试下面的代码,它只使用 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