我试图理解为什么这段代码存储了一个纯黑色图像而不是我想要上传的图片(即图像永远不会存储,只是一个黑盒子,其测量值与我上传的图片相同) ?
Protected Sub btn_FileUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_FileUpload.Click
If FileUpload1.HasFile Then
Dim FileExtension As String = Path.GetExtension(FileUpload1.FileName.ToLower())
Dim allowedExtensions As String() = {".png", ".jpeg", ".jpg", ".gif"}
If Not FileUpload1.FileContent.Length > "1024000" Then
For i As Integer = 0 To allowedExtensions.Length - 1
If FileExtension = allowedExtensions(i) Then
Try
SaveImageFile(FileUpload1, Server.MapPath("~/Profile/ImageBank/Temp/image-" & GetStubs.GetUserGuid(HttpContext.Current.User.Identity.Name.ToString()) & ".jpg"), "600")
Response.Redirect(Utilities.Helpers.ResolveUrl("~/MyProfile.aspx?tab=crop"))
Catch ex As Exception
lblError.Text = "Bilden kunde inte laddas upp." + ex.Message.ToString()
End Try
End If
Next
Else
lblError.Text = "Filen du försöker att ladda upp är större en 1 Mb. Välj en annan bild eller spara bilden i ett annat filformat."
End If
End If
End Sub
Public Shared Sub SaveImageFile(ByVal clientFile As FileUpload, ByVal saveImagePath As String, ByVal maxImageWidth As Integer)
Dim sourceImage As New Bitmap(clientFile.PostedFile.InputStream)
' Resize if source image width is greater than the max:
If sourceImage.Width > maxImageWidth Then
Dim newImageHeight As Integer = CInt(sourceImage.Height * (CSng(maxImageWidth) / CSng(sourceImage.Width)))
Dim resizedImage As New Bitmap(maxImageWidth, newImageHeight)
' Save the resized image:
resizedImage.Save(saveImagePath, ImageFormat.Jpeg)
resizedImage.Dispose()
Else
' Save the source image (no resizing necessary):
sourceImage.Save(saveImagePath, ImageFormat.Jpeg)
sourceImage.Dispose()
End If
End Sub
这是形式......
<asp:FileUpload ID="FileUpload1" runat="server" />
<span class="button-field">
<asp:Button ID="btn_FileUpload" runat="server" Text="Ladda upp" OnClick="btn_FileUpload_Click" />
</span>
<asp:Label ID="lblError" CssClass="red" runat="server"></asp:Label>
你有什么好主意吗?
答案 0 :(得分:0)
我假设您正在上传gif或png。 gif和png支持透明度,但jpeg不支持透明度。保存为jpeg时,gif / png文件中的透明度/ alpha通道存储为黑色。
要保留透明度/ Alpha通道和清晰度,请将图像保存为原始文件类型。
答案 1 :(得分:0)
我通过将SaveImageFile方法更改为:
来解决这个问题 Public Shared Sub SaveImageFile(ByVal clientFile As FileUpload, ByVal saveImagePath As String, ByVal maxImageWidth As Integer)
Dim sourceImage As New Bitmap(clientFile.PostedFile.InputStream)
' Resize if source image width is greater than the max:
If sourceImage.Width > maxImageWidth Then
Dim newImageHeight As Integer = CInt(sourceImage.Height * (CSng(maxImageWidth) / CSng(sourceImage.Width)))
Dim resizedImage As New Bitmap(sourceImage, maxImageWidth, newImageHeight)
Dim canvas As Graphics = Graphics.FromImage(resizedImage)
' Save the resized image:
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
canvas.DrawImage(resizedImage, New Point(0, 0))
resizedImage.Save(saveImagePath, ImageFormat.Jpeg)
resizedImage.Dispose()
Else
' Save the source image (no resizing necessary):
sourceImage.Save(saveImagePath, ImageFormat.Jpeg)
sourceImage.Dispose()
End If
End Sub
感谢@Andrew Morton和@pasty向我指出这一点。 :)