What i am trying to do is to convert an image into a byte array and then write that byte array into a file. here’s the code
public static byte[] Convert(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
// or whatever output format you like
return ms.ToArray();
}
}
public Form1()
{
InitializeComponent();
Bitmap pic = new Bitmap("tulips.jpg");
pictureBox1.Image = pic;
byte[] img_array;
img_array = Convert(pic);
File.WriteAllBytes("test.txt", img_array);
}
Now I have been successfully able to convert the image into byte array. I have checked the values in byte array by means of a breakpoint and all of them are valid.
However when I try to write the array into a file and then open the file all I see is garbage.
Am I missing something?
Your
Bitmapis anImage. Why do you convert it into abytearray when you can simply callSave(documented here)To manipulate the image, you will usually access the
pixel data– what you have now contains the file type header as well as the pixels! SeeBitmap.LockBitsto manipulate the pixels (documented here)