byte array to image - vb.net

i am trying to convert a byte array into an image
here is my code:
Public img As BitmapImage
Public bytes As Byte()
Sub convert()
Using ms As New MemoryStream(bytes, 0, bytes.Length)
ms.Write(bytes, 0, bytes.Length)
'Dim bitmg As New BitmapImage
'bitmg.SetSource(ms)
img.SetSource(ms)
End Using
End Sub
but when i am running the application i get this error : Null reference was unhandled
on the last line
img.SetSource(ms)
Any ideas ? thank you

You haven't shown any code which actually gives img a value. Do you have
img = New BitmapImage
anywhere?

Related

Screen sharing through sockets

I'm making my first screen sharing application in VB.NET using sockets to establish the connections.
This is the client side receiving screen images from the server (they are both running in a thread):
Private Sub startscreen()
Using imgstream As NetworkStream = imgclient.GetStream()
Using ms As New MemoryStream
Dim read As Double
Do
If (imgstream.DataAvailable) Then
read = 0
ms.Seek(0, SeekOrigin.Begin)
While imgclient.Available
Dim buffer(imgclient.Available - 1) As Byte
imgstream.Read(buffer, 0, buffer.Length)
ms.Write(buffer, 0, buffer.Length)
read += buffer.Length
End While
Me.Text = "Frame bytes read: " & read
PictureBox1.Image = Image.FromStream(ms)
ms.Flush()
End If
Thread.Sleep(34) 'about 30 FPS
Loop
End Using
End Using
End Sub
And this is the server side:
Private Sub screen()
Using imgstream As NetworkStream = imgclient.GetStream()
Using ms As New MemoryStream
Do
Thread.Sleep(34) 'about 30 FPS
ms.Seek(0, SeekOrigin.Begin)
Using img = ScreenCap()
img.Save(ms, Imaging.ImageFormat.Jpeg)
End Using
ms.WriteTo(imgstream)
Loop
End Using
End Using
End Sub
Public Function ScreenCap() As Image
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height) ', Imaging.PixelFormat.Format16bppRgb555)
Dim g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
g.Dispose()
Return screenGrab
End Function
The main problem is when I call the "Image.FromStream(ms)" function, it sometimes works and others doesn't depending on how many milliseconds I set the thread to wait. Tested on 2 different computers in my LAN, on around 1 second it seems OK but always with a high CPU-Network usage. If I set, as the example says around 34 milliseconds to get all more "LIVE", that function throw an exception because of the MemoryStream. How can I speed it up? Is there any smarter way I'm missing right now? I've also tried putting a delimiter byte (like a char = "*") at the and of the MemoryStream and then send it to the client who read one byte at a time until it found a char equal to the delimiter. But it turned out to be a bad solution because a single byte of the image could represent the delimiter if converted to char. Another question I have is: How can I change the image quality and the color depth? Is it a good approach using what the comment says: "Imaging.PixelFormat.Format16bppRgb555"
Thank you!

Passing WebResponse Byte Array to function to extract image

There are many suggestions for using the Image.FromStream(stream) on the Internet and look like they should do what I want. However, there is just one piece I don't fully understand.
I am making a GET request to a URL that returns a Byte Array which is an image. I make the request and get the response like this:
Public Function getPic(ByRef stu As String) As Image
Dim url As String = "myurl" & stu 'identifying number I am adding to the url
Dim hwrequest As HttpWebRequest = Net.WebRequest.Create(url)
hwrequest.Method = "Get"
Dim hwresponse = hwrequest.GetResponse().GetResponseStream()
Dim ms As New MemoryStream()
hwresponse.CopyTo(ms)
Dim img As Image
img = Image.FromStream(ms)
PictureBox1.Image = img
End Function
I have found two different code suggestions:
Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
Using mStream As New MemoryStream(byteArrayIn)
Return Image.FromStream(mStream)
End Function
Here I understand I need to pass the Byte Array from my response to the function but that is where my understanding is lacking. When I pass in
Public Function byteArrayToImage(ByVal hwresponse As Byte()) As Image
Using mStream As New MemoryStream(hwresponse)
Return Image.FromStream(mStream)
I get "Value of Stream cannot be converted to Byte"
When I try this code:
Private Function BytesToImage(hwresponse AS Stream) As Image
Dim imgNew As Image
Dim memImage As New System.IO.MemoryStream(hwresponse)
imgNew = Image.FromStream(memImage)
Return imgNew
End Function
I get "Parameter not valid" on Image.fromStream.
I am not understanding how to take my WebResponse and pass it to the function correctly to extract the image

Display picture from access and show on form in vb.net

I want to display picture from access and show that on a form in vb.net
I have this for display information:
info.TextBox5.Text = DataGridView1.SelectedRows(0).Cells(5).Value
Now I tried something like this for pictures:
info.PictureBox1.Image = DataGridView1.SelectedRows(0).Cells(6).Value
But I got an error:
Can not associate the type of Object ' System.Byte []' to type '
System.Drawing.Image ' .
Can you help me?
dim str as string =DataGridView1.SelectedRows(0).Cells(5).Value
if str <> string.empty then
info.TextBox5.Text = str
end if
Use this:
Using ms As New MemoryStream(CType(DataGridView1.SelectedRows(0).Cells(6).Value, Byte()))
info.PictureBox1.Image = New Bitmap(ms)
End Using
Since you cannot simply assign a Byte() to an Image, you need to first create a Bitmap out of the data.
So what the above code does is:
Creates a new MemoryStream with the Byte() returned by DataGridView1.SelectedRows(0).Cells(6).Value.
Initializes a new Bitmap with the data in that stream, so that you can assign it to the Image property of your PictureBox.

Function converts image to bytes. I need to modify function to check if no image exists

I wrote the follow function to convert an image to bytes:
Public Function ConvertImage(ByVal myImage As Image) As Byte()
'store image in memory before converting and
'create memory stream, save image in proper format
Dim mStream As New MemoryStream
myImage.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg)
'convert new stream into bytes and indicate size
Dim myBytes(mStream.Length - 1) As Byte
mStream.Position = 0
mStream.Read(myBytes, 0, mStream.Length)
Return myBytes
End Function
The function works great, however, I am struggling in trying to figure out how to best modify my function to also check if an image exist. if an image exists, then convert, if no image exists, then return Nothing from a particular path and then convert. Can I do it on this function, or do I have to write another function?

Image field on ReportViewer

Anybody tried to display image field (image/byte array) data type on reportviewer.
Regards,
Peter
Yes. ReportViewer requires a Base64 Image encoding in order to display the image properly.
If your image is in a Byte array, it will need to be converted to Base64:
Public Function ConvertImageToBase64String(ByVal img As Image) As String
Dim output As String = ""
Dim outputArray() As Byte
Dim stream As New MemoryStream
img.Save(stream, Drawing.Imaging.ImageFormat.Bmp)
outputArray = stream.ToArray()
stream.Close()
output = Convert.ToBase64String(outputArray)
Return output
End Function
I have with an image data type in SQL Server. Works fine with SSRS 2005 and 2008.