Solved

VB.Net Convert Picturebox BMP to String

Posted on 2014-12-07
10
992 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]
Comment Utility
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 1

Author Comment

by:PNRT
Comment Utility
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 1

Author Comment

by:PNRT
Comment Utility
Sorry, I should have mentioned the error is on line 3

Thanks
0
 
LVL 142

Expert Comment

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

Author Comment

by:PNRT
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
Comment Utility
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
Comment Utility
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 1

Author Comment

by:PNRT
Comment Utility
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]
Comment Utility
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 1

Author Comment

by:PNRT
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

8 Experts available now in Live!

Get 1:1 Help Now