Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Professional
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 9th, 2003, 11:15 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default 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
__________________
-Peter
compiledthoughts.com
twitter/peterlanoie
Reply With Quote
  #2 (permalink)  
Old September 10th, 2003, 10:19 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,413
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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
Reply With Quote
  #3 (permalink)  
Old November 17th, 2003, 05:44 PM
Registered User
 
Join Date: Nov 2003
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
filling RichTextBox using MemoryStream melvik C# 8 August 30th, 2011 03:25 AM
Java IO error bb1234_00 Java Basics 2 March 26th, 2007 08:25 AM
System.IO.FileNotFoundException cuca_macaii2000 BOOK: ASP.NET Website Programming Problem-Design-Solution 1 February 5th, 2005 10:10 AM
Writing more than 1Kb to a MemoryStream owain Pro VB.NET 2002/2003 1 November 4th, 2004 09:59 AM
C# File IO IsFileOpen WGP3 C# 4 August 4th, 2004 08:35 AM



All times are GMT -4. The time now is 07:18 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.