views:

383

answers:

3

I have a generic handler in vb.net that builds a chart and then returns a png as a result.

Relevant code on /GetChart.ashx (which is actually called as /GetChart.ashx?report=1):

AssetChart.RenderType = RenderType.BinaryStreaming

Dim mstream As New MemoryStream()
AssetChart.SaveImage(mstream, ChartImageFormat.Png)
Dim byteArray As Byte() = mstream.ToArray()

context.Response.Clear()
context.Response.ContentType = "image/png"
context.Response.AddHeader("Content-Length", byteArray.Length.ToString())

context.Response.BinaryWrite(byteArray)
context.Response.Flush()
context.Response.Close()

When I try and hit this page via FireFox or IE I DO receive the PNG image in the browser without any errors.

BUT, when I try and call this handler from another generic handler, I receive the parameter is invalid when calling FromStream for the image:

url = "http://www.google.com/images/logos/ps_logo2.png"    
url = "http://mysite/GetChart.ashx?report=1"


Dim HttpWebRequest As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim HttpWebResponse As HttpWebResponse = DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse)

Dim respStream As Stream = HttpWebResponse.GetResponseStream()

Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(respStream)
image.Save("C:\test.png", ImageFormat.Png)

If I comment out this line and use the Google image to test instead ...

url = "http://mysite/GetChart.ashx?report=1"

... it WORKS, so that leads me to believe that the problem lies in the FIRST handler (GetChart.ashx) and somehow it's not delivering exactly what I'm looking for even though the browsers handle it as expected?

Any thoughts or assistance would be greatly appreciated.

Thank you!

A: 

First thought: try dumping the contents of the stream to disk, as the easiest way of seeing what's actually being delivered. Compare it to the original file.

Second thought: use WireShark to see what's happening at the HTTP level.

Jon Skeet
Thanks for the thoughts. I can't really get back into the project until tomorrow morning (no vpn access to pull off the rest of the surrounding code and too much work to just work it up alone), but I appreciate the post.
2GDave
I don't have 15 points yet, otherwise I'd bump this up. That's the right thing to do, right? You might not have answered it, but gave a valid thought or two with a link, etc. Right?
2GDave
+1  A: 

Can you simplify your code to:

context.Response.Clear()
context.Response.ContentType = "image/png"
AssetChart.SaveImage(context.Response.OutputStream, ChartImageFormat.Png)

and see what that gets you?

Jesse C. Slicer
As I said above, can't hit it tonight, but this will be the first thing I try in the morning. Thanks guys, I'll let you know how it goes.
2GDave
Same here - I don't have 15 points yet, otherwise I'd bump this up TOO. That's the right thing to do, right? You might not have answered it for sure but gave a good piece of code to try?
2GDave
I think you should only upvote if it helps you toward solving the issue. If it doesn't, then just leave it alone.
Jesse C. Slicer
You don't think it should get an upvote even if the person put together a really great post with a few options (and possibly code that might help someone else), but none of them happened to work for me?
2GDave
If it helps me towards my issue, I should click the check mark, right? So isn't that different?
2GDave
Vote your conscience, that's all any of us can do :)
Jesse C. Slicer
Nope, I get the same Parameter is not valid on the FromStream method: Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(respStream)
2GDave
FWIW, this is what I get when I view the response headers from calling the initial handler from the browser.Cache-Control: no-cache, no-storePragma: no-cacheContent-Type: image/pngExpires: -1Server: Microsoft-IIS/7.5X-Powered-By: ASP.NETDate: Thu, 09 Sep 2010 12:58:06 GMTContent-Length: 30116
2GDave
Aha - I don't have it fixed but have figured out it's not really getting the correct stream because of permissions. When I check out the ResponseURI on the HttpWebResponse object, I get this:http://mysite/login.aspx?ReturnUrl=/CurrentAssetsChart.ashx?report=1Is there a way I can persist who I am in that response?
2GDave
A: 

I added a section to the web.config to allow all users to hit GetChart.ashx and now the request goes through and the image is retrieved.

<location path="GetChart.ashx">
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>
  </location>
2GDave