Solved

VB.Net Convert Picturebox BMP to String

Posted on 2014-12-07
10
1,088 Views
Last Modified: 2014-12-09
Hi Experts.
In VB.Net, I save a picturebox BMP to a BMP file on disk and then convert this file to a string with this function
Using ImagePath

            Dim data As String
            Dim ms As MemoryStream = New MemoryStream
            Dim bmp As Bitmap = New Bitmap(ImagePath)
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.BMP)
            data = Convert.ToBase64String(ms.ToArray())
            Return data

Please advise how I would get the string  of a picturebox image directly without having to save to disk first and then convert it

Many Thanks
0
Comment
Question by:PNRT
  • 5
  • 4
10 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40485469
you should be able to use the image object from the picturebox directly...

Dim data As String
MemoryStream ms = new MemoryStream()
 PictureBox.image.Save(ms, System.Drawing.Imaging.ImageFormat.BMP)
data = Convert.ToBase64String(ms.ToArray())

Open in new window

0
 
LVL 2

Author Comment

by:PNRT
ID: 40485480
Hi, thank you for you reply.
I get a  'System.NullReferenceException'   with this code

       
 Dim data As String
        Dim ms As MemoryStream = New MemoryStream
        PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
        data = Convert.ToBase64String(ms.ToArray())
        MsgBox(data)

Open in new window


Many Thanks
0
 
LVL 2

Author Comment

by:PNRT
ID: 40485482
Sorry, I should have mentioned the error is on line 3

Thanks
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40485496
this would mean that the image object of the picturebox is not set?
can you check/test/validate that (debugging)
0
 
LVL 2

Author Comment

by:PNRT
ID: 40485521
I am using code to draw in the picture box and that is working correctly
Also, if I use the following code, it saves the picturebox contents correctly to a file

         
  Dim bmp As New Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height)
            PictureBox1.DrawToBitmap(bmp, PictureBox1.ClientRectangle)
            bmp.Save(System.IO.Path.Combine("C:\Test", "test.bmp"), System.Drawing.Imaging.ImageFormat.Bmp)

Open in new window


AM I missing something different?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 40485575
Try this one out:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim data As String = PictureBoxToString(PictureBox1)
    End Sub

    Private Function PictureBoxToString(ByVal PB As PictureBox) As String
        Dim bmp As New Drawing.Bitmap(PB.Width, PB.Height)
        PB.DrawToBitmap(bmp, PB.ClientRectangle)
        Dim ms As New IO.MemoryStream
        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
        Return Convert.ToBase64String(ms.ToArray())
    End Function

Open in new window

0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40485596
Indeed, that is what I wanted to post also...
in short:
bmp.Save(System.IO.Path.Combine("C:\Test", "test.bmp"), System.Drawing.Imaging.ImageFormat.Bmp)
should be:
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
0
 
LVL 2

Author Comment

by:PNRT
ID: 40485598
Hi, thanks for the reply

I added a msgbox after        Dim data As String = PictureBoxToString(PictureBox1)
But the msgbox returned blank.

So I added a bmp graphic to the picturebox - still blank
So I added a bmp graphic as a background image to the picture box, msgbox still blank.

The function seems to make perfect sense but isn't doing anything

I think I have a defective picturebox!!!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40485610
I rather guess that the messagebox is unable to display the "text" generated by the bitmap string...
what is your goal, actually, to see a "string" from a bitmap?
0
 
LVL 2

Author Comment

by:PNRT
ID: 40485620
Hi, yes I wanted to save the bitmap to a file as a string
If I save the bitmap to a file on the disk, I can then convert the file OK and that string then shows in a msgbox
I was trying to cut out the step of saving the bitmap to disk and then converting it to a string

I used this code to convert the file and it works fine

           Dim data As String
            Dim ms As MemoryStream = New MemoryStream
            Dim bmp As Bitmap = New Bitmap(sImagePath)
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
            data = Convert.ToBase64String(ms.ToArray())
            Return data

Open in new window


Many thanks for the help!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question