Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.Net Convert Picturebox BMP to String

Posted on 2014-12-07
10
Medium Priority
?
1,416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 143

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

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 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 1000 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 143

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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