2014-01-24 90 views
53

如何将图像转换为C#中的base64字符串?将图像路径转换为base64字符串

例如,我有图像C:/image/1.gif的路径,并且想要返回data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD..

+2

的http:// www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string _to_image.aspx –

+0

如果要将它们嵌入到CSS中,请考虑配置可以为您处理此任务的编译系统(如Gulp.js) –

+0

您是否想要编码路径字符串或该位置的图像,给出一个数据URI? – Marcel

回答

103

试试这个

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; 
    } 
} 
+1

为什么还要留心呢?你可以只读取文件的字节并将其转换。 – Nyerguds

47

获取图像的字节数组(byte[])表示,然后使用Convert.ToBase64String(),st。像这样:

byte[] imageArray = System.IO.File.ReadAllBytes(@"image file path"); 
string base64ImageRepresentation = Convert.ToBase64String(imageArray); 

要将base4图像转换回一个System.Drawing.Image对象:

var img = Image.FromStream(new MemoryStream(Convert.FromBase64String(base64String))); 
+2

@史密斯,如果你的意思是从base64转换回'System.Drawing.Image',你可以使用st。像这样:'var img = Image.FromStream(new MemoryStream(Convert.FromBase64String(base64String)));' –

+0

谢谢,这对我很有用。 – GTCrais

-1

你可以将其转换这样

string test = @"C:/image/1.gif"; 
    byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(test); 
    string base64String = System.Convert.ToBase64String(bytes); 
    Console.WriteLine("Base 64 string: " + base64String); 

输出

QzovaW1hZ2UvMS5naWY= 
+0

先生谢谢,还有一个问题先生我怎么能声明作为我的图像标记的图像源,就像

+0

我是否像添加64base字符串到我的src像

+0

您不必将base 64作为图像源。正常的路径应该足够了。你面临的问题是什么? – Ehsan

4

这是我写的这个目的类:

public class Base64Image 
{ 
    public static Base64Image Parse(string base64Content) 
    { 
     if (string.IsNullOrEmpty(base64Content)) 
     { 
      throw new ArgumentNullException(nameof(base64Content)); 
     } 

     int indexOfSemiColon = base64Content.IndexOf(";", StringComparison.OrdinalIgnoreCase); 

     string dataLabel = base64Content.Substring(0, indexOfSemiColon); 

     string contentType = dataLabel.Split(':').Last(); 

     var startIndex = base64Content.IndexOf("base64,", StringComparison.OrdinalIgnoreCase) + 7; 

     var fileContents = base64Content.Substring(startIndex); 

     var bytes = Convert.FromBase64String(fileContents); 

     return new Base64Image 
     { 
      ContentType = contentType, 
      FileContents = bytes 
     }; 
    } 

    public string ContentType { get; set; } 

    public byte[] FileContents { get; set; } 

    public override string ToString() 
    { 
     return $"data:{ContentType};base64,{Convert.ToBase64String(FileContents)}"; 
    } 
} 

var base64Img = new Base64Image { 
    FileContents = File.ReadAllBytes("Path to image"), 
    ContentType="image/png" 
}; 

string base64EncodedImg = base64Img.ToString(); 
0

您可以使用Server.Map路径给出相对路径,然后你可以使用base64转换创建图像,也可以直接添加base64字符串image src

byte[] imageArray = System.IO.File.ReadAllBytes(Server.MapPath("~/Images/Upload_Image.png")); 

string base64ImageRepresentation = Convert.ToBase64String(imageArray); 
8

由于我们大多数人一样oneliners:

Convert.ToBase64String(File.ReadAllBytes(imageFilepath)); 

如果你需要它为Base64字节数组:

Encoding.ASCII.GetBytes(Convert.ToBase64String(File.ReadAllBytes(imageFilepath))); 
0

类似的东西

Function imgTo64(ByVal thePath As String) As String 
    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(thePath) 
    Dim m As IO.MemoryStream = New IO.MemoryStream() 

    img.Save(m, img.RawFormat) 
    Dim imageBytes As Byte() = m.ToArray 
    img.Dispose() 

    Dim str64 = Convert.ToBase64String(imageBytes) 
    Return str64 
End Function 
相关问题