Bitmap from Webservice

robh

Member
Licensed User
Longtime User
I have a .Net webservice with a method that is successfully returning an image from a DB BLOB field. I succesfully tested reconstruction of the image in a .Net client.

Now using Basic4Android, I'm really struggling to successfully get anything into a Bitmap type. The webservice itself is being called and the expected result is being returned in the webrepsonse, however after trying all manner of conversions I keep receiving "Error loading bitmap" errors.
Any help greatly appreciated.

For information, my data stream is being returned as <base64Binary> from the webservice, this is my latest attempt to get the image:

Sub GetProductImageFullSize_EndElement(Uri As String, Name As String, Text As StringBuilder)





If Name = "GetProductImageFullSizeResult" Then

Dim b() As Byte
Dim bmp As Bitmap
Dim s As StringUtils
Dim i As InputStream


b =s.DecodeBase64(Text)

i.InitializeFromBytesArray(b,0,b.Length)
bmp.Initialize2(i)
i.Close

Activity.SetBackgroundImage(bmp)
End If
End Sub
 

robh

Member
Licensed User
Longtime User
Hi Erel,

Thank you for the swift response.

I have attempted to write the initial text out to a jpg file(see amended code), when loading the file there no image displayed? When stepping through the code I can see the expected number of bytes in the stream, I think this is a conversion problem more than the general flow of code/objects. Can you suggest anything else?



I have amended the code as follows:
Sub GetProductImageFullSize_EndElement(Uri As String, Name As String, Text As StringBuilder)





If Name = "GetProductImageFullSizeResult" Then





' test save to sd card

Dim out As TextWriter



Dim b() As Byte
Dim bmp As Bitmap
Dim s As StringUtils
Dim i As InputStream




File.WriteString(File.DirRootExternal,"TestImage.jpg",Text)
b =s.DecodeBase64(Text)
Dim o As OutputStream
o.WriteBytes(b,0,b.Length)




i.InitializeFromBytesArray(b,0,b.Length)

bmp.Initialize2(i)
i.Close

Activity.SetBackgroundImage(bmp)
End If
End Sub
 
Upvote 0

robh

Member
Licensed User
Longtime User
Thanks again Erel, yes I have written out the text contents to text file and the content matches what is being returned from the webservice. Not too sure where to go from here??
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The base 64 text is invalid. I tried it with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim su As StringUtils
   Dim b() As Byte
   b = su.DecodeBase64(File.ReadString(File.DirAssets, "1.txt"))
   Dim in As InputStream
   in.InitializeFromBytesArray(b, 0, b.Length)
   Dim out As OutputStream
   out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
   File.Copy2(in, out)
   out.Close
   Log(b.Length)
   Dim bmp As Bitmap
   bmp.Initialize2(in)
   Activity.SetBackgroundImage(bmp)
End Sub
And with a base 64 decoder tool. The result is an invalid bitmap. The size is 1396483.
 
Upvote 0

robh

Member
Licensed User
Longtime User
Thanks for the time you are spending investigating this, your support is fantastic! :)


Hmmm, I'm a little confused as to where to go with this now? I have written some simple VB.Net code against the same service and webmethod and everything works as expected where 'result' is the byte array being returned from the webservice and size = 1396483

Dim ms As New System.IO.MemoryStream
Dim bmp As Bitmap
Dim arData() As Byte = result

ms.Write(arData, 0, arData.Length)

bmp = New Bitmap(ms)

PictureBox1.Image = bmp


I simply want to replicate the above in Basic4Android to establish a bitmap image.
 
Upvote 0

robh

Member
Licensed User
Longtime User
Hi Erel,

I've now managed to get this working! :) I took a couple of steps back and followed your code and returned to basics, seems I had a problem with one of the streams in the webservice.

Thanks a million!
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
reconstructing image

Hi guys, i'm having the same issue, i was wondering if robh could share the code you used to get this working, i also have the bytearray encoded with base64
i'm really confused, when i compare the data being sent with the data received they seem to match, just like robh i don't know where to go from here, if you guys could please help me out i will really appreciate it very much.

I hope to hear from you guys soon

:)
walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Help please

Hi Erel, i need your help with this, i encoded an image using encodebase64 function in visual basic, i can see that it works because i save the data to a text file and i used an online service that decodes the data and it reconstructs the image, however when i use the code you provided, i get an error that says
"Bad base64 input character decimal 63 in array position 0", i'm not sure why it gives me this error, but like i said if i use the service found here Free Usenet Tools - Online Base64 Encoder and Decoder. Usenet newsgroups binaries.
the file actually gets decoded and i can see the image.

here's the text file generated by my code in visual basic

View attachment testfile.zip


can you please help me figure this out Erel, i've been searching all over to figure this out but i haven't found any help so far, i'm very close to finishing this app and this is the part i'm stuck on, please help!!!!!

thanks,
:BangHead:
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Utf8 Bom

Hi Erel here's the code that encodes the file in visual studio 2008

B4X:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        trasmiUdpHost.Connect(GLOIP, GLOINTPORT)

        'Dim strReturnData As String
        Dim path As String = "C:\Users\florew1\Pictures\purple5.jpg"
        Dim sr As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read)
        Dim bytes As [Byte]() = New Byte((sr.Length) - 1) {}
        Dim sw As IO.StreamWriter
        sw = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\florew1\Desktop\testfile.txt", True)
        Dim numBytesToRead As Integer = CType(sr.Length, Integer)
        Dim numBytesRead As Integer = 0
        'Dim sendata As String
        'Dim sendata1 As String
        Dim sendata2() As Byte
        While (numBytesToRead > 0)

            ' Read may return anything from 0 to numBytesToRead.
            Dim n As Integer = sr.Read(bytes, numBytesRead, numBytesToRead)
            ' Break when the end of the file is reached.
            'strReturnData = System.Text.Encoding.Unicode.GetString(bytes)
            sr.Close()
            Dim converted As String
            ' sendata = System.Text.Encoding.Unicode.GetString(bytes)
            'sendata1 = EncodeBase64(sendata)
            converted = ConvertFileToBase64(path)
            sendata2 = Encoding.ASCII.GetBytes(converted)
            'ExtractBase64ToFile(converted, "image.gif")    'if i uncomment this line the the file gets reconstructed and i can see the image
            pRet = trasmiUdpHost.Send(sendata2, sendata2.Length)
            sw.WriteLine(converted)
            sw.Close()



            If (n = 0) Then
                Exit While
            End If
            numBytesRead = (numBytesRead + n)
            numBytesToRead = (numBytesToRead - n)

        End While
        numBytesToRead = bytes.Length
        ' For i = 0 To bytes.Length - 1

        ' pRet = trasmiUdpHost.Send(bytes, bytes.Length)



    End Sub

    Public Function ConvertFileToBase64(ByVal fileName As String) As String
        Dim ReturnValue As String = ""
        If My.Computer.FileSystem.FileExists(fileName) Then
            Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
                Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
                Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
                ReturnValue = Convert.ToBase64String(BinBytes)
                BinaryFile.Close()
            End Using
        End If
        Return ReturnValue
    End Function

One thing i noticed right now is that if i take the file created by this code and decode it with the online service i can see the image, if i take that image and re-encode it using the same online service it gives me a text file and when i use that and use it in b4a it actually works, i'm using a software called winmerge to compare the two text files but it doesn't detect any difference between the two files, i'm going crazy trying to figure this out, hopefully you can help me wort this out Erel, thank you very much for your time.

cheers,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Utf8 Bom

Use a StreamWriter instead and explicitly set the encoder (c# code):
B4X:
StreamWriter sw = new StreamWriter(path, false, new UTF8Encoding(false))

Wow, once again Erel, you are the man, thank you so much for your help, such a simple solution and yet i was going crazy and had not thought about that, that's why you are the king, thank you very much Erel, that works perfect.

:icon_clap::icon_clap::icon_clap::icon_clap:
 
Upvote 0
Top