How to embed an Image Stream to MailMessage
You can embed the image and skip working with resources by converting it to base64 instead:
public static string BitmapToBase64(Bitmap b)
{
ImageConverter ic = new ImageConverter();
byte[] ba = (byte[])ic.ConvertTo(b, typeof(byte[]));
return Convert.ToBase64String(ba, 0, ba.Length);
}
and use it as html image src :
string logoimage="<img src='data:image/png;base64," + BitmapToBase64(logo) + "'>";
Note that converting to Base64 slighly increases the size of the image.
Ok i have solved the problem.
Instead of using the BitMap save method I converted the BitMap to Byte[] and gave the memory stream the Byte[]
Did not work :
b.Save(logo, ImageFormat.Jpeg);
Did Work:
Bitmap b = new Bitmap(Properties.Resources.companyLogo);
ImageConverter ic = new ImageConverter();
Byte [] ba = (Byte[]) ic.ConvertTo(b,typeof(Byte[]));
MemoryStream logo = new MemoryStream(ba);
I think it has something to do with the Bitmap.Save method, in the MSDN lib it mentioned that the stream has to have an offset of 0.
Bitmap b = new Bitmap(Properties.Resources.companyLogo);
MemoryStream logo = new MemoryStream();
b.Save(logo, ImageFormat.Jpeg);
After you do the save, you have to "seek" the MemoryStream back to the start.
logo.Position = 0;