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).