Main Site Documentation

Read bitmap from SD card an put on LCD problem


#1

Hello,

I have a problem to display an bitmap from de SD card to the LCD screen.

I use three Classed named: Program, Display, FlashCard.

In the class FlashCard I read the bitmap to an byte array witch is public:


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];

            lcd.ClearLCD();
            lcd.ShowMessage(0, 0, "Load picture..");

            // Copy file to byte array
            fileHandle.Read(picData, 0, picData.Length);

            // finished
            fileHandle.Close();

            lcd.ShowMessage(75, 0, "done");
        }

Then in de class Display I will show the bitmap to the screen:


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();
        }

But when the bitmap img rule is acces the software crashes, what I do wrong?


#2

Do you receive null exception?
Your flash.picData is empty = null because you never call testSD() try this:


public void ShowPic()
        {
            FlashCard flash = new FlashCard();
            flash.testSD();

            Bitmap img = new Bitmap(flash.picData , Bitmap.BitmapImageType.Bmp);
 
            LCDPanel.Clear();
            LCDPanel.DrawImage(0, 0,img, 0, 0, 272, 480);
            LCDPanel.Flush();
        }


#3

Even when I call that function I get an Null Expetion error at this line

Bitmap img = new Bitmap(flash.picData , Bitmap.BitmapImageType.Bmp);


#4

One question do you have mounted storage?
Something like this:


class FlashCard
    {
        public byte[] picData;
        public PersistentStorage sd_card;
....

In FlashCard class:


private static void MountStorage(string storage)
        {
            try
            {
                sd_card = new PersistentStorage(storage);
                sd_card.MountFileSystem();

                // Format the media if it was not.
                VolumeInfo sdvolumeinfo = new VolumeInfo(storage);
                if (!sdvolumeinfo.IsFormatted)
                {
                    sdvolumeinfo.Format("FAT", 32, true);
                }
            }
            catch (Exception)
            {
            }
        }


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


If this will work for you then you need move MountStorage() method to your main class and call it on device start only once and not in each ShowPic() request …


#5

Yes I have, the file is read in correctly, because when I acces the picData in the FlashCard class, then i can read that an .bmp file is the picData.

But when in my Display class then it looks lilke of the picData is empty and I get the null expresion.


#6

It works now, but something I find very strange.

The following crash: First code is snippet from the main, class Programm


....
            sdCard.testSD();   //read file from sd card to picdata

            Thread.Sleep(1000);

            lcd.ShowPic();    //show picdata on screen

Class Display:


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();
        }

The above code will crash ath the bitmap img line, but when I in the class Programm remove the line sdCard.testSD(); and do the following in the class Display


public void ShowPic()
        {
            FlashCard flash = new FlashCard();
            flash.testSD(); //read bitmap from sd card and put in picData

            Bitmap img = new Bitmap(flash.picData , Bitmap.BitmapImageType.Bmp);

            LCDPanel.Clear();
            LCDPanel.DrawImage(0, 0,img, 0, 0, 272, 480);
            LCDPanel.Flush();
        }

Then it works??!! How can this?