I'm making some Bitmap image manipulation application in c# so I need to implicitly convert image to bmp so I can manipulate it like byte[] (byte array). Problem appears when this image is not bmp format - in that case after I convert it to Bitmap and after I convert this Bitmap into byte[] and apply some functions (by the way I "avoided" first 53 bytes - the header bytes) app breaks and I get "Parameter is not valid" message while trying to instance new Bitmap using MemoryStream on this byte[].
This is the code:
public void load(PictureBox pom)
{
OpenFileDialog o = new OpenFileDialog();
//o.Filter = "bin files (*.bin)|*.bin";
if (o.ShowDialog() == DialogResult.OK)
{
Bitmap b = (Bitmap)Image.FromFile(o.FileName);
pom.Image = b;
//pom.Image = new Bitmap(o.FileName);
this.p = pom;
this.input_bin_fajl = File.ReadAllBytes(o.FileName);
this.output_bin_fajl = new byte[this.input_bin_fajl.Length];
}
}
public static Bitmap ByteToImage(byte[] blob)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Seek(0, SeekOrigin.Begin);
// this is the breaking point
Bitmap bm = new Bitmap(mStream);
//
return bm;
}
}
Bitmapconstructor would expect you to have removed the header first? It needs that information to know the size of the image, and potentially any compression. Basically, you should pass the whole data to theBitmapconstructor. It's not like the header is just junk bytes... - Jon Skeet