4

I'm trying to render a bitmap in Memory using mono. This image should be displayed on Adafruits 2.8" touch TFT (320*240). The Programm is developed with Visual Studio 2013 Community Edition. I want to host a ASP.NET Web Api and Show some data on the Display. The ASP.NET part is working fine and the image is rendered. My idea was to write the Image to the framebuffer Input, but doing this I get an Exception saying that file is to large. I'm just writing raw data without BMP Header. Has someone managed doing this? Maybe creation of image is wrong. It seems as something is happening because the display changes and I can see white areas which might be from my image. I don't want to use any extra libraries to keep it simple. So my idea is to use FBI directly. Does anyone know this problem and the solution?

Here is some of my code:

using (Bitmap bmp = new Bitmap(240, 320, PixelFormat.Format16bppRgb555))
{
    [...]
    Byte[] image = null;

    using(MemoryStream memoryStream = new MemoryStream())
    {
        bitmap.Save(memoryStream, ImageFormat.Bmp);

        Byte[] imageTemp = memoryStream.GetBuffer();
        //Remove BMP header
        image = new Byte[imageTemp.Length - 54];
        Buffer.BlockCopy(imageTemp, 54, image, 0, image.Length);
        //153600 byte
        using (FileStream fb1 = new FileStream("/dev/fb1", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            fb1.Write(image, 0, image.Length);
            fb1.Close();
        }
    }
}
  • I'm not an expert here (except owning a Raspi:-), but anyway: Have you tried writing the data in smaller chunks? And are you sure that the binary data of the bitmap is what he frame buffer expects? E.g. maybe the BMP data is run-length-encoded and the FB does not like it or the other way round... – hvb Feb 25 '15 at 07:21

2 Answers2

0

Take a look at http://computerstruggles.blogspot.de/2013/02/how-to-program-directfb-in-c-on.html - the idea is to install the directfb library and use it from C# with PInvoke. The blog's author uses a mini wrapper in C to make using it even easier. BTW why don't you like to install additional libraries and to profit from the work others have done for you?

hvb
  • 2,484
  • 1
  • 10
  • 13
  • Okay, I could install DirectFB and use PInvoke. I'm developing under Windows and don't know to much about linux. So I just know PInvoke in Windows using a Dll. I think it is too much to write a wrapper in C and implement all structures there. How can I use directfb with just C# code and PInvoke? – Heiko Eichhorn Feb 25 '15 at 16:31
  • Sorry, I don't know more than I already told you. Maybe the Pi is not the best device to code for when C# is the only programming language to choose from. – hvb Feb 26 '15 at 17:19
0

You may be running out of memory when the MemoryStream reallocates memory. When it needs to grow, it doubles in size. With this large of a write, the internal buffer is probably exceeding available memory. See Why does C# memory stream reserve so much memory? for more information.

Community
  • 1
  • 1
Jim Rush
  • 4,143
  • 3
  • 25
  • 27