0

I have a web service that contains function returning array of bits. I want to use javascript and html5 to draw this array of bits in imagebox for example , the array of bits form image

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
         CodeBehind="Default.aspx.cs" Inherits="testDICOMImageDraw._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  <script type="text/javascript">
    alert("WebService1.asmx");
    var data;

    function draw(data) {
        alert("withing the function");
        var i = 0,
            image = document.getElementById('image'),
            pixel;
        for (; i < data.length; i++) {
            pixel = document.createElement('div');
            pixel.className = (data[i]) ? 'on' : 'off';
            image.appendChild(pixel)
        }
    }

    function fail() {
        alert('request failed');
    }

    data = WebService1.imageArray('s//s/', draw, fail);

  </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <div id="image">
  </div>
</asp:Content>

The web service function is

[WebMethod]
public byte[] imageArray(string path)
    {
        return new byte[] { 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0 };
    } 

Both the site and the web service are in the same project.

4
  • 1
    imagebox is what? Why does the webservice not just return the image? document.getElementById("imgId").src = "yourWebservice.php"; Commented Aug 19, 2011 at 19:39
  • Have a look at the canvas element - diveintohtml5.org/canvas.html Commented Aug 19, 2011 at 19:39
  • 4
    You'll need to provide more information on the "array of bits" returned by your webservice call. Is it in a JSON string representation? "[1,0,0,1,0,1,1,0]" for example? To draw it on the screen, you would use a <canvas>. Commented Aug 19, 2011 at 19:40
  • yes it's [1,0,0,1,0,1,1,0] like array Commented Aug 31, 2011 at 19:38

1 Answer 1

4

Use the following code. Replace WebService1.getImage with your service name and the function that returns your data.

<div id="image"></div>
<script type="text/javascript">
function draw(data)
{
    var canvas = document.getElementById('image'),
        ctx = canvas.getContext('2d'),
        imgLength = 4,
        size = 50;

    canvas.width = canvas.height = 200;

    for(var x = 0; x < imgLength; x++){
       for(var y = 0; y < imgLength; y++){
           if(data[y * imgLength + x]){
               ctx.fillRect(x*size, y*size, size, size)
           }
       } 
    }
}

function fail()
{
    alert('request failed');
}

WebService1.getImage(draw, fail);
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I update my question with code , but it doesn't work , any suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.