IO.MemoryStream from a string
Here's the scenario:
I have a routine that returns a memory stream. The data for the memory stream is either coming from System.Drawing.Image.Save() or from New System.IO.MemoryStream(Convert.FromBase64String(<s tring>)). The condition is based on whether the string data is found in an XML object. The MemoryStream is subsequently written to the text stream of a page request using Response.BinaryWrite(objStream.GetBuffer()).
<snip from routine>
If Not objNode Is Nothing Then
objStream = New System.IO.MemoryStream(Convert.FromBase64String(ob jNode.InnerText))
objResults.Stream = objStream
objResults.Success = True
Else
objImage = System.Drawing.Image.FromFile(sImagePath)
objStream = New System.IO.MemoryStream()
objImage.Save(objStream, System.Drawing.Imaging.ImageFormat.Jpeg)
objElement = objXML.CreateElement("Thumbnail")
objElement.Attributes.Append(objXML.CreateAttribut e("path"))
objElement.Attributes("path").InnerText = sImagePath
objElement.InnerText = Convert.ToBase64String(objStream.GetBuffer())
objXML.DocumentElement.AppendChild(objElement)
objResults.Stream = objStream
objResults.Success = True
End If
Return objResults
</snip>
<snip from calling page>
objGetImageResults = PBXML.GetImageThumbnail(sFullFileName)
If objGetImageResults.Success Then
objStream = objGetImageResults.Stream
End If
Response.Clear()
Response.ContentType = "image/jpeg"
Response.BinaryWrite(objStream.GetBuffer())
</snip>
The XML read/write works fine. When the MemoryStream is returned from the image save method (else condition above), there is no problem. But when it is returned from the conversion from the string the stream returns just fine from the routine but I get a crash on the BinaryWrite call. The error I get is this:
MemoryStream's internal buffer cannot be accessed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.UnauthorizedAccessException: MemoryStream's internal buffer cannot be accessed.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET write access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
I'm not working with a file nor anything else that should require any kind of access rights, just some objects stored in memory. The structure of the memory stream looks the same when generated by either condition in the routine. It's got me baffled.
Anyone have an idea?
Peter
|