1

I have an ASP.NET page where I am rendering some HTML markup which includes a barcode image which is generated by another asp.net page

 <div id='divDynamic'>
   <h1>Some content</h1>
   <img src='barcode.aspx?mode=something' />
 </div>

And in the barcode.aspx.cs, I have:

 protected void Page_Load(object sender, EventArgs e)
 {
    Response.CacheControl = "no-cache"; 
    Response.AddHeader("Pragma", "no-cache");
    Response.Expires = -1 ; 
    Response.Buffer = false;
    Response.ContentType = "image/JPEG";
    MemoryStream ms = new MemoryStream();    

    System.Drawing.Image objBitmap = GenCode128.Code128Rendering.MakeBarcodeImage(Request.QueryString["mode"] + "", 2,false );
    objBitmap.Save(ms ,ImageFormat.Bmp); 

    Response.BinaryWrite(ms.GetBuffer());
    Response.End();
}

I need to use the same functionality in many similar websites. So now I am trying to convert this to a WCF service where the markup for the div "divDynamic" will be generated and will be sent back to the client (an asp.net website). My service has a method of return type string which will return the HTMLMarkup like the following

 public string GetUSPSLabelMarkup()
 {
        StringBuilder strHtml = new StringBuilder();
        strHtml.Append("<h1>Some content</h1>");
        // How do I have the barcode image here?

        return strHtml.ToString();

    }

I am wondering how should I have the Image generation part in the above method in my service? I believe Response.BinaryWrite shouldn't work here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shyju
  • 214,206
  • 104
  • 411
  • 497

2 Answers2

2

If you're sending back HTML, it means the consumer can understand the IMG tag as well, so why don't you send this:

 strHtml.Append("<h1>Some content</h1>");
 strHtml.Append("<img src='barcode.aspx?mode=something' />");

what's the problem with this?

PS: you can simplify your streaming code like this (no need to create a memory stream and a byte array that will slowly kill your server's heap):

protected void Page_Load(object sender, EventArgs e)
 {
    Response.ContentType = "image/JPEG";
    System.Drawing.Image objBitmap = GenCode128.Code128Rendering.MakeBarcodeImage(Request.QueryString["mode"] + "", 2,false );
    objBitmap.Save(Response.OutputStream, ImageFormat.Bmp); 
}

Also, make sure you have consistency between the contentType and the ImageFormat (you declare JPEG on one, and BMP on the other).

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • I dont want to have barcode.aspx in the consumer side anymore.The barcode image generation should be in the WCF service.Not at client side(asp.net app) – Shyju Dec 09 '10 at 15:11
  • If you're sending HTML, you can only build an IMG SRC tag that will answer HTTP request (what you already have). If you want a WCF service, you won't be servicing HTML. You need to make a choice. HTML or WCF? If it's WCF, then see this SO thread: http://stackoverflow.com/questions/592505/how-can-a-wcf-service-return-large-amounts-of-data – Simon Mourier Dec 09 '10 at 15:14
  • GenCode128 can also be installed from NuGet: https://www.nuget.org/packages/GenCode128/ – Nikolay Kostov Nov 21 '15 at 16:01
1

I think there's a conceptual mistake in there.

Even in WCF, you will still have two separate calls:

  • Client loads HTML Markup with the <img> reference
  • Client loads binary image data from server

So you'll have two methods on the WCF-Service: string GetUSPSLabelMarkup() and byte() GetUSPSLabelImageData().

wagi
  • 448
  • 4
  • 12