0

I am new to the web development world . I am trying to return a Bitmap Value from a web api to a javascript to be displayed on an HTML page but for some reason it is not doing so , here is my code :

in the webAPI :

    public Bitmap  gettestBitmap()
    {
        Bitmap theBM = new Bitmap(Properties.Resources._2_15_2013_5_08_48_PM);

        return theBM;
    }

in javascript , here is my code

var callstring = "api/Generator/gettestBitmap";
alert(callstring);
$.getJSON(callstring,
function (data) {
    alert(data);                                 
    anImage.dataSrc = data;
})
.fail(
function (jqXHR, textStatus, err)
{
    alert("All checks are correct, image was not      
           generated.\n jqXHR = " + jqXHR.responseText + "\n  
           textStatus=" + textStatus + " \n Error=" + err);});

the webAPI function gets called successfully and the data that comes back is System.Drawing.Bitmap and not the actual image

any clues how to fix this ?

Thanks!

2 Answers 2

1

Found the Solution!!!!

simply do the following :

var callstring = "api/Generator/gettestBitmap";
 $("#anImage").attr("src", callstring);
Sign up to request clarification or add additional context in comments.

Comments

0

You may want to return a byte array instead which will now be base64 encoded before being sent to the client.

 public byte[] gettestBitmap()
    {
        var imageBytes = File.ReadAllBytes(Properties.Resources._2_15_2013_5_08_48_PM);

    return imageBytes;
    }

You may want to change the image line as well.

anImage.attr("src", "data:image/png;base64," + data); // if format is jpeg change image/png to image/jpeg

5 Comments

thank you for your answer , I tried the suggested code , at the line theBM.Save(ms, theBM.RawFormat); I get an exception :{"Value cannot be null.\r\nParameter name: encoder"} any reason ?
@user1415780 What format is the image in? is it on the file system? does Properties.Resources._2_15_2013_5_08_48_PM point to the image directly? i may need to modify the code a bit after you respond.
the image is in png format , it is saved in the project resources and yes it points to the image directly
but my plan in the future is to create a Bitmap image and return it
@user1415780 i don't think you can do that. javascript has no notion of what a Bitmap image is, so you won't be able to use it in your code. What you need to send is either the url or a stream of bytes. i'm modifying my code now to solve your issue.

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.