Giri Dharan
Giri Dharan

Reputation: 161

Convert Image to Memory Stream data in Javascript

I generate a qr code image using jquery.qrcode library. I get the image in the following format.

<img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"/>

I want to print this image with some text added to it(since this is a qrcode). I know to print the image when the image is drawn from backend of C#. Is there anyway I can convert this image to memory stream data to send to C# or anyway that I can print from Javascript when the image is drawn?

Upvotes: 0

Views: 4625

Answers (2)

Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24600

You can print directly from JavaScript, you don't need to convert or send it to c#. The c# is in back-end, and I believe you need to print on the front-end (end-user machine)

You can use IFRAME and add the image your text to it.

After that you can print the IFRAME.

http://jsfiddle.net/8gcz0dw7/

A working Example

iframe=document.createElement('iframe')
document.body.appendChild(iframe)
var co=iframe.contentDocument.body;
$(co).append('your text<img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"/>')
iframe.focus()
iframe.contentWindow.print()

Upvotes: 0

Bhavik Patel
Bhavik Patel

Reputation: 1464

send your image base64 url using ajax call

on C# side use this function

public Image Base64ToImage(string base64String)
{
  // Convert Base64 String to byte[]
  byte[] imageBytes = Convert.FromBase64String(base64String);
  MemoryStream ms = new MemoryStream(imageBytes, 0, 
    imageBytes.Length);

  // Convert byte[] to Image
  ms.Write(imageBytes, 0, imageBytes.Length);
  Image image = Image.FromStream(ms, true);
  return image;
}

Upvotes: 3

Related Questions