Results 1 to 14 of 14

Thread: [RESOLVED] VB.Net add PictureBox1.Image into Email Body

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    Resolved [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    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)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB.Net add PictureBox1.Image into Email Body

    Looks like the src is incorrect…

    <img src=“data:image/bmp;base64, SigBase64”>

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB.Net add PictureBox1.Image into Email Body

    You can replace those double quotes with single quotes

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    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

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    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

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB.Net add PictureBox1.Image into Email Body

    I can't see your attachments. It's a problem with the site

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    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.

    Name:  error-2.jpg
Views: 741
Size:  6.5 KB
    Attached Images Attached Images  
    Last edited by Wilder1234; Aug 10th, 2021 at 06:59 PM.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB.Net add PictureBox1.Image into Email Body

    I don’t know why that would happen. Maybe someone else can help with that…

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    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

  13. #13
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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 & "'>"

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jul 2021
    Posts
    23

    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
  •  



Click Here to Expand Forum to Full Width