Popular blog tags

In HomeController.cs:

string path = FileStrem.GetFilePath("wwwroot/images/1ad6d6d772966fc112a68096dee8a315.png");

byte[] imagebyte= LoadImage.GetPictureData(path);

var base64 = Convert.ToBase64String(imagebyte);

ViewBag.imagesrc = string.Format("data:image/png;base64,{0}", base64);

ViewBag.imagelegth = base64.Length;

return View();

In Home.cshtml:

<img src="@ViewBag.imagesrc" /><br/>

<h1> Convert.ToBase64String(imagebyte).length: @ViewBag.imagelegth </h1>

Other code:

public static class LoadImage

{

public static byte[] GetPictureData(string imagePath)

{

FileStream fs = new FileStream(imagePath, FileMode.Open);

byte[] byteData = new byte[fs.Length];

fs.Read(byteData, 0, byteData.Length);

fs.Close();

return byteData;

}

}

public static class FileStrem

{

/// <summary>

/// get absolute path

/// </summary>

/// <param name="relativepath">"TextFile.txt"</param>

/// <returns></returns>

public static string GetFilePath(string relativepath)

{

return Path.Combine(Directory.GetCurrentDirectory(), relativepath);

}

}.