Asp.net System.Drawing.Bitmap.FromFile内存不足异常错误
我对.Net知识不多。每当我尝试运行System.Drawing.Bitmap.FromFile(imagePath)命令时,我都会遇到内存不足的异常,请您协助。图像大小小于1MB,并且它们似乎没有损坏(可以使用任何查看器打开),而且我正在处理之后的图像(正如我注意到人们在不同的解决方案中建议的那样) 代码如下:Asp.net System.Drawing.Bitmap.FromFile内存不足异常错误,asp.net,.net,bitmap,Asp.net,.net,Bitmap,我对.Net知识不多。每当我尝试运行System.Drawing.Bitmap.FromFile(imagePath)命令时,我都会遇到内存不足的异常,请您协助。图像大小小于1MB,并且它们似乎没有损坏(可以使用任何查看器打开),而且我正在处理之后的图像(正如我注意到人们在不同的解决方案中建议的那样) 代码如下: If My.Computer.FileSystem.FileExists(Server.MapPath(urlToProcess)) Then
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
如果存在My.Computer.FileSystem.files(Server.MapPath(urlToProcess)),则
output.Text=output.Text&“如果成功
”
imagePath=Server.MapPath(urlToProcess)
output.Text=output.Text&imagePath&“
bmpImg=System.Drawing.Bitmap.FromFile(imagePath)
imgFormat=bmpImg.RawFormat
itemName=Path.GetFileNameWithoutExtension(Server.MapPath(urlToProcess))
如果imgFormat.Equals(Imaging.ImageFormat.Gif),则
吉菲姆=真
'获取gif图像
bmpImg=System.Drawing.Bitmap.FromFile(imagePath)
'从末端修剪延伸部分
trimmedImgName=Path.GetFileNameWithoutExtension(Server.MapPath(urlToProcess))
"开辟新路",
urlToProcess=currWorkingDir&“/”&trimmedImgName&“.jpg”
imagePath=Server.MapPath(urlToProcess)
'保存为jpeg格式
bmpImg.Save(imagePath、System.Drawing.Imaging.ImageFormat.Jpeg)
itemName=trimmedImgName
'需要编写删除函数吗
如果结束
bmpImg.Dispose()
如果Request.QueryString(“s_r”)=“checked”,则
宽度=请求.查询字符串(“s_w”)
高度=请求查询字符串(“s_h”)
AR=请求.查询字符串(“sAR”)
measureType=Request.QueryString(“spxpct”)
resizeSourceImage(urlToProcess、itemName&“\u s.jpg”、高度、宽度、AR、度量类型)
'output.Text=output.Text&urlToProcess&'
“
如果结束
如果Request.QueryString(“n\u r”)=“选中”,则
宽度=请求.查询字符串(“n_w”)
高度=请求查询字符串(“n_h”)
AR=请求查询字符串(“nAR”)
measureType=Request.QueryString(“npxpct”)
resizeSourceImage(urlToProcess、itemName和“\u n.jpg”、高度、宽度、AR、度量类型)
'output.Text=output.Text&'n resize
'
如果结束
如果Request.QueryString(“l_r”)=“选中”,则
宽度=请求.查询字符串(“l_w”)
高度=请求查询字符串(“l_h”)
AR=请求.查询字符串(“lAR”)
measureType=Request.QueryString(“lpxpct”)
resizeSourceImage(urlToProcess、itemName和“_l.jpg”、高度、宽度、AR、度量类型)
'output.Text=output.Text&'l resize
'
如果结束
'删除gif源的临时jpg文件
如果吉菲姆那么
如果存在My.Computer.FileSystem.files(Server.MapPath(urlToProcess)),则
My.Computer.FileSystem.DeleteFile(Server.MapPath(urlToProcess))
如果结束
如果结束
其他的
找不到output.Text=output.Text&itemName&。
“
如果结束
System.Drawing.Bitmap实现IDisposable
试着说:
using bmpImg = System.Drawing.Bitmap.FromFile(imagePath)
End Using
我得到一个编译错误。使用bmpImg中的“使用”似乎是导致ITI的原因。如果您对选项要求严格,则需要使用bmpImg作为位图写出完整的
。另外,为了避免不必要的强制转换,我建议您使用新位图(imagePath)
而不是FromFile(imagePath)
。在内部,它也是这样做的,但是这个函数实际上返回了一个类型为Bitmap
的对象,而不是Image
@nyrguds。实际上,它在前面被声明为“Dim bmpImg as Bitmap”,使用的变量从未在前面声明过。它们应该只存在于“使用”块中。