我有一个简单的处理程序(ASHX),它接收一个基本的64位编码图像,我想保存该图像。
以下是代码:
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim imageInfo = context.Request("imgData")
Dim imageData As String() = imageInfo.Split(",")
' check that there are two parts; ie the header and the string data.
If imageData.Length < 2 Then
context.Response.Write("error")
Return
End If
' Example of imageInfo
' data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAE...
Dim imageType As String = imageData(0)
Dim imageString As String = imageData(1)
If imageType <> "data:image/png;base64" Then
context.Response.Write("error")
Return
End If
imageString = imageString.Replace(" ", "+")
Dim bytes As Byte()
bytes = Convert.FromBase64String(imageString)
Dim image As System.Drawing.Image
Using ms As MemoryStream = New MemoryStream(bytes)
image = System.Drawing.Image.FromStream(ms)
End Using
image.Save("test", System.Drawing.Imaging.ImageFormat.Png)
End Sub
但是,我收到以下错误:
类型的例外 &#39; System.Runtime.InteropServices.ExternalException&#39;发生在 System.Drawing.dll但未在用户代码中处理
其他信息:GDI +中发生了一般性错误
我知道图像数据很好,因为它适用于php版本。我正在尝试将PHP转换为ASP.NET。
/*get file name and image data from POST*/
$filename = $_POST['filename'];
$data = $_POST['imgdata'];
list( $type, $data ) = explode( ';', $data );
list( $base, $data ) = explode( ',', $data );
/*check that image data is base64 of png*/
if ( $type === 'data:image/png' && $base === 'base64') {
$data = base64_decode( $data );
file_put_contents( '../pathtosave/' . $filename, $data );
echo 'saved';
} else {
echo 'error';
}
检查调试器中的图像,似乎没问题。它正确地获取图像的高度和宽度。
任何人都可以看到错误的位置吗?
答案 0 :(得分:0)
我很尴尬。问题在于&#34;测试&#34;不是正确的文件路径。我正在我的本地机器上进行测试,一旦我使用了完整路径,就可以立即使用。