使用 ASP.NET 进行图像调整大小






1.67/5 (4投票s)
使用 ASP.NET 和 VB.NET 调整非常大的图像大小

介绍
有时,当我们上传图像并应用可变大小进行调整时,图像显示质量会下降。因此,本文将展示如何在不破坏图像质量的情况下调整图像大小。
背景
在本文中,我们将使用一些绘图方法。
使用代码
' Create new stream.
' Dim stream As New FileStream(fileName, FileMode.Open, FileAccess.Read)
' Create new image.
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(Stream)
' Calculate proportional max width and height.
Dim oldWidth As Integer = image.Width
Dim oldHeight As Integer = image.Height
If (CDec(oldWidth) / CDec(oldHeight)) > (CDec(newWidth) / CDec(newHeight)) Then
Dim ratio As Decimal = CDec(newWidth) / oldWidth
newHeight = CInt((oldHeight * ratio))
Else
Dim ratio As Decimal = CDec(newHeight) / oldHeight
newWidth = CInt((oldWidth * ratio))
End If
' Create a new bitmap with the same resolution as the original image.
Dim bitmap As New Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb)
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution)
' Create a new graphic.
Dim graphics__1 As Graphics = Graphics.FromImage(bitmap)
graphics__1.Clear(Color.White)
graphics__1.InterpolationMode = InterpolationMode.HighQualityBicubic
' Create a scaled image based on the original.
graphics__1.DrawImage(image, New Rectangle(0, 0, newWidth, newHeight),
New Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel)
graphics__1.Dispose()
' Save the scaled image.
bitmap.Save(Server.MapPath("Images/") & strDestinationFileName, image.RawFormat)