转换图像路径为base64字符串 [英] Convert image path to base64 string

查看:290
本文介绍了转换图像路径为base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何将图像转换为一个base64字符串在C#中?

How do you convert an image to a base64 string in C#?

例如,我有路径图像 C:/image/1.gif ,并想数据:图像/ GIF; BASE64 ,/ 9J / 4AAQSkZJRgABAgEAYABgAAD .. 返回。

For example, I have the path to the image C:/image/1.gif and would like data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD.. returned.

推荐答案

试试这个

  using (Image image = Image.FromFile(Path))
    {                 
        using (MemoryStream m = new MemoryStream())
        {
            image.Save(m, image.RawFormat);
            byte[] imageBytes = m.ToArray();

            // Convert byte[] to Base64 String
            string base64String = Convert.ToBase64String(imageBytes);
            return base64String;
        }                  
    }

这篇关于转换图像路径为base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆