-
Aug 10th, 2021, 05:50 PM
#1
Thread Starter
Junior Member
[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 = "test@test.ca"
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
-
Aug 10th, 2021, 06:04 PM
#2
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"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2021, 06:11 PM
#3
Thread Starter
Junior Member
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)
-
Aug 10th, 2021, 06:19 PM
#4
Thread Starter
Junior Member
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 = "test@test.ca"
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
Last edited by Wilder1234; Aug 10th, 2021 at 07:11 PM.
-
Aug 10th, 2021, 06:23 PM
#5
Re: VB.Net add PictureBox1.Image into Email Body
Looks like the src is incorrect…
<img src=“data:image/bmp;base64, SigBase64”>
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2021, 06:25 PM
#6
Re: VB.Net add PictureBox1.Image into Email Body
You can replace those double quotes with single quotes
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2021, 06:30 PM
#7
Thread Starter
Junior Member
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
-
Aug 10th, 2021, 06:35 PM
#8
Thread Starter
Junior Member
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
-
Aug 10th, 2021, 06:39 PM
#9
Re: VB.Net add PictureBox1.Image into Email Body
I can't see your attachments. It's a problem with the site
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2021, 06:43 PM
#10
Thread Starter
Junior Member
-
Aug 10th, 2021, 07:08 PM
#11
Re: VB.Net add PictureBox1.Image into Email Body
I don’t know why that would happen. Maybe someone else can help with that…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 10th, 2021, 07:31 PM
#12
Thread Starter
Junior Member
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 = "test@test.ca"
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
-
Aug 10th, 2021, 07:37 PM
#13
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 & "'>"
-
Aug 10th, 2021, 07:41 PM
#14
Thread Starter
Junior Member
Re: VB.Net add PictureBox1.Image into Email Body
Yes, this is working great.
Thanks .Paul. and OptionBase1 for your help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|