-1

How can I convert an image to a base64 format which is stored in my project folder? I have tried this. But it doesn't work

     public string ImageToBase64( string path)
    {
        string path = @"~/img/company.png";
        using (System.Drawing.Image image = System.Drawing.Image.FromFile(path))
        {
            using (MemoryStream m = new MemoryStream())
            {
                image.Save(m, image.RawFormat);
                byte[] imageBytes = m.ToArray();
                base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }
    }
Abdu
  • 185
  • 1
  • 7
  • 17
  • 1
    Try `path = Server.MapPath(path)`. Also note that you're currently overwriting the path parameter (I assume that's deliberate for this post, but pointing out in case accidental). – JohnLBevan Mar 07 '18 at 07:56
  • What error do you get? – FaizanHussainRabbani Mar 07 '18 at 08:04
  • What do you mean it with `it doesn't work`? Do you get any errors? – Jerodev Mar 07 '18 at 08:06
  • 1
    You know `doesn't work` just doesn't work, you need to specify 3 things to get an answer: what you did, what you get, what you expected – Alexey Klipilin Mar 07 '18 at 08:25
  • @FaizanRabbani this is the error.. Exception Details: System.IO.FileNotFoundException: D:\AbdulPC\mvc sample\eVitalz-mvc-version-69\eVitalz-mvc\Home\~img\company.png – Abdu Mar 07 '18 at 08:52
  • @FaizanRabbani .. but sir . It is there in my project. Sir "eVitalz-mvc" is my project folder. The image is in the Folder "img". Shall I change the path to "~/eVitalz-mvc/img/company.png"; – Abdu Mar 07 '18 at 09:00
  • See This: https://stackoverflow.com/a/21325753/7738489 – Dinkar Veer Mar 07 '18 at 10:29
  • 1
    @JohnLBevan . Thank you sir. Now it works. – Abdu Mar 09 '18 at 05:30
  • @Abdu no worries, I've given a slightly more detailed answer below to explain why this works; hopefully it makes sense / aids your understanding. Good luck with the rest of your project. – JohnLBevan Mar 09 '18 at 08:29

1 Answers1

1

Before calling System.Drawing.Image.FromFile(path) resolve the path from a URL to an absolute path. You can do this by calling Server.MapPath(path).

i.e.

string path = @"~/img/company.png";
path = Server.MapPath(path);
using (System.Drawing.Image image = System.Drawing.Image.FromFile(path))

or simply

string path = @"~/img/company.png";
using (System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(path)))

More Detailed Explanation

  • When dealing with resources on the web, we use URLs.
  • The URL ~/img/company.png says to navigate from the site's root (this is what the tilde (~) refers to) to the img subfolder, then to the file company.png.
  • The System.Drawing.Image class was not built for URLs, so doesn't know how to handle them. Rather it expects a path for the file system. The reasons it can't understand a URL are:
    • it doesn't know what tilde means
    • it doesn't know where the site's root is
    • even given path = @"img\company.png" it would try to find the file based on the working directory rather than the site's root directory. The current working directory is likely to be the location of IIS's executable (c:\Windows\System32\inetsrv\ though I'm not sure / it's probably best not to know as the idea of a working directory doesn't make sense in a web context).
    • it has no awareness of virtual paths; so even if the working directory were the same as the site's root and the tilde were missed, \img\ may sit in an entirely different location on disk to where it comes in the site's structure; perhaps even being mapped to a share on some remote file server).
  • Calling Server.MapPath resolves this URL to a real path which can be understood by non-web aware functions.

There's a great answer giving a lot more detail and examples on Server.MapPath here: https://stackoverflow.com/a/27500722/361842

JohnLBevan
  • 22,735
  • 13
  • 96
  • 178