tags:

views:

399

answers:

2

How do I get bytes out of a PNG file using C#, (Reason for this:I need to pass the PNG as a string in an XML file.)

+9  A: 

System.IO.File.ReadAllBytes

System.Convert.ToBase64String

System.Convert.ToBase64String(System.IO.File.ReadAllBytes(filePath));
ChaosPandion
+1  A: 

Alternatively, if you have the PNG file in memory:

  • create a System.Drawing.Bitmap object out of it
  • Serialize it to a memory stream using Bitmap.Save() (pass in PNG as the encoding)
  • Use MemoryStream.GetBuffer() to retrieve the underlying byte array for the MS
  • Use Convert.ToBase64String(byte[], 0, memoryStream.Position) to convert it to a base64 string
LorenVS