how to start with xamarin forms

Lets get some xamarin.forms basics done and check out how we can emulate this with focus to android. Why android focus? My test phone is based on android. So lets start here with some action. For this we need visual studio (in my case 2017 professional). So we create a new project -> Cross Plattform App (Xamarin) -> Emty App -> Xamarin.Forms -> portable class library (PCL). Just do not think to much about it if you are new to xamarin.

Now I choosed CameraApp.Android to debug things:

So visual studio got some emulator if you just installed the xamarin-part. Problem here is that it is incompatible with hyper-v. So if you do not want to disable hyper-v you should the Visual Studio Android Emulator for example. So check out this. With that you can set up some emulators that work with hyper-v without any further problems. I use the 5” KitKat (4.4) XXHDPI Phone. Install that one and it should look like this:

If you got this you should go for 2 further things to avoid problems with dll-uploading:

  • Go to hyper-v -> your new device -> settings -> processor -> compatibility -> and make sure that you put on the migration to other physical computers.
  • In visual-studio -> solution-explorer -> android solution -> properties -> android-options -> disable fast deployment
  • Update alle nuGet packages

So now we have to start our emulator by using the vs-android-emulator. Here we need to enable developer-mode and maybe usb-debugging. Than start debugging in visual studio and you should see the emulator running your code. If you get problems here you always have to check the output in vs. A lot of problems you can not see as a real fail in vs. But instead you get some information in the output-window. As well if you have trouble make sure that all nuget-packages are updated to the correct/last version. BTW: the last version is not the correct version in any case. And another thing that may can confuse you: Rebuild your solution. Sometimes you have to rebuild your solution to make vs get all new things that you did. However you should see something like this:


mysql basics in c#

Today I want to describe how we can work with MySQL-DBs in C#. Before Using queries and other things we have to connect to the database. Todo this set up an MySQLMgt-code-file that contains all things we need to have to work with our database. Here we got some properties like this. To use the MySsqlConnection just include the library “MySql.Data.MySqlClient”.

    static private MySqlConnection connection;
    static private string server;
    static private string database;
    static private string uid;
    static private string password;

I decide to fill these properties in the constructor and connect than to the server:

    static MySqlManagement()

For sure we can fill the properties hard-coded but I want to make it better and I put the things into the appsettings. Just put in the things you need and you can write these values into the class-properties:

static private void loadServerSettings()
        server = ConfigurationManager.AppSettings["server"];
        database = ConfigurationManager.AppSettings["database"];
        uid = ConfigurationManager.AppSettings["uid"];
        password = ConfigurationManager.AppSettings["password"];

To connect to the server we need a connection-string and a MySQL-Connection.

static private void connectServer()
        string connectionString =
            "SERVER=" + server + ";" +
            "DATABASE=" + database + ";" +
            "UID=" + uid + ";" +
            "PASSWORD=" + password + ";";
        connection = new MySqlConnection(connectionString);
        catch (Exception ex)
            throw ex;

Wit the connection. Open we are able to give some instruction/queries into our database. Here we can insert, modify and delete data or change the structure of the data as well. In the following you will see an easy insert-mysql-stament fired into the mysql-db.

string query = "INSERT INTO link (url, provider_id, posting_datetime) VALUES(" +
                    "'" + url + "', " +
                    "'" + providerId + "', " +
                    "'" + postingDateTime.ToString("yyyy-MM-dd H:mm:ss") + "')";
                MySqlCommand cmd = new MySqlCommand(query, connection);

Another thing that you should know in term of mysql-c# basics is the use of return value of queries. Here is method that tells me if an entry already exists:

static public bool UrlExists(string url)
           string query = "SELECT COUNT(*) FROM link WHERE url = '" + url + "'";
           MySqlCommand cmd = new MySqlCommand(query, connection);
           int Count;
           Count = int.Parse(cmd.ExecuteScalar() + "");
           return Count > 0;

@ the end of your code: Make sure to close the connection to the MySQLServer with:


Have fun with it! 🙂

c# webcrawling basics

To download the metadata and the content of a website you have some opportunities like this for example:

HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
WebRequest webRequest = (WebRequest)httpWebRequest;
WebResponse webResponse = webRequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream());
string htmlStr = sr.ReadToEnd();

While doing this you create a http-web-request and a web-response. With the stream-reader you will get the metadata and the content of the website and are able to write this into string.

dynamic read on JSON response in c#

If you want to work with a JSON-formatted response, there are many ways to handle this. In the following I could figure out a dynamic read that will give you the opportunity to use the different types.

The first code-example shows how to download some data with a given URL. For this you will need the assembly “System.Net”:

string url = "address?$format=json";
var webClient = new WebClient();
String jsonResponse = webClient.DownloadString(url);

The second code-example shows how to read out the single attributes. For this you have to include the assembly “Newtonsoft.Json”:

dynamic results = JsonConvert.DeserializeObject(jsonResponse);

string myName = results.Name;
string myFirstName = results.FirstName;
int myAge = results.Age;

After using the JsonConvert.DeserializeObject-function you can use every single attribute.

rotate an image in c#

There are different ways to rotate an image in C#. The first is probably the easiest one but provides just the basic 90, 180, 240 and 360 degree. Here is the way to do this:


For this example you need the library “System.Drawing” and an image.

The next example shows how to turn an image with an given angle:

private Bitmap rotateImage(Image thisImage, float angle)
 var newBitmap = new Bitmap(thisImage.Width, thisImage.Height);

 using (Graphics g = Graphics.FromImage(newBitmap))
  g.TranslateTransform(thisImage.Width / 2, thisImage.Height / 2);
  g.TranslateTransform(-thisImage.Width / 2, -thisImage.Height / 2);
  g.DrawImage(thisImage, new Point(0, 0));

 return newBitmap;

At the end of the function the grafic draw a new image. Here I used a new point (0/0). This is the orientation of the new draw and left-top of the new picture.

And this is the result:

transparency in c#

In .net and c# we have different opportunities to set something transparent. I want use a picture and add a blue semi-transparency. For this example I used a windows-forms-application and added a track bar and a picture box. In my code I use some functions of the library “System.Drawing.Drawing2D”. In the constructor of my main form I add the method “drawBlueTransparency” to the paint-event of the picture box.

public Form1()
 trackBar1.Maximum = 255;
 pictureBox1.Paint += new PaintEventHandler(drawBlueTransparency);

I also set the maximum of the track bar to 255 because this is the maximum of the alpha value that I used in the event-function (see variable  “alphaValue”). As well you could use a max of 10 and recalc the alphaValue.

private void drawBlueTransparency(object sender, PaintEventArgs e) 
 var rect = new Rectangle(20, 20, 184, 184); 
 var brush = new SolidBrush(Color.FromArgb(alphaValue, 0, 0, 255));
 e.Graphics.FillEllipse(brush, rect); 

The method just creates a new rectangle and use it to draw a ellips in it. For this it also needs a brush that is initialized by a color with the alphaValue. If the alphaValue is 0 you will see nothing of the blue ellipse and if you set the alphaValue to 255 (maximum) you just will see the blue ellipse without any transparency. Everything in between is like semi-transparent.

Now I can set different alpha values and use the method “.Refresh()” to redraw the picture box.

private void trackBar1_Scroll(object sender, System.EventArgs e)
 alphaValue = trackBar1.Value;

Here you can see the result:


Easy! 🙂