Skip to main content
 首页 » 编程设计

vb.net中调整大小并上传图像

2024年12月31日79lidabo

目标:

调整图像大小并保存 我正在使用Resizing an image in VB.NET供引用,但正在努力克服保存障碍。当我只是直接上传而没有任何问题时,我通常使用 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 

aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Resize.aspx.vb" Inherits="Resize" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
      <asp:FileUpload ID="FileUpload1" runat="server" /> 
        <asp:Button ID="btnResize" runat="server" Text="Resize" /> 
    </div> 
    </form> 
</body> 
</html> 

请您参考如下方法:

替换

' 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) 

如果文件名为 ResizeMe.png,分辨率为 1200 x 768 像素,您将得到以下输出: C:\sites\Examples\ImageUploadAndResize\File\ResizeMe.png\1024x768ResizeMe.png