In addition to creating graphical interfaces via web servers such as Node.JS or Python and TKinter, we also have the option to run GUIs that were created by Visul Studio on the Raspberry Pi. These are created via Windows and then made executable on the Raspberry Pi. Not much is needed!
In this tutorial we created a small test GUI and prepare everything so that we can open user interface that is written in C# on the Raspberry Pi as well.
Accessory parts
In addition to a Windows PC (Win7, Win 8, Windows 10) you need a Raspberry Pi with user interface. Raspbian (no Lite version) with installed desktop is recommended.
If you are new to Visual Studio, C #, .NET, and GUI building, we recommend a small but helpful guide. In addition, Visual Studio must be on our computer.
Install Visual Studio
First of all, we have to install Visual Studio. This is available for free in the Community Edition. Download it on your Windows PC and install at least the .NET desktop development environment, which is about 5GB in size. It takes a while and you have to restart the computer.
After the installation, open Visual Studio and choose File -> New -> Project -> Visual C# -> Windows Forms App (.NET Framework). With it we can start a new project. Enter the name and location below.
Create your own Raspberry Pi GUI
After the project has been created, the Form Designer opens. Here you can design the GUI. On the left side there is a toolbox, in which all elements such as buttons, etc. are present. Here, you can simply pull out and place elements. In my case, I choose a text box, a button and a label:
You can set further details and create a nice GUI very fast. In our example, it should only be about the fact that the text is read out and displayed on the label as soon as the button is pressed.
Double click on the button opens the Click function, in which we write our code:
private void button1_Click(object sender, EventArgs e) { this.label1.Text = "Hello, " + this.textBox1.Text + "!"; }
Nothing special – the entered name of the text box is read and just written in the label. Of course you can extend your GUI as much as you like! You can find more information here.
Then, we save the project and compile it. Click on the “Start” button at the top of the taskbar. Your program will open and you can already test it.
Now open the directory where your project is located and change into the “bin/Debug” folder, which contains the compiled “WindowsFormsApp1.exe” file. These will be copied to the Raspberry Pi in the next step.
Test the GUI on the Raspberry Pi
Before the created GUI runs, we need to install some packages on the Raspberry Pi. For this, we use the Mono project. Open the terminal on the Raspberry Pi (via SSH) and enter the following (if you are not using Raspbian Strech, you will need to adjust the command accordingly):
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb http://download.mono-project.com/repo/debian raspbianstretch main" | sudo tee /etc/apt/sources.list.d/mono-official.list sudo apt-get update sudo apt-get install mono-devel --yes --allow-unauthenticated
If everything went well, we will now copy the .exe file to the Raspberry Pi. This is easiest via FTP. Connect with a FTP client (like FileZilla) and transfer the file to a directory (for me this is /home/pi/CustomGUI/).
Now open the desktop environment. Either by logging in with a screen / touchscreen or by using the Remote Desktop Protocol (RDC), for example.
There you also open the terminal and enter the following command (adjust name and path if necessary):
mono /home/pi/CustomGUI/WindowsFormsApp1.exe
This will open the GUI:
If you want a clickable file, you can create it as well:
sudo nano startGUI.sh
Insert the following:
#!/bin/bash mono /home/pi/CustomGUI/WindowsFormsApp1.exe
After saving, we finally give the necessary rights:
sudo chmod +x start.sh
You can now start this file by double clicking.
24 Comments
Nice tutorial, but how about to start the WindowsFormsApp1.exe on the raspberry from a windows client ? do you know how to do that ?
dijilius – you can remote into the raspberry pi. Check out xrdp or VNC.
Kinda late but if anyone is still looking at this you can run a node.js server from your raspberry pi and send requests to it from a remote computer to run certain commands. Just a thought and might not be the best way to do this!
Hello Dear,
I want to make a device to drive a machine. But i want to drive the machine using C# GUI Application. this app contain a MS Access database to save some data and machine Process config. to genarate report. Please tell me the Solution about this.
Thanks
hi i want to make a video jukebox with c sharp
is this posible can you give me some pointers
Hi Wayne, did you find any succes with this?
Install Windows IoT and you can have one foreground app running..
Will this work even if I terminate my SSH connection between Windows and Raspbian?
Thanks, this was really helpful. I’ve tried a few other tutorials but this one finally got my C# program working on the Pi.
I’m glad that it helped you!
can i ask if it support rpi4??
Yes, it does
I followed the steps carefully. What can possibly be wrong when PI gives the following error code :
Cannot open assembly ‘/home/pi/FTP/JPtest2.exe’: File does not contain a valid CIL image.
i got the same error, if you know please show me how to fix it, thanks you!
i tried to do the same as you. but my application returns an error:
Cannot open assembly ‘/home/pi/Downloads/netcore_Hello/bin/Debug/netcoreapp3.1/netcore_Hello.exe’: File does not contain a valid CIL image.
What should i do to fix this , thanks you!
You need to rename:
sudo chmod +x start.sh
to
sudo chmod +x startGUI.sh
For those of you getting the CIL image error – if you’re using the command line version of FTP, once FTP is running, type in BIN to switch it to binary mode, then transfer the file.
This article was very helpful! Thanks!
Hi,
I followed the steps but failed in my RPi 4B, below is detail error, could you kindly help with it?
Thank you very much.
Unhandled Exception:
System.TypeInitializationException: The type initializer for ‘System.Windows.Forms.XplatUI’ threw an exception. —> System.ArgumentNullException: Could not open display (X-Server required. Check your DISPLAY environment variable)
Parameter name: Display
at System.Windows.Forms.XplatUIX11.SetDisplay (System.IntPtr display_handle) [0x003d0] in :0
at System.Windows.Forms.XplatUIX11..ctor () [0x00077] in :0
at System.Windows.Forms.XplatUIX11.GetInstance () [0x00019] in :0
at System.Windows.Forms.XplatUI..cctor () [0x000c0] in :0
— End of inner exception stack trace —
at System.Windows.Forms.Application.EnableVisualStyles () [0x00006] in :0
at WindowsFormsApp1.Program.Main () [0x00001] in :0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for ‘System.Windows.Forms.XplatUI’ threw an exception. —> System.ArgumentNullException: Could not open display (X-Server required. Check your DISPLAY environment variable)
Parameter name: Display
at System.Windows.Forms.XplatUIX11.SetDisplay (System.IntPtr display_handle) [0x003d0] in :0
at System.Windows.Forms.XplatUIX11..ctor () [0x00077] in :0
at System.Windows.Forms.XplatUIX11.GetInstance () [0x00019] in :0
at System.Windows.Forms.XplatUI..cctor () [0x000c0] in :0
— End of inner exception stack trace —
at System.Windows.Forms.Application.EnableVisualStyles () [0x00006] in :0
at WindowsFormsApp1.Program.Main () [0x00001] in :0
Here’s my mono environment in RPi
Here’s my mono environment in RPI-4B
Mono JIT compiler version 6.12.0.107 (tarball Thu Dec 10 07:53:38 UTC 2020)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. http://www.mono-project.com
TLS: __thread
SIGSEGV: normal
Notifications: epoll
Architecture: armel,vfp+hard
Disabled: none
Misc: softdebug
Interpreter: yes
LLVM: yes(610)
Suspend: preemptive
GC: sgen (concurrent by default)
Hi,
Problem fixed after i enable VNC. Previous i used xrdp and the X-server is not started.
Great tutorial. Do you know how to run the winform app in headless raspberry pi? What needs to be installed to support the graphical side without the actual desktop?
ok it’s a bit more code but you could also use avaloniaui, and .net core, publish to a self contained single file, and have no dependencies to think of on the linux sidem you can even get dot net publish to create a linux executable.This also makes distribution a lot easier although the executable is a bit larger, but you could always gzip it
I get the same “File does not contain a valid CIL image” error as mentioned by a few people above. I am using the GUI in FileZilla to transfer the files, so there isn’t a BIN option as mentioned above. I have installed the dotnet-sdk version 3.1 and 5 on the pi. I have tried targeting both of these frameworks in Visual Studio when I compile the WinForms app, but neither works. Do you have any ideas?
hello dear , i made a camera app on c# windows form and want to run on respberry pi but it wont run what willbe the issue ?please guide me
Hi!
My problem (“File does not contain a valid CIL image.”) is the same or similar to that of others.
For my Raspberry Pi 4B with
Raspbian GNU/Linux 10 (buster)
I have installed mono as described or I have replaced in the above echo line “raspbianstretch” against “raspbianbuster”. As expected the result did not change:
When I try a hello world example https://superuser.com/questions/489328/file-does-not-contain-a-valid-cil-image
and type
mono helloworld.exe
it runs well (outputs hello world in the terminal).
I have created a GUI WindowsFormsApp1.exe in Visual Studio, very similar to the above, but in VB NET (not Visual C# NET) and simpler (it simply exits from the GUI when I press a button). When I double-click it in Windows it runs well. For file transfer to raspi I have tried two things:
– upload it in the web, download it with raspi
– zip it, upload it into the web, download it with raspi, unzip it
In both cases, when I type in the correct directory
mono WindowsFormsApp1.exe
I get
Cannot open assembly ‘WindowsFormsApp1.exe’: File does not contain a valid CIL image.
When I double-click helloworld.exe (runs in mono) or WindowsFormsApp1.exe (does not run in mono) a terminal seems to open for about 100 ms at the lower left corner of the screen.
I have also created startGUI.sh (with correct sudo chmod +x startGUI.sh). When I “Execute in Terminal”, a terminal seems to open for about 100 ms at the lower left corner of the screen. When I “Execute” there is no visible reaction.
Can you please help?