Asp.net 在VB.NET中调整图像大小
我的IHttpHandler中有以下代码:Asp.net 在VB.NET中调整图像大小,asp.net,vb.net,image,ihttphandler,Asp.net,Vb.net,Image,Ihttphandler,我的IHttpHandler中有以下代码: Dim MemoryStream1 As New System.IO.MemoryStream MemoryStream1.Write(SqlDataReader1("cover"), 0, SqlDataReader1("cover").Length - 1) Dim Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitm
Dim MemoryStream1 As New System.IO.MemoryStream
MemoryStream1.Write(SqlDataReader1("cover"), 0, SqlDataReader1("cover").Length - 1)
Dim Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(MemoryStream1)
Dim Width1 As Integer = Bitmap1.Width
Dim Height1 As Integer = Bitmap1.Height
Dim Width2 As Integer = 90
Dim Height2 As Integer = Height1 * Width1 / Width1
Dim Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2)
Dim Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2)
Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2)
Dim MemoryStream2 As New System.IO.MemoryStream
Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png)
context.Response.BinaryWrite(MemoryStream2.ToArray)
它可以工作,但我不确定它是否是调整图像大小的正确方法。如何简化代码
提前谢谢 Public函数将Image(imgToResize作为Image,size作为size)的大小调整为Byte()
Public Function ResizeImage(imgToResize As Image, size As Size) As Byte()
Dim sourceWidth As Integer = imgToResize.Width
Dim sourceHeight As Integer = imgToResize.Height
Dim nPercent As Single = 0
Dim nPercentW As Single = 0
Dim nPercentH As Single = 0
nPercentW = (CSng(size.Width) / CSng(sourceWidth))
nPercentH = (CSng(size.Height) / CSng(sourceHeight))
If nPercentH < nPercentW Then
nPercent = nPercentH
Else
nPercent = nPercentW
End If
Dim destWidth As Integer = CInt(Math.Truncate(sourceWidth * nPercent))
Dim destHeight As Integer = CInt(Math.Truncate(sourceHeight * nPercent))
Dim b As New Bitmap(destWidth, destHeight)
Dim g As Graphics = Graphics.FromImage(DirectCast(b, Image))
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight)
g.Dispose()
Return b.ToByteArray()
End Function
Dim sourceWidth As Integer=imgToResize.Width
Dim sourceHeight作为整数=imgToResize.Height
单个尺寸百分比=0
尺寸nPercentW为单个=0
尺寸nPercentH为单个=0
nPercentW=(CSng(size.Width)/CSng(sourceWidth))
nPercentH=(CSng(size.Height)/CSng(sourceHeight))
如果nPercentH
此函数将图像调整为指定大小,并保持其比例。它是用C#写的,我把它通过一个在线转换器,所以可能不是100%正确
ToByteArray()是我编写的一个扩展方法,用于将图像存储在数据库中,如果您愿意,我也可以提供给您。公共函数ResizeImage(imgToResize作为图像,size作为大小)作为Byte()
Dim sourceWidth As Integer=imgToResize.Width
Dim sourceHeight作为整数=imgToResize.Height
单个尺寸百分比=0
尺寸nPercentW为单个=0
尺寸nPercentH为单个=0
nPercentW=(CSng(size.Width)/CSng(sourceWidth))
nPercentH=(CSng(size.Height)/CSng(sourceHeight))
如果nPercentH
此函数将图像调整为指定大小,并保持其比例。它是用C#写的,我把它通过一个在线转换器,所以可能不是100%正确
ToByteArray()是我编写的一个扩展方法,用于将图像存储在DB中,如果您愿意,我也可以提供给您。基本上代码是正确的,但它存在一些问题:
- 写入第一个内存流时跳过最后一个字节。
调用中的最后一个属性应该是长度,而不是长度减去1Write
- 您对高度2的计算不正确。表达式
将始终计算为Height1*Width1/Width1
的值。您应该改用Height1
Height1*Width2/Width1
- 您没有处理内存流、位图或图形对象。使用
块以确保对象已被释放使用
Using MemoryStream1 As New System.IO.MemoryStream(SqlDataReader1("cover"))
Using Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(MemoryStream1)
Dim Width1 As Integer = Bitmap1.Width
Dim Height1 As Integer = Bitmap1.Height
Dim Width2 As Integer = 90
Dim Height2 As Integer = Height1 * Width2 / Width1
Using Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2)
Using Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2)
Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2)
End Using
Using MemoryStream2 As New System.IO.MemoryStream
Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png)
context.Response.BinaryWrite(MemoryStream2.ToArray)
End Using
End Using
End Using
End Using
代码基本上是正确的,但存在一些问题:
- 写入第一个内存流时跳过最后一个字节。
调用中的最后一个属性应该是长度,而不是长度减去1Write
- 您对高度2的计算不正确。表达式
将始终计算为Height1*Width1/Width1
的值。您应该改用Height1
Height1*Width2/Width1
- 您没有处理内存流、位图或图形对象。使用
块以确保对象已被释放使用
Using MemoryStream1 As New System.IO.MemoryStream(SqlDataReader1("cover"))
Using Bitmap1 As System.Drawing.Bitmap = System.Drawing.Bitmap.FromStream(MemoryStream1)
Dim Width1 As Integer = Bitmap1.Width
Dim Height1 As Integer = Bitmap1.Height
Dim Width2 As Integer = 90
Dim Height2 As Integer = Height1 * Width2 / Width1
Using Bitmap2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(Width2, Height2)
Using Graphics1 As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(Bitmap2)
Graphics1.DrawImage(Bitmap1, 0, 0, Width2, Height2)
End Using
Using MemoryStream2 As New System.IO.MemoryStream
Bitmap2.Save(MemoryStream2, System.Drawing.Imaging.ImageFormat.Png)
context.Response.BinaryWrite(MemoryStream2.ToArray)
End Using
End Using
End Using
End Using
下面是它的问题列表:我建议使用,这不是你可以塞进1页甚至10页代码而不会在系统中出现严重缺陷的东西。下面是它的问题列表:我建议使用,如果系统中没有严重的缺陷,你无法将其塞进1页甚至10页的代码中。这将缩小图像,使整个图像适合
大小
尺寸(因此,如果显示在与原始图像具有不同纵横比的尺寸
标注容器上,则空格可能会显示为左下或上下)。要放大图像以填充所有尺寸
标注,只需将中的
更改为,如果nPercentH行更改为。(但这实际上会返回一个大于大小
尺寸的图像,然后取决于您将使用图像的哪一部分以及如何使用-例如,从左上角或居中部分,显示在具有大小
尺寸的容器中)这将缩小图像,以便整个图像适合大小
尺寸(因此,如果显示在与原始图像具有不同纵横比的尺寸
标注容器上,则空格可能会显示为左下或上下)。要放大图像以填充所有尺寸
标注,只需将中的
更改为,如果nPercentH行更改为。(但这实际上会返回一个大于大小的
尺寸的图像,然后取决于您是哪一部分