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.
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).
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()
.
精彩评论