0

I want to convert Image to Base64String format,For that I am using this code

string conversionData = imageToBase64("http//localhost/MyService/Images/myImage.png");

the code of "imageToBase64()" is

private string imageToBase64(string path)
{
  using(Image image = Image.FromFile(path))
  {
   using(MemoryStream m = new MemoryStream())
   {
    byte[] imageBytes = m.ToArray();
    string base64String = Convert.ToBase64String(imageBytes);
    return base64String;
   }
  }
 }

When I am passing path to this method I am getting the error "An exception of type 'System.ArgumentException' occered ...". thanks in advance

nenu235
  • 48
  • 7

2 Answers2

0

take a look :https://stackoverflow.com/questions/21325661/convert-image-path-to-base64-string.You missed image.Save(m, image.RawFormat) Hope it will help

0

The parameter of fromfile cannot be URI, otherwise an error will be reported.

enter image description here

The interface that accesses WCF will return stream. We need to use the Httpwebrequest request interface to receive its stream,then save it locally before using fromfile.Here is a Demo:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:8000/Service/GetImage?width=50&height=40");
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (File.Exists("d:\\name.png"))
                File.Delete("d:\\name.png");
            Stream outStream = System.IO.File.Create("d:\\name.png");
            Stream inStream = response.GetResponseStream();
            int l;
            byte[] buffer = new byte[1024];
            do
            {
                l = inStream.Read(buffer, 0, buffer.Length);
                if (l > 0)
                    outStream.Write(buffer, 0, l);
            }
            while (l > 0);
            outStream.Close();
            inStream.Close();

           Image image = Image.FromFile("d:\\name.png");
Ding Peng
  • 3,702
  • 1
  • 5
  • 8