3
Hello Jaya
Please try the below code
let image = await axios.get('http://aaa.bbb/image.png', {responseType: 'arraybuffer'});
let returnedB64 = Buffer.from(image.data).toString('base64');
Thanks
3
chat gpt code is not working i tried but didnt get proper format
3
To convert an image from a URL to Base64 in ASP.NET, you can use the WebClient
class to download the image from the URL, then convert it to Base64. Here's a sample code snippet to achieve this:
using System;
using System.Net;
using System.IO;
public class ImageConverter
{
public static string ConvertToBase64(string imageUrl)
{
try
{
using (WebClient client = new WebClient())
{
// Download the image from the URL
byte[] imageData = client.DownloadData(imageUrl);
// Convert the byte array to a Base64 string
string base64String = Convert.ToBase64String(imageData);
return base64String;
}
}
catch (Exception ex)
{
// Handle exceptions if any
Console.WriteLine("Error: " + ex.Message);
return null;
}
}
public static void Main(string[] args)
{
string imageUrl = "https://example.com/image.jpg"; // Replace this with your image URL
string base64Image = ConvertToBase64(imageUrl);
if (base64Image != null)
{
Console.WriteLine("Base64 Image: " + base64Image);
}
else
{
Console.WriteLine("Failed to convert image to Base64.");
}
}
}
Replace "https://example.com/image.jpg"
with your actual image URL. When you run this code, it will download the image from the URL and convert it to a Base64 string. You can then use this Base64 string as needed in your application. Make sure to handle exceptions appropriately, especially when dealing with network operations.
