?
Solved

VB.Net Convert Picturebox BMP to String

Posted on 2014-12-07
10
Medium Priority
?
1,311 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 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