Vb.net 调整大小并上传图像
目标: 调整大小并保存图像 我正在借鉴,但正在努力克服这一障碍。我通常使用Vb.net 调整大小并上传图像,vb.net,Vb.net,目标: 调整大小并保存图像 我正在借鉴,但正在努力克服这一障碍。我通常使用FileUpload1.SaveAs(uploadFolder&“\”+vFileName)当我只直接上传而没有任何问题时 发行行: bmp.Save(uploadFolder & "\" & imgStream, System.Drawing.Imaging.ImageFormat.Png) 错误: Operator '&' is not defined for types
FileUpload1.SaveAs(uploadFolder&“\”+vFileName)
当我只直接上传而没有任何问题时
发行行:
bmp.Save(uploadFolder & "\" & imgStream, System.Drawing.Imaging.ImageFormat.Png)
错误:
Operator '&' is not defined for types 'String' and 'System.IO.MemoryStream'. C:\sites\Examples\ImageUploadAndResize\Resize.aspx.vb 50 18 C:\...\ImageUploadAndResize\
代码-aspx.vb
Imports System.IO
Imports System.Drawing
Partial Class Resize
Inherits System.Web.UI.Page
'https://stackoverflow.com/questions/8431062/resizing-an-image-in-vb-net?rq=1
Protected Sub btnResize_Click(sender As Object, e As System.EventArgs) Handles btnResize.Click
Dim vName As String = FileUpload1.PostedFile.FileName
Dim uploadFolder As String = "C:\sites\Examples\ImageUploadAndResize\File\" & vName & "\"
Const maxWidth As Integer = 1024
Const maxHeight As Integer = 768
Dim newImage As Image = Image.FromFile(vName)
Dim percentToShrink As Double = -1
If newImage.Width >= newImage.Height Then
' Do we need to resize based on width?
If newImage.Width > maxWidth Then
percentToShrink = CDbl(maxWidth) / CDbl(newImage.Width)
End If
Else
' Do we need to resize based on Height?
If newImage.Height > maxHeight Then
percentToShrink = CDbl(maxHeight) / CDbl(newImage.Height)
End If
End If
Dim newWidth As Integer = newImage.Width
Dim newHeight As Integer = newImage.Height
' So do we need to resize?
If percentToShrink <> -1 Then
newWidth = CInt(Math.Truncate(newImage.Width * percentToShrink))
newHeight = CInt(Math.Truncate(newImage.Height * percentToShrink))
End If
' convert the image to a png and get a byte[]
Dim imgStream As New MemoryStream()
Dim bmp As New Bitmap(newWidth, newHeight)
Using g As Graphics = Graphics.FromImage(bmp)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g.FillRectangle(System.Drawing.Brushes.White, 0, 0, newWidth, newHeight)
g.DrawImage(newImage, 0, 0, newWidth, newHeight)
End Using
' This can be whatever format you need and save
bmp.Save(uploadFolder & "\" & imgStream, System.Drawing.Imaging.ImageFormat.Png)
Dim imgBinaryData As Byte() = imgStream.ToArray()
imgStream.Dispose()
End Sub
End Class
Imports System.IO
导入系统。绘图
部分类调整大小
继承System.Web.UI.Page
'https://stackoverflow.com/questions/8431062/resizing-an-image-in-vb-net?rq=1
受保护的子btnResize\u Click(发件人作为对象,e作为System.EventArgs)处理btnResize。单击
Dim vName As String=FileUpload1.PostedFile.FileName
Dim uploadFolder为String=“C:\sites\Examples\ImageUploadAndResize\File\”&vName&“\”
常量maxWidth为整数=1024
常量maxHeight为整数=768
Dim newImage As Image=Image.FromFile(vName)
Dim PERCENTTOSHINK As Double=-1
如果newImage.Width>=newImage.Height,则
'是否需要根据宽度调整大小?
如果newImage.Width>maxWidth,则
PercentToScrink=CDbl(最大宽度)/CDbl(newImage.Width)
如果结束
其他的
'我们是否需要根据高度调整大小?
如果newImage.Height>maxHeight,则
PercentToScrink=CDbl(最大高度)/CDbl(新图像高度)
如果结束
如果结束
Dim newWidth作为整数=newImage.Width
Dim newHeight为整数=newImage.Height
“那么我们需要调整尺寸吗?
如果百分比为-1,则
newWidth=CInt(数学截断(newImage.Width*percentToShrink))
newHeight=CInt(数学截断(newImage.Height*percentToShrink))
如果结束
'将图像转换为png并获取字节[]
将imgStream作为新的MemoryStream()进行调整
将bmp调整为新位图(新宽度、新高度)
使用g作为Graphics=Graphics.FromImage(bmp)
g、 插值模式=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
g、 FillRectangle(System.Drawing.Brusks.White、0、newWidth、newHeight)
g、 DrawImage(新图像、0、0、新宽度、新高度)
终端使用
“这可以是您需要并保存的任何格式
bmp.Save(上传文件夹&“\”&imgStream,System.Drawing.Imaging.ImageFormat.Png)
Dim imgBinaryData作为字节()=imgStream.ToArray()
imgStream.Dispose()
端接头
末级
aspx
更换
' This can be whatever format you need and save
bmp.Save(uploadFolder & "\" & imgStream, System.Drawing.Imaging.ImageFormat.Png)
与
使用1200 x 768像素的ResizeMe.png文件,您将获得
C:\sites\Examples\ImageUploadAndResize\File\ResizeMe.png\1024x768ResizeMe.pngimgStream应该是一个文件名,而不是图像的数据
imgStream
是一个包含数据的对象,不是字符串变量或文件夹/文件名我有点迷路了,我不能使用“vName”,因为这是原始文件,而不是调整大小的文件。如果你能让我看看,那就太好了。我假设更像bmp.Save(uploadFolder&“\”&vNamexxx,System.Drawing.Imaging.ImageFormat.Png),但是我从哪里获得大小合适的vNamexxx图像呢?
' This can be whatever format you need and save
bmp.Save(uploadFolder & "\" & imgStream, System.Drawing.Imaging.ImageFormat.Png)
Dim resizedImageName As String = uploadFolder & newWidth & "x" & newHeight & vname
' This can be whatever format you need and save
bmp.Save(resizedImageName , System.Drawing.Imaging.ImageFormat.Png)