【发布时间】:2018-08-06 21:15:53
【问题描述】:
我对 .Net 了解不多。每当我尝试运行 System.Drawing.Bitmap.FromFile(imagePath) 命令时,我都会遇到内存不足异常,请您帮忙。图像大小小于 1MB,而且它们似乎没有损坏(可以使用任何查看器打开它们),我也在之后处理图像(因为我注意到人们在不同的解决方案中建议这样做)
代码如下:
If My.Computer.FileSystem.FileExists(Server.MapPath(urlToProcess)) Then
output.Text = output.Text & "if succeed<br>"
imagePath = Server.MapPath(urlToProcess)
output.Text = output.Text & imagePath & " "
bmpImg = System.Drawing.Bitmap.FromFile(imagePath)
imgFormat = bmpImg.RawFormat
itemName = Path.GetFileNameWithoutExtension(Server.MapPath(urlToProcess))
If imgFormat.Equals(Imaging.ImageFormat.Gif) Then
gifIMG = True
'get gif image
bmpImg = System.Drawing.Bitmap.FromFile(imagePath)
'trim away extension from end
trimmedImgName = Path.GetFileNameWithoutExtension(Server.MapPath(urlToProcess))
'Map a new path
urlToProcess = currWorkingDir & "/" & trimmedImgName & ".jpg"
imagePath = Server.MapPath(urlToProcess)
'save to jpeg format
bmpImg.Save(imagePath, System.Drawing.Imaging.ImageFormat.Jpeg)
itemName = trimmedImgName
'Need to write delete function
End If
bmpImg.Dispose()
If Request.QueryString("s_r") = "checked" Then
width = Request.QueryString("s_w")
height = Request.QueryString("s_h")
AR = Request.QueryString("sAR")
measureType = Request.QueryString("spxpct")
resizeSourceImage(urlToProcess, itemName & "_s.jpg", height, width, AR, measureType)
'output.Text = output.Text & urlToProcess & "<br>"
End If
If Request.QueryString("n_r") = "checked" Then
width = Request.QueryString("n_w")
height = Request.QueryString("n_h")
AR = Request.QueryString("nAR")
measureType = Request.QueryString("npxpct")
resizeSourceImage(urlToProcess, itemName & "_n.jpg", height, width, AR, measureType)
'output.Text = output.Text & "n resize<br>"
End If
If Request.QueryString("l_r") = "checked" Then
width = Request.QueryString("l_w")
height = Request.QueryString("l_h")
AR = Request.QueryString("lAR")
measureType = Request.QueryString("lpxpct")
resizeSourceImage(urlToProcess, itemName & "_l.jpg", height, width, AR, measureType)
'output.Text = output.Text & "l resize<br>"
End If
'remove temp jpg file of a gif source
If gifIMG Then
If My.Computer.FileSystem.FileExists(Server.MapPath(urlToProcess)) Then
My.Computer.FileSystem.DeleteFile(Server.MapPath(urlToProcess))
End If
End If
Else
output.Text = output.Text & itemName & " cannot be found.<br>"
End If
【问题讨论】: