C# Base64 String to PNG Image

Here’s how you can save a base64 encoded string to image file in C#.

public void SaveImageFile(string filename, string base64Image)
    string destinationImgPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Constants.UserImagesPath+"\\"+filename+".png";
        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Constants.UserImagesPath);
    catch (IOException iox)
        Console.WriteLine(iox.Message+" "+iox.Data);
    var bytes = Convert.FromBase64String(base64Image);
    using (var imageFile = new FileStream(destinationImgPath, FileMode.Create))
        imageFile.Write(bytes, 0, bytes.Length);

How to Calculate the Difference in Months Between two Dates C#

This code snippet lets you calculate the difference in months between two dates.

If all you want is simply a difference in the months -completely disregarding the date values- then you can use this:

public static int MonthDifference(this DateTime lValue, DateTime rValue)
    return (lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year);

Note that this returns a relative difference, meaning that if rValue is greater than lValue, then the return value will be negative. If you want an absolute difference, you can use this:

public static int MonthDifference(this DateTime lValue, DateTime rValue)
    return Math.Abs((lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year));

How to Print an Existing Excel File Using C#

In this tutorial I will show you how to print a Excel file using a WPF app. You might also want to look at a related post.

Create and Download Excel File Using PHP

Step 1: Add a reference to Excel Interop in your Project

Add a reference to Microsoft.Office.Interop.Excel in your project by right clicking on References, Add Reference from the Solution Explorer. Next choose Microsoft.Office.Interop.Excel from the Extensions tab under Assemblies.

Add a using statement at the top of the page in which you want to add the print XlS function.

using Excel= Microsoft.Office.Interop.Excel;

Step 2: Define Public Variables for Excel

Define the following variables before the class constructor.

public Excel.Application APP = null;
public Excel.Workbook WB = null;
public Excel.Worksheet WS = null;
public Excel.Range Range = null;  

Step 3: Add the Method to Print Excel

The following function opens a Excel file to print it and then closes it again.

void PrintMyExcelFile()
    this.APP = new Microsoft.Office.Interop.Excel.Application(); 

    // Open the Workbook:
    this.Open("C:\\Users\\maska\\Documents\\MyExcel.xlsx", 1);

    // Get the first worksheet.
    // (Excel uses base 1 indexing, not base 0.)
    WS = (Excel.Worksheet)WB.Worksheets[1];

    // Print out 1 copy to the default printer:
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    // Cleanup:


    WB.Close(false, Type.Missing, Type.Missing);


That’s it. Call this method to print an existing excel file.

How to Hide and Show the Console Window Associated with your own C# app?

Here’s how:

Add a reference to InteropServices in your class.

using System.Runtime.InteropServices;

Next, define two function signatures GetConsoleWindow and ShowWindow. These functions use kernel32.dll and user32.dll respectively.

static extern IntPtr GetConsoleWindow();

static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;

Finally, use these functions as follows. Get the handle by calling the GetConsoleWindow function.

var handle = GetConsoleWindow();

To hide a Window:

// Hide
ShowWindow(handle, SW_HIDE);

To show a Window:

// Show
ShowWindow(handle, SW_SHOW);

How to get the Month Name in C#?

You can use the CultureInfo to get the month name. Another way is to use extension methods. Here is an example of how to do it using extension methods:

Add a DateTimeExtensions class to your project.

static class DateTimeExtensions
    public static string ToMonthName(this DateTime dateTime)
        return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month);

    public static string ToShortMonthName(this DateTime dateTime)
        return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(dateTime.Month);

Now you can simply use these extension methods in your project like any other DateTime method.

using System;
using System.Globalization;

class Program
    static void Main()


Retrieve File Size From Remote URL using C#

The system API class System.Net.HttpWebRequest allows you to retrieve the file information programmatically. The size of each file/page generally available in the header section of web response, which you can query to get it.
Header contains Content-Length property value which gives you the size of the file in bytes. This you can later calculate to convert in MB or GB.

Here’s the source code that can be used:

private static string GetFileSize(Uri uriPath)
     var webRequest = HttpWebRequest.Create(uriPath);
     webRequest.Method = "HEAD";
     using (var webResponse = webRequest.GetResponse())
        var fileSize = webResponse.Headers.Get("Content-Length");
        var fileSizeInMegaByte = Math.Round(Convert.ToDouble(fileSize) / 1024.0 / 1024.0, 2);
        return fileSizeInMegaByte + " MB";