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();
@ 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.
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?
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
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.
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
@ 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.