[RESOLVED] VB.Net add PictureBox1.Image into Email Body
He everyone.
I'm trying to add the PictureBox1 Image into my email body when i click on a Button, but without any results.
The picture is not something that was saved on my hard drive, so no real file attachment.
It does not fail with the code i have, but it does not add the picture either. So this is my problem.
How can i make it work?
Thanks again for your help
Here is my code:
Code:
Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
Dim tmpImg As New Bitmap(Control.Width, Control.Height)
Using g As Graphics = Graphics.FromImage(tmpImg)
g.CopyFromScreen(PictureBox1.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(PictureBox1.Width, PictureBox1.Height))
End Using
Return tmpImg
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Outlook As Outlook.Application
Dim Mail As Outlook.MailItem
Dim Acc As Outlook.Account
Outlook = New Outlook.Application()
Mail = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
Mail.To = "[email protected]"
Mail.Subject = "Please review"
'If you have multiple accounts you could change it in sender:
For Each Acc In Outlook.Session.Accounts
'Select first pop3 for instance.
If Acc.AccountType = Microsoft.Office.Interop.Outlook.OlAccountType.olPop3 Then
Mail.Sender = Acc
End If
Next
'Attach files
' Mail.Attachments.Add("C:\Path\To\File1.pdf")
'Append some text:
Mail.HTMLBody &= "<span style='font-family:calibri,serif;'>
<span style='font-size: 22px;'><strong>
<span style='color: #1e398a'>PICTURE</span> -> <span style='font-family:calibri,serif;'>
<span style='font-size: 22px;'>below</span>
</strong>" & "<br><br>
<img src=TakeScreenShot(PictureBox1)>"
Mail.Display()
End Sub
Re: VB.Net add PictureBox1.Image into Email Body
Here’s how to get a base64 string in C# (conversions are simple)…
Code:
Bitmap bImage = newImage; // Your Bitmap Image
System.IO.MemoryStream ms = new MemoryStream();
bImage.Save(ms, ImageFormat.Jpeg);
byte[] byteImage = ms.ToArray();
var SigBase64= Convert.ToBase64String(byteImage); // Get Base64
Then you can set your img src…
img src="data:image/bmp;base64, SigBase64"
Re: VB.Net add PictureBox1.Image into Email Body
Let me try that:
Code:
Dim bImage As Bitmap = TakeScreenShot(PictureBox1)
Dim ms As System.IO.MemoryStream = New MemoryStream()
bImage.Save(ms, ImageFormat.Jpeg)
Dim byteImage As Byte() = ms.ToArray()
Dim SigBase64 = Convert.ToBase64String(byteImage)
Re: VB.Net add PictureBox1.Image into Email Body
I've tried like below:
Code:
Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
Dim tmpImg As New Bitmap(Control.Width, Control.Height)
Using g As Graphics = Graphics.FromImage(tmpImg)
g.CopyFromScreen(PictureBox1.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(PictureBox1.Width, PictureBox1.Height))
End Using
Return tmpImg
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Outlook As Outlook.Application
Dim Mail As Outlook.MailItem
Dim Acc As Outlook.Account
Dim bImage As Bitmap = TakeScreenShot(PictureBox1)
Dim ms As System.IO.MemoryStream = New MemoryStream()
bImage.Save(ms, ImageFormat.Jpeg)
Dim byteImage As Byte() = ms.ToArray()
Dim SigBase64 = Convert.ToBase64String(byteImage)
Outlook = New Outlook.Application()
Mail = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
Mail.To = "[email protected]"
Mail.Subject = "Please review"
'If you have multiple accounts you could change it in sender:
For Each Acc In Outlook.Session.Accounts
'Select first pop3 for instance.
If Acc.AccountType = Microsoft.Office.Interop.Outlook.OlAccountType.olPop3 Then
Mail.Sender = Acc
End If
Next
'Attach files
' Mail.Attachments.Add("C:\Path\To\File1.pdf")
'Append some text:
Mail.HTMLBody &= "<span style='font-family:calibri,serif;'>
<span style='font-size: 22px;'><strong>
<span style='color: #1e398a'>PICTURE</span> -> <span style='font-family:calibri,serif;'>
<span style='font-size: 22px;'>below</span>
</strong>" & "<br><br>
<img src=data:image/bmp;base64, SigBase64>"
Mail.Display()
End Sub
But the result i have in my email is:
Attachment 182057
Re: VB.Net add PictureBox1.Image into Email Body
Looks like the src is incorrect…
<img src=“data:image/bmp;base64, SigBase64”>
Re: VB.Net add PictureBox1.Image into Email Body
You can replace those double quotes with single quotes
Re: VB.Net add PictureBox1.Image into Email Body
This code was not working:
Code:
<img src="data:image/bmp;base64, SigBase64">
But i think we are almost there cause when i did:
Code:
<img src=""data:image/bmp;base64, SigBase64"">
The result was:
Attachment 182058
When it should be:
Attachment 182059
Re: VB.Net add PictureBox1.Image into Email Body
Even with single quote like:
Code:
<img src='data:image/bmp;base64, SigBase64'>
I have the result
Attachment 182060
Very strange
Re: VB.Net add PictureBox1.Image into Email Body
I can't see your attachments. It's a problem with the site
2 Attachment(s)
Re: VB.Net add PictureBox1.Image into Email Body
What it does is that it adds the empty box without the image and it says:
The linked image cannot be displayed. The file may have been moved, renamed, or deleted. Verify that the link points to the correct file and location.
Attachment 182062
Re: VB.Net add PictureBox1.Image into Email Body
I don’t know why that would happen. Maybe someone else can help with that…
Re: VB.Net add PictureBox1.Image into Email Body
I agree with you. It kind of weird that it does not work.
I've clean the code a little more in case this could help troubleshooting:
Code:
Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
Dim tmpImg As New Bitmap(Control.Width, Control.Height)
Using g As Graphics = Graphics.FromImage(tmpImg)
g.CopyFromScreen(PictureBox1.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(PictureBox1.Width, PictureBox1.Height))
End Using
Return tmpImg
End Function
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim Outlook As Outlook.Application
Dim Mail As Outlook.MailItem
Outlook = New Outlook.Application()
Mail = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
Mail.To = "[email protected]"
Mail.Subject = "Please review"
Dim bImage As Bitmap = TakeScreenShot(PictureBox1)
Dim ms As System.IO.MemoryStream = New MemoryStream()
bImage.Save(ms, ImageFormat.Jpeg)
Dim byteImage As Byte() = ms.ToArray()
Dim SigBase64 = Convert.ToBase64String(byteImage)
Mail.HTMLBody &= "<img src='data:image/bmp;base64, SigBase64'>"
Mail.Display()
End Sub
Re: VB.Net add PictureBox1.Image into Email Body
I read your current code as adding the literal string "SigBase64" as the image source, rather than the contents of the variable SigBase64.
I would imagine it would need to look something more like this:
Code:
Mail.HTMLBody &= "<img src='data:image/bmp;base64, " & SigBase64 & "'>"
Re: VB.Net add PictureBox1.Image into Email Body
Yes, this is working great.
Thanks .Paul. and OptionBase1 for your help.