Base64 in C# and .NET

Published

In this guide

This developer guide on Base64Encoding explains csharp in plain language for developers and everyday users.

Everything important stays private: try the matching converter or tool in your browser — files and tokens are not uploaded.

Read the full English article below for code samples and edge cases, or switch language anytime from the menu. We keep expanding translations from real user feedback.

Try these private tools next

.NET exposes Base64 through the static Convert class, the high-performance System.Buffers.Text.Base64 API, and since .NET 9 a dedicated Base64Url class.

Strings

using System.Text;

string b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes("Grüße 👋"));
string text = Encoding.UTF8.GetString(Convert.FromBase64String(b64));

Safe decoding without exceptions

Convert.FromBase64String throws FormatException for invalid input. Use TryFromBase64String with a buffer instead:

Span<byte> buffer = new byte[input.Length * 3 / 4];
if (Convert.TryFromBase64String(input, buffer, out int written))
{
    var data = buffer[..written];
}

FromBase64String ignores whitespace (spaces, tabs, CR, LF) but rejects any other character, including the URL-safe - and _.

Base64URL

// .NET 9+
using System.Buffers.Text;
string token = Base64Url.EncodeToString(bytes);
byte[] back = Base64Url.DecodeFromChars(token);

// Older versions
static string ToBase64Url(byte[] b) =>
    Convert.ToBase64String(b).TrimEnd('=').Replace('+', '-').Replace('/', '_');

ASP.NET Core also offers WebEncoders.Base64UrlEncode in Microsoft.AspNetCore.WebUtilities.

Line breaks

Convert.ToBase64String(bytes, Base64FormattingOptions.InsertLineBreaks); // 76 chars per line

Files and streams

string b64 = Convert.ToBase64String(await File.ReadAllBytesAsync("photo.jpg"));
await File.WriteAllBytesAsync("copy.jpg", Convert.FromBase64String(b64));

// Streaming:
using var input = File.OpenRead("big.bin");
using var output = File.Create("big.b64");
using var transform = new CryptoStream(output, new ToBase64Transform(), CryptoStreamMode.Write);
await input.CopyToAsync(transform);

Image to data URI (ASP.NET)

var src = $"data:image/png;base64,{Convert.ToBase64String(pngBytes)}";

Verify results with the Base64 decoder.

Často kladené otázky

Does this page upload my data?

No. Articles are static. Linked tools run locally in your browser unless a feature explicitly needs a network relay.

Can I request a translation or a new tool?

Yes — use the private feedback form. We prioritize what users ask for across languages.

What is the fastest way to try csharp?

Open the homepage Base64 converter or the related tools linked on this page and paste a sample.

Související nástroje a návody

Komentáře (0)

Otázky, nápady nebo chyba v „Base64 in C# and .NET“? Napiš komentář, ohodnoť nástroj nebo odpověz ostatním.

Zatím žádné komentáře. Buď první, kdo se podělí o zpětnou vazbu!

Napsat komentář

Ohodnoť tento nástroj:
Esc
↑ ↓ přesunout↵ otevřítEsc zavřít