Read and Write GPS coordinates with RedCorners.ExifLibrary

I recently published RedCorners.ExifLibrary, a NuGet that lets you read and write EXIF metadata in image files. This is basically a fork of the ExifLibrary project by oozcitak, with four minor changes:

  • It removes dependencies to System.Drawing, making the library work across all .net Standard 2.0 compatible runtimes, including Xamarin.iOS and Xamarin.Android.
  • It adds a conversion helper method for going back and forth between floating point or Degrees/Minutes/Seconds representations for coordinates.
  • It adds helper methods to quickly read and write GPS coordinates in the image files.
  • Everything is moved to the RedCorners.ExifLibrary namespace, in order to make it possible to use both the original library and my version of it simultaneously.

It has been tested on Xamarin.iOS, Xamarin.Android, .NET Core and .NET Framework on JPEG images.

You can get the NuGet from here: https://www.nuget.org/packages/RedCorners.ExifLibrary

Some quick examples:


using RedCorners.ExifLibrary;

// Read GPS Coordinates
var file = ImageFile.FromFile(Path);
var coords = file.GetGPSCoords();
if (coords.HasValue)
    (Latitude, Longitude) = coords.Value;

// Write GPS Coordinates
var file = ImageFile.FromFile(Path);
file.SetGPSCoords(Latitude, Longitude);
file.Save(Path);

More information on GitHub.

REST requests with Xamarin and RestSharp

RestSharp (available on NuGet) is a clean way to send HTTP requests and handle their response. As a portable package, you can use it in your Xamarin.iOS and Xamarin.Android applications. The following example, POSTs a serialized JSON body, created automatically from a C# class, to an endpoint located at https://myserver.com/api/messages/message. It also sends a token parameter inside the query string:

public class MessageDTO : DTO
{
    public string Sender { get; set; }
    public string Recipient { get; set; }
    public string Body { get; set; }
}
public static class PinHelper
{
    async void SendMessage(MessageDTO dto)
    {
        const string ApiPath = "https://myserver.com/api/";
        var client = new RestClient (ApiPath);
        var request = new RestRequest ("messages/message", Method.POST);
        request.AddQueryParameter ("token", Settings.Token);
        request.AddJsonBody (dto);

        try
        {
            var result = await client.ExecuteTaskAsync(request);
            Debug.WriteLine($"Result Status Code: {result.StatusCode} - {result.Content}");
        }
        catch (Exception e) {
            Debug.WriteLine ($"Error: {e.Message}");
        }
    }
}

On an ASP.NET WebApi controller with EntityFramework, this request can be received and stored using a function that looks like this:

[HttpPost][Route("message")]
public IHTTPActionResult AddMessage(string token, [FromBody] MessageDTO dto)
{
    Message message = new Message ();
    dto.Inject (message);
    db.Messages.Add (message);
    db.SaveChanges ();

    return Ok ();
}

Mono Frustrations: “The authentication or decryption has failed.”

Often calling the AuthenticateAsClient method of an SslStream results in the “The authentication or decryption has failed.” error, because Mono cannot verify the certificate. As a last resort, the error can be circumvented by disabling the certificate validation by adding the following line to the code:

ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;

If you are creating the SslStream in a using block, you can do this instead:

using (SslStream s = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback((o, certificate, chain, errors) => true)))
{
s.AuthenticateAsClient(uri.Host, null, SslProtocols.Tls, false);
...
}

Mono Frustrations: WebRequest

The WebRequest class has many issues, especially in Mono. A major problem occurs when the response code is an HTTP error (e.g. 4xx), for which the class throws an Exception, causing a Runtime Error if it goes unhandled. This is not a big deal in .NET, because the request.GetResponse() call can be surrounded by a try/catch block that catches the thrown WebException, which still contains the response in its Response property. However, a bug in Mono makes it impossible to retrieve the response from a WebException, as the Response property is always null.

Solution: It is a better idea to use a TcpClient instead and manually generate an HTTP request using it.