I have an image (.png) and I want this picture to convert to binary. How can I do this?
I am using Webforms ASP.NET C#
I have an image (.png) and I want this picture to convert to binary. How can I do this?
I am using Webforms ASP.NET C#
byte[] b = File.ReadAllBytes(file);
Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
Try this:
Byte[] result
= (Byte[])new ImageConverter().ConvertTo(yourImage, typeof(Byte[]));
Since you have a file use:-
Response.ContentType = "image/png";
Response.WriteFile(physicalPathOfPngFile);
You could do:
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Png);
BinaryReader streamreader = new BinaryReader(stream);
byte[] data = streamreader.ReadBytes(stream.Length);
data would then contain the contents of the image.
First, convert the image into a byte array using ImageConverter class. Then specify the mime type of your png image, and voila!
Here's an example:
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Byte[]));
Response.ContentType = "image/png";
Response.BinaryWrite((Byte[])tc.ConvertTo(img,tc));