I have to embeded an image in an email
<img src="....." style="float:right;height;400px;"/>
I create and alternate view this way
try{
System.Net.Mime.ContentType mimeType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html);
AlternateView avHTMLImage = AlternateView.CreateAlternateViewFromString(msgMail.Mensaje, mimeType);
LinkedResource lrHTMLImage;
System.IO.MemoryStream img = new System.IO.MemoryStream(Imagen.Image2Bytes(ie.img));
lrHTMLImage = new LinkedResource(img, System.Net.Mime.MediaTypeNames.Image.Jpeg);
lrHTMLImage.ContentId = ie.nombreImg;
avHTMLImage.LinkedResources.Add(lrHTMLImage);
avHTMLImage.ContentId = ie.nombreImg;
this.mail.AlternateViews.Add(avHTMLImage);
}
But it is attaching and embedding the image without the settings of float neither height I'm telling it
try and avoid using css where possible in an email. you may find that it's something to do with perhaps the loading order of the css? Some basics to adhere to just in case http://kb.mailchimp.com/campaigns/ways-to-build/css-in-html-email. Put the styling in, the old way eg...
<img src="....." align="right" width="..%" />
and then set the width, making sure that the image is proportionally correct to the width. Setting heights in an email is tricky as clients like outlook and online email viewed in IE will ignore and use the default image size.
FYI also use
display:inline-block
on your images to avoide unwanted margins in gmail
Related
I am programmatically creating an image via C#, and I'm wondering how can I use this created image as a background of a div.
To explain it better, here's an example:
public string BackImage()
{
System.Drawing.Image img = //generated image goes here
var ms = new MemoryStream();
img.Save(ms, ImageFormat.Jpeg);
return Convert.ToBase64String(ms.ToArray());
//should I return it in a different way?
}
This gives me the image that I can use, now how would I set this image to be a background of a div?
<div id="main">
</div>
I have tried using background-image:url(data:image/jpeg;base64,#Html.Action("BackImage"))" but the browser starts freezing because of the image.
Is there some simpler way of doing it?
Thanks.
Two ways:
Simple:
Save your image in a temp folder and in your css class or style definition use
background-image: url('whatevertheurlis')
A bit more complex:
Create an IHttpHandler implementation to deliver the generated image. The css/style background-image setting is different only in the Url part.
I've done both before and the choice was based on the requirements of the number of different images to create and lifetime.
I have a pdf file created with itextsharp with images in the file. I would like to put a hyperlink in the file that if you pick the picture it will open that picture in a picture viewer. I can set a hyperlink to a web address but have no idea how to get it to open a file. Below is the code, yes I know that c:\test.jpg is a bad hardcoded file name but it is just a test. When you click the picture it does nothing but I have no idea how to tell it what to do.
iTextSharp.text.Image pic =TextSharp.text.Image.GetInstance(comment.examplePic);
pic.ScaleToFit(200f, 200f);
Chunk cImage = new Chunk(pic, 0, 0, false);
Anchor anchor = new Anchor(cImage);
anchor.Reference = "c:\\test.jpg";
doc.Add(pic);
doc.Add(anchor);
A PDF is self-contained. This means that all the resources needed to show the PDF are (usually) stored inside the PDF (exceptions are for instance fonts that can be retrieved from the operating system).
When you have an image that is shown on a PDF page, the bytes of that image are stored in what we call an Image XObject. An XObject is an object that is external to the page, but that is stored as a separate object inside the PDF file.
You are asking to serve the image bytes stored inside this separate object to a viewer on the operating system. This is impossible. I don't know of any viewer that can take those bytes and somehow forward them to an image viewer.
I can think of three possible workarounds. I don't know if any of these workarounds is acceptable to you.
1. Serve the image online
You could put the image on a server and use the code you have in your snippet to link to that online image. Of course: this will only work if the person viewing the document is online and clicks OK when his viewer asks him if it's OK to link to a resources on the internet.
2. Serve the image as an annotation
In this case, you create an annotation for which you create an appearance that renders that same image XObject in the annotation layer (all annotations are shown on top of the page content). You can easily change the visibility status of an annotation to make it invisible (in your case, this would be the default status) or visible (in your case, this would be triggered by a JavaScript action when clicking the link).
There's an example of such an annotation here: Advertisement. If you open advertisement.pdf, you see an image with a button that says "Close this advertisement". Once you click that, the status of the annotation will be changed to invisible. You could do something similar, but the other way round: click a link to make it visible instead of invisible.
This solution doesn't depend on an external viewer, the image is shown in the PDF viewer.
3. Add the image as optional content
Starting with PDF 1.5, PDF supports optional content. See for instance the OptionalContentExample. In this example, we have some questions and answers, but the answers are not visible by default. See layer_actions.pdf. There are links "on / off / toggle" to make the answers visible or invisible.
You could do the same with images: you could add them to a layer that is invisible by default, but that can be made visible if somebody clicks a link. However: this requires a viewer that supports OCG (optional content groups) and the actions to change the status of these OCGs. For instance: if you would try the layer_actions.pdf example in the PDF viewer in Chrome, it won't work, but if you download the PDF and open it in Adobe Reader, you'll see the behavior I described.
Summarized:
You are asking something that is impossible, but there are workarounds. Please post another question if you have chosen a workaround and you don't succeed in making that workaround word (but please take into account that not all viewers support every workaround).
no offence but too much knowledge sometimes makes you ignorant of small things.
simple solution to this problem is here
http://kuujinbo.info/iTextSharp/imageAnchor.aspx
sample code that i implemented works like charm
PdfPCell p1 = new PdfPCell();
p1 = new PdfPCell();
p1.Padding = 0;
p1.Border = 0;
PdfPTable nav = new PdfPTable(1);
nav.WidthPercentage = 100;
nav.SpacingAfter = 12;
navbarImg.Annotation= new Annotation(0, 0, 0, 0, ur);
p1.Image = navbarImg;
nav.AddCell(p1);
_doc.Add(nav);
Could you help me - I want each day to display same text with image from Access database where I stored text and image path. Text is going on the page, but for image I got only placeholder. I tried also with Handlers. I search through theses questions and nothing is exact what I want.
Here is code in C# I tried:
//image
int IDImage = 0;
upit = new baza.Upit("SELECT * FROM (Images INNER JOIN JoinImageText ON Images.IDImage = JoinImageText.IDImage) WHERE JoinImageText.IDText = " + IDText.ToString() + " ORDER BY LastTimeDisplayed, RND()");
upit = new baza.Upit("SELECT * FROM (Images INNER JOIN JoinImageText ON Images.IDImage = JoinImageText.IDImage;
if (upit.Reader.Read())
{
TextImage.Src = upit.Reader["image"].ToString();
IDImage = Convert.ToInt32(upit.Reader["IDImage"]);
}
upit.Zatvori();
Thanks in advance.
You have to use Handlers in ASP.Net.
This post from CodeProject can help you in achieving your goal.
When you say you're getting a placeholder you mean when the page renders in the browser? If so, it means the path to the image is incorrect. What image information are storing in the Access DB? Image name only or image with path? Make sure the path to the image is correct.
Make sure your tag has the src attribute:
<img src="/images/someimage.jpg" alt="Image of something" height="64" width="64">
If you include the runat=server attribute, in code behind you could so something like:
<img src="<%= Url.Content("~/database_value_for_image_path") %>" runat=server/>.
The UrlHelper would resolve the relative path for you.
Or since you're using asp.net, consider the asp:Image tag as well - Image Class on MSDN
I'm trying to display my picture out on the image tag at my webform in a web application which i used VS2012 to develop. I have already stored my image in a binary form in the sql server 2008. I attempted to display out the image through the c# codes. However, the image displayed out is the exact dimension size. I'm trying to store this particular selected into the image tag with a specific dimension. Here are my codes which i used to display out my image.
This code here is for me to select out the picture from the database. I managed to display the picture out by using this.
string strQuery = "select profilepic from LoginRegisterOthers where username=#username";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("#username", SqlDbType.VarChar).Value = username;
DataTable dt = GetData(cmd);
if (dt != null)
{
download(dt);
}
This code then used to convert the image binary data into an image.
private void download(DataTable dt)
{
Byte[] bytes = (Byte[])dt.Rows[0]["profilepic"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpg";
//Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
What you do with the binary array needs to change (if I guessed correctly what you're trying to do)
This is an alternative technique of inserting binary contents into image tag.
Instead of just streaming the binary contents down to the browser, you could write it in the src attribute of the <img> tag. The HTML you are aiming to have would look like this example (note that the image contents is Base64-encoded):
<img src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAAAAABQAA8AAAPb
WLrc/jDKSVe4OOvNu/9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVr
ApTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KT
kpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxM
AF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=
" alt="British Blog Directory" width="80" height="15" />
Now, to accomplish this, you have probably several options. It all depends how you're rendering the <img> tag. If you're using a server-side control, then code like this may do (this sample snippet comes from this SO answer):
byte[] picByteArray = user.Picture.ToArray();
string myPicString = Convert.ToBase64String(picByteArray);
myPicture.Attributes["src"] = "data:image/gif;base64," + myPicString;
ADDITION:
One possible benefit of going that way is that, if design allows it or requires it, you could retrieve more than one image in a single database trip.
But as I also say in the comments, this method comes with the downside that images are processed synchronously, as your page is processed.
Have a look at this url. One of the users has created a function that takes a System.Drawing.Image as input. You will probably have to write the binary data you download from your db into an object of type System.Drawing.Image before you call the function that is written in the page below
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a43fe625-a5cd-4438-895f-3ddeec6eb866/
You have two choices:
in tag specify width and height of the image using CSS or html attributes. This causes that users will always download full size image but it will be displayed cropped
in src of img tag put width and height like: image.aspx?width=200&height=100. Parse query string on server side and resize image on server.
Of course you can use mix of both.
How to use custom font asp.net application.
I have a font file with me. I want to add that file as a resource and use it inside the project.
how to do it?
PrivateFontCollection pfc = new PrivateFontCollection();
Stream fontStream = this.GetType().Assembly.GetManifestResourceStream(Server.MapPath("~/Resources/EAN-13.ttf"));
byte[] fontdata = new byte[fontStream.Length];
fontStream.Read(fontdata, 0, (int)fontStream.Length);
fontStream.Close();
unsafe
{
fixed (byte* pFontData = fontdata)
{
pfc.AddMemoryFont((System.IntPtr)pFontData, fontdata.Length);
}
}
I tried this. But its not working.
You must distinguish between the code that run on server and the code that run on client browser.
The fonts that you have on your computer, on your server, can not be visible on user computer using the code behind just because you loading the fonts.
So you have two options here.
Load the fonts, and render a text in an image using this fonts, then show this image to the user.
Use client side techniques to show this fonts. Some of them are cross browser font embedding, and other can be javascript that can rendered them on client side.
You can google it with "embed custom fonts website".
Also you can check : http://typeface.neocracy.org/
and Is it possible to use custom fonts - using font-face?