Main Site Documentation

VB2012 - fez hydra/ - reading BMP from SD card


#1

Hi

I have ‘tried’ converting someones C# program for reading from an SD card to an LCD display. This is what I have come up with:

Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports System
Imports System.IO

Namespace read_image

    Public Class Program

        Public Sub ProgramStarted()
            sdCard.MountSDCard()
            Debug.Print("Program Started")
        End Sub

        Public Class flahscard
            Public picdata As Byte
        End Class

    Public Sub abc()
            Dim root = sdCard.GetStorageDevice().RootDirectory
        Dim dave = New FileStream(root & "\dave1.bmp", FileMode.Open, FileAccess.Read)
        Dim picdata(CInt(dave.Length)) As Byte
        dave.Read(picdata, 0, picdata.Length)
            dave.Close()
        End Sub

    Public Sub showpic()
            Dim flash = New flahscard()
            Dim img = New Bitmap(flash.picdata, Bitmap.BitmapImageType.Bmp)
            [b]oledDisplay.FlushRawBitmap(0, 0, 127, 127, img)[/b]
    End Sub

    End Class

This is the error I am getting:

Value of type ‘Microsoft.SPOT.Bitmap’ cannot be converted to ‘1-dimensional array of Byte’.


I have tried to interprit this original code:

class FlashCard
    {
        public byte[] picData;
.... 
In the same class to following
 public void testSD()
        {
            Display lcd = new Display();
 
            string rootDir = VolumeInfo.GetVolumes()[0].RootDirectory;
 
            FileStream fileHandle = new FileStream(rootDir + @ "\Test.bmp", FileMode.Open, FileAccess.Read);
 
            picData = new byte[fileHandle.Length];
 

            fileHandle.Read(picData, 0, picData.Length);
 
            // finished
            fileHandle.Close();
 
            lcd.ShowMessage(75, 0, "done");
        } 

 public void ShowPic()
        {
            FlashCard flash = new FlashCard();
 
            Bitmap img = new Bitmap(flash.picData , Bitmap.BitmapImageType.Bmp);
 
            LCDPanel.Clear();
            LCDPanel.DrawImage(0, 0,img, 0, 0, 272, 480);
            LCDPanel.Flush();


Any help would be appreciated. Thanks





#2

Using code tags will make your post more readable. This can be done in two ways:[ol]
Click the “101010” icon and paste your code between the

 tags or...
Select the code within your post and click the "101010" icon.[/ol]
(Generated by QuickReply)

#3

@ Linkin, for the future, a better solution to posting it all again is to edit your original post. Use the pencil icon on the top right of the post text, then highlight the code section, then hit the 101010 icon at the top right (not the coloured one bottom left) and then resubmit.


#4

i can’t test it but I think

        oledDisplay.FlushRawBitmap(0, 0, 127, 127, img.GetBitmap)

should work


#5

Hi vb-Daniel

Thanks for the reply. I spotted that mistake but when I run it now, from a button press I get the error, “cannot instantiate two Programs”. I have tried lots of subtle changes but I keep getting the same error. Also I have set dim picdata as byte at the very start, but then under the sub abc() I had to use dim picdata(cint(Dave.length)) as byte - should I somehow set that value in to the original dim picdata? If so how do I do that?

Thanks


#6

Hi Linkin,
i have no idea jet, please post:

  1. the hole error message (with the Error number)
  2. the complete programcode to retrace

for the “dim”-Problem i use:


Partial Public Class Program
    Dim picdata() As Byte

    Public Sub ProgramStarted()
    ..........
    End Sub

    Public Sub abc()
        Dim root = sdCard.GetStorageDevice().RootDirectory
        Dim dave = New FileStream(root & "\dave1.bmp", FileMode.Open, FileAccess.Read)
        Dim buffer(CInt(dave.Length)) As Byte
        dave.Read(buffer, 0, buffer.Length)
        picdata = buffer
        dave.Close()
    End Sub
   .....

   End Class
 

#7

Hi VB-Daniel

The error I get is:

#### Exception System.Exception - 0x00000000 (1) ####

#### Message: Cannot instantiate two Programs
#### Gadgeteer.Program::.ctor [IP: 002a] ####
#### read_image.read_image.Program::.ctor [IP: 0004] ####
#### read_image.read_image.Program::showpic [IP: 0003] ####
#### read_image.read_image.Program::button_ButtonPressed [IP: 0004] ####
#### Gadgeteer.Modules.GHIElectronics.Button::OnButtonEvent [IP: 0058] ####
#### Gadgeteer.Modules.GHIElectronics.Button::_input_Interrupt [IP: 0068] ####
#### Gadgeteer.Interfaces.InterruptInput::OnInterruptEvent [IP: 0055] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
A first chance exception of type ‘System.Exception’ occurred in Gadgeteer.dll
Error invoking method “Gadgeteer.Interfaces.InterruptInput” (check arguments to Program.BeginInvoke are correct)


I have changed my original program with your suggestions and I have listed it here:

Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports System
Imports System.IO

Namespace read_image

    Public Class Program
        Dim picdata() As Byte
        Public Sub ProgramStarted()
            sdCard.MountSDCard()
            Debug.Print("Program Started")

        End Sub

        Public Sub abc()
            Dim root = sdCard.GetStorageDevice().RootDirectory
            Dim dave = New FileStream(root & "\dave1.bmp", FileMode.Open, FileAccess.Read)
            Dim buffer(CInt(dave.Length)) As Byte
            dave.Read(buffer, 0, buffer.Length)
            picdata = buffer
            dave.Close()
           
        End Sub

        Public Sub showpic()
            Dim flash = New Program()
            flash.abc()
            Dim img = New Bitmap(flash.picdata, Bitmap.BitmapImageType.Bmp)
            oledDisplay.FlushRawBitmap(0, 0, 127, 127, img.GetBitmap)
        End Sub

        Private Sub button_ButtonPressed(sender As Gadgeteer.Modules.GHIElectronics.Button, state As Gadgeteer.Modules.GHIElectronics.Button.ButtonState) Handles button.ButtonPressed
            Call showpic()
        End Sub
    End Class

I’ve obviously got my order of calling the sub routines fundamentally wrong but I can’t seem to get it to work however I change them around, I have even combined the sub routines abc() and showpic() as a single subroutine.

thanks Linkin


#8

The Gadgeteer framework will instantiate the Program class at startup so there is no need for you to do this at all.

The problem is the following line


Dim flash = New Program()

Here you are creating another instance of Program in addition to the one created by the Gadgeteer framework.

Because ‘showpic’ and ‘abc’ are members of the same class ‘Program’ you do not need to create a new instance, in fact that would be a serious bug even if this was not the program class but some other class that you could create multiple instances of.

Just use the following


Public Sub showpic()
    abc()
    Dim img = New Bitmap(flash.picdata, Bitmap.BitmapImageType.Bmp)
    oledDisplay.FlushRawBitmap(0, 0, 127, 127, img.GetBitmap)
End Sub


#9

@ Linkin1977 - Can I make a general suggestion. Since your problems seem to stem mostly from not knowing VB.NET, I would suggest that you get a good resource that will teach you the VB.NET language and OO concepts.

Of course I am sure the people here would be very happy to help you, I definitely will help where I can. But my concern would be that learning bits and pieces like this does not build a solid foundation which can be used to move forward.

I cannot make a recommendation on a good VB.NET resource, for C# there are many… but I am sure that guys like @ VB-Daniel can provide better guidance here.


#10

Hi Taylorza

The problem now is that “flash” isn’t defined as anything:

Public Sub showpic()
            Dim img = New Bitmap(flash.picdata, Bitmap.BitmapImageType.Bmp)
            oledDisplay.FlushRawBitmap(0, 0, 127, 127, img.GetBitmap)
        End Sub

How do I set the variable flash and refer it to the abc subroutine?

Thanks for clearing up the program call twice, I was looking back at that and wondering if it was the problem!

Linkin


#11

@ Linkin1977 - In the same way that ‘abc’ is a member of the class so is ‘picdata’ so there is also no need for ‘flash.picdata’ just use ‘picdata’.