|
 |
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|

September 9th, 2003, 10:15 PM
|
 |
Friend of Wrox
Points: 16,481, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,409
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|

September 10th, 2003, 09:19 AM
|
 |
Friend of Wrox
Points: 16,481, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,409
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Solved!
I figured out the problem. The MemoryStream.GetBuffer() method returns the original byte array buffer from which the memory stream was created. Because I am creating the memory stream from a string there is no original buffer despite the fact that a string is *technically* a byte array. The solution was to use the GetArray() method which still returns a byte array, but this time generates it from the contents of the memory stream.
Hope that's of some use to someone else down the road!
Peter
|

November 17th, 2003, 04:44 PM
|
Registered User
|
|
Join Date: Nov 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for that tip! I was having the same problem by converting a string to byte array to memory stream as follows:
MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(myString));
When I try to call myStream.GetBuffer(), I get the exception. I changed it to myStream.ToArray(), and now works great.
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |