Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have my custom httphandler which calls a wcf service. By Service returns a Stream

[OperationContract]
Stream GetMyStream(int width, int height);

I am calling the http handler in my aspx page from an image tag:

<asp:Image ID="imgStream" runat="server" ImageUrl="MyStreamHandler.ashx" Visible="true" />

In my handler, I have a reference to the WCF service and I am calling the operation as below:

MyServiceClient tcpClient = new MyServiceClient();
Image img = Image.FromStream(tcpClient.GetMyStream(30,100));

I am using NetTcp binding in my service. Now, I can call the http handler in my aspx page through jquery ajax and display the stream in the image tag so as to avoid post backs on the page.

Thanks.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I do not think you will need to use ajax, you could just use the below and add the image reference to point to the handler in the src.

Please note the image type will need to match your image.

public class MyStreamHandler : IHttpHandler {

public void ProcessRequest (HttpContext context) {

    MyServiceClient tcpClient = new MyServiceClient();
    Image img = Image.FromStream(tcpClient.GetMyStream(30,100));
    img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)  
        img.Dispose()
    }

    public bool IsReusable {
    get {
        return false;
    }
   }
}
share|improve this answer
    
We don't need to use AJAX. But, I want to avoid the post back each time on the page when I call my http handler. I tried this: ` function GetImage() { var url = 'MyStreamHandler.ashx?w=' + 300 + '&h=' + 75; $("#div1").append($("<img src='" + url + "' />")); } <input type="submit" name="btnSubmit" value="Refresh" onclick="GetImage();" /> ` It is still resulting in postbacks. –  PushCode Jun 27 '13 at 20:55
    
Sorry for bad formating. i tried formating the code, but i couldn't achieve it –  PushCode Jun 27 '13 at 20:56
    
No problem, your inputs are type="submit", this will cause a submit back to the page. You can either change your type or use jQuery to remove the default event. –  hutchonoid Jun 27 '13 at 21:03
    
I used jQuery and is working fine –  PushCode Jul 1 '13 at 16:14
    
Nice one, thanks. –  hutchonoid Jul 1 '13 at 17:20

You should use the native file handler of IIS to return the contents of an image file as opposed to running the stream through managed code. While hutchonoid's answer works, I would like to point out that the end result will probably be less scalable since it is writing out the stream to an Image object in memory and then sending that back through the response stream.

It will probably work just fine for most work loads, just saying it's actually less elegant a solution even though it is harder to implement. If anything, your service would probably be best if it uploaded to some CDN and then returned an image path to that resource as opposed to serving it through an httphandler.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.