Solved

VB.Net Convert Picturebox BMP to String

Posted on 2014-12-07
10
1,037 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - For Loop Error 5 37
Setting runtime form location 4 31
Closing all open child forms. 4 38
POWERSHELL: do / until, should continue even when there is an error 4 55
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now