1

I'm trying to figure out how to send an Image via Json HttpResponseMessage from Server to Client as an Answer to a Post Request.

public async Task<HttpResponseMessage> PostRequest(QrTransactionModel requestViewModel) {

*do something*

Image img = qrGenerator.RenderQRWithPicture(transactionUrl);

QrLinKRefViewModel qrLinKRefViewModel = new QrLinKRefViewModel();
qrLinKRefViewModel.link = transactionUrl;
qrLinKRefViewModel.img = img;
qrLinKRefViewModel.refId = transactionId;


Request.CreateResponse(HttpStatusCode.Created, qrLinKRefViewModel);
}

If i send a Post Request via Postman I receive the answer:

{
"link": "link.com",
"refId": "SomeRefId",
"img": "System.Drawing.Bitmap"
}

But I cant see any image, also not in the preview, it just shows:

{"link":"link.com","refId":"SomeRefId","img":"System.Drawing.Bitmap"}

Saving the image in the function somewhere as a Jpeg is working pretty fine, but I could not figure out how to send it via Json. I found something about Base64 Encoding, but the snippets that I found did not really help me.

Thanks for your help!


Edit:

 public Image RenderQRWithPicture(String url)
    {
        String saveIn = pathPC + "Code1.jpg";
        qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H; //30%
        qrEncoder.QRCodeScale = 10;


        Bitmap img = this.qrEncoder.Encode(url);

        System.Drawing.Image logo = System.Drawing.Image.FromFile(System.Web.Hosting.HostingEnvironment.MapPath(@"~\QRCodes\Logo.jpg"));


        Graphics g = Graphics.FromImage(img);
        int left = (img.Width / 2) - (logo.Width / 2);
        int top = (img.Height / 2) - (logo.Height / 2);
        g.DrawImage(logo, new Point(left, top));


        img.Save(System.Web.Hosting.HostingEnvironment.MapPath(@"~\QRCodes\Code1.jpg"), ImageFormat.Jpeg);
       // img.Save(saveIn, ImageFormat.Jpeg);
        return img;

    }

This is my RenderQRWIthPicture Method (might help to understand the dataformat).

davidrue
  • 159
  • 11
  • 1
    What format do you want that "image" to take? Seems like Base64-encoding it to a string sounds reasonable. When you said "did not really help me" what do you mean? Did you *try*? What happened? – David Jun 02 '18 at 11:52
  • 1
    why dont you convert Image to string and set it in json? https://stackoverflow.com/questions/21325661/convert-image-path-to-base64-string – Daniel B Jun 02 '18 at 12:09
  • @DanielB Well, thank you. That already worked out for me. Pretty easier than I thought – davidrue Jun 02 '18 at 12:17

0 Answers0