i am woring in Asp.net(C#) .i am trying convert html to PDF using Pechkin, PDF convert successfully.., but in my html tag is their that image is not showing in PDF,please Help Me
My html code is
<img id="ContentPlaceHolder1_img_header"
src="/CompanyLogos/12_09_YYYY_07_41_48_logo.png" style="width:130px"
and my .cs page Code IS
byte[] pdfContent = new SynchronizedPechkin(
new GlobalConfig())
.Convert(
new ObjectConfig()
.SetLoadImages(true)
.SetPrintBackground(true)
.SetCreateExternalLinks(true)
.SetAllowLocalContent(true),
html);
Msg.Attachments.Add(new Attachment(new MemoryStream(pdfContent), filename.ToString()));
As for your image, you'll need to convert it to base64, VERY rough example
var base64 = convertTobase64(myImg);
<img src=base64 />
An example of the converting an image to base64 in Asp.NET can be found here. After converting the image to a base64 string, set the source of your image to the base64 string instead of the image URL. Then your image will appear in your PDF.
If you wanna accomplish this in javascript/html it's pretty easy.
<canvas id='imgCanvas' style='display:none;'></canvas>
var dataUrl;
var myImg = new Image();
var canvas = document.getElementById('imgCanvas');
$(myImg).on('load', function(){
canvas.height = img.height;
canvas.width = img.width;
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
dataUrl = canvas.toDataUrl('png');
});
img.src = '/CompanyLogos/12_09_YYYY_07_41_48_logo.png'
And then you just set the source of your image tag to the dataUrl variable and it will render in your PDF.
Related
I need to develop a web application which needs to scan the QR code and read the contents of it.
Is it possible with Asp.net Web application , the code behind used is C#.
Is it possible to take a picture and save it using c#.net in web application
Take a picture in the browser client. Send image data to c# pages.
<input id="upload" name="upload" type="file" accept="image/*" />
It is just a html file input,you can get the QR image data,when image received by select or camera .
$("#upload").on('change', function() {
var file = $(this)[0].files[0];
if(!file) {//undefined
return;
}
if(!startLoading()) {
return;
}
var file = $(this)[0].files[0];
var reader = new FileReader();
reader.readAsDataURL(file); // read file as Data URL
reader.onload = function() {
var base64 = this.result;
//send this base64 string to c# backend page using ajax
...
});
Then code in you c# page,get the base64 string,change to image.
byte[] arr2 = Convert.FromBase64String(base64);
using (MemoryStream ms2 = new MemoryStream(arr2))
{
System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
bmp2.Save(filePath + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
...
I found this solution for showing an image in the body of the email:
Add image to body of an email
And it works fine but it also adds the image as an attachment to the email.
Attachment inlineLogo = new Attachment(EmailLogo.ImageUrl);
mailMsg.Attachments.Add(inlineLogo);
string contentID = "Image";
inlineLogo.ContentId = contentID;
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
//To embed image in email
mailMsg.Body = "<htm><body> <img height=\"49\" width=\"169\" src=\"cid:" + contentID + "\"> </body></html>";
There is a line of code with the comment to display as inline and not as attachment but this line isn't working because the image still gets attached to the email:
//To make the image display as inline and not as attachment
inlineLogo.ContentDisposition.Inline = true;
inlineLogo.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
How can I stop the image from attaching to the email?
Use AlternateView to store your html code with image embedded as LinkedResource:
string contentID = "Image";
var inlineLogo = new LinkedResource(EmailLogo.ImageUrl, "image/png");
inlineLogo.ContentId = contentID;
var htmlView = AlternateView.CreateAlternateViewFromString(
"<html><body> <img height=\"30\" width=\"30\" src=\"cid:" + contentID + "\"> </body></html>",
Encoding.UTF8, "text/html");
htmlView.TransferEncoding = TransferEncoding.QuotedPrintable;
htmlView.LinkedResources.Add(inlineLogo);
mailMsg.AlternateViews.Add(htmlView);
P.S. Embedding image as base24 string is not very good idea, because many mail clients do not support such ability.
If you want to display an image in an email it has to exist somewhere. It is either attached as part of the message payload (regardless of whether it is "displayed inline" or as a true "attachment") - or is fetched from a remote web server when the reader reads the email (and optionally has to choose to "view images")
To not attach the image to the email payload itself:
You have to host the image on a public web server so that the reader opening the message can access it.
You have to use a fully qualified URL in your message body source, so it can find it.
Assuming you have stored the image on your web server at the following URL:
http://www.example.com/images/myimage.jpg
... then your source should simply change to reflect:
mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"http://www.example.com/images/myimage.jpg\"> </body></html>";
No need to attach it at all.
An alternative that can be used which embeds the image inline, but also isnt generally filtered by email clients is (which is generally the case today in things like junk mail) You could use a DataURL.
<img src="data:image/<type>;base64,<string>"/>
where <type> is the image type, ie jpg, gif,png, and is a base64 string. Just convert the image to a base64 string and assign it to the source using the above syntax
For example, with a jpeg...
mailMsg.Body = "<html><body> <img height=\"49\" width=\"169\" src=\"data:image/jpg;base64,<myPictureString>"\"> </body></html>";
I downloaded a image by using this method .
public Image getImageFromURL(String sURL)
{
using (WebClient wc = new WebClient())
{
var data = wc.DownloadData(sURL);
var image = Image.FromStream(new MemoryStream(data));
return image;
}
}
In my razor view and it returns me Bitmap object , but when i want to show the downloaded in image then it doesn't work
Thanks in advance
If you get Image in response from your getImageFromURL(String sURL) method.
you need to store image at any location in your application, then you can render it on view.
you can use this code.
var image = Image.FromStream(new MemoryStream(data));
string path=Path.Combine(Server.MapPath("~/images"), imagename);
image.Save(path);
this will save image at your images folder, and then you can render your image from this images folder location on view.
<img src="~/images/imagename" alt="" />
I am trying to post a picture on a page wall with this code:
string message = "Some message";
string picture = "http://somesource.com/test.jpg";
string pageToken = "page access toke";
string pageId = "page id";
string PostUri = "https://graph.facebook.com/{0}/feed?access_token={1}&";
var request = string.Format(PostUri, pageId, pageToken);
var sb = new StringBuilder();
sb.AppendFormat("picture={0}&", picture);
sb.AppendFormat("message={0}", message);
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var bytes = Encoding.UTF8.GetBytes(sb.ToString());
webClient.UploadData(request, bytes);
It is works for me. But posted image using graph api has smaller size than the image posted using regular facebook web interface. What should I change in this code to display image with larger size?
Change it to source from picture
sb.AppendFormat("source={0}&", picture);
picture is used to thumbnails.
UPDATE
You can also set height and width of the image
sb.AppendFormat("height={0}&", 450);
sb.AppendFormat("width={0}&", 450);
I have an HTML Mail template, with a place holder for the image. I am getting the image I need to send out of a database and saving it into a photo directory. I need to embed the image in the HTML Message.
I have explored using an AlternateView:
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<HTML> <img src=cid:VisitorImage> </HTML>");
LinkedResource VisitorImage = new LinkedResource(p_ImagePath);
VisitorImage.ContentId= "VisitorImage";
htmlView.LinkedResources.Add(VisitorImage);
Try this:
LinkedResource objLinkedRes = new LinkedResource(
Server.MapPath(".") + "\\fuzzydev-logo.jpg",
"image/jpeg");
objLinkedRes.ContentId = "fuzzydev-logo";
AlternateView objHTLMAltView = AlternateView.CreateAlternateViewFromString(
"<img src='cid:fuzzydev-logo' />",
new System.Net.Mime.ContentType("text/html"));
objHTLMAltView.LinkedResources.Add(objLinkedRes);
objMailMessage.AlternateViews.Add(objHTLMAltView);