目的:
调整大小并保存图像
我使用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 width?
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>
答案 0 :(得分:1)
替换
' 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:\网站\实例\ ImageUploadAndResize \文件\ ResizeMe.png \ 1024x768ResizeMe.png