Bitmap to ByteArray Conversion...?
-
hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz
Shanzay
-
hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz
Shanzay
-
hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz
Shanzay
DeepOceans wrote:
plz tell me the way to convert the Bitmap object to ByteArray in C#?
Do you want the object serialised, the raw image data, or compressed as an image format?
DeepOceans wrote:
I don't want to use the Memory stream as it causes problem with my System memory.
What kind of problem?
Despite everything, the person most likely to be fooling you next is yourself.
-
hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz
Shanzay
You should find out what problem using a MemoryStream is causing, because there shouldn't be any. And i thought that first reply was a good way to get it into a byte array without using a stream in memory (which would probably be the best solution) Apart from that you could write the data yourself, reading the colour of each pixel and then writing this data into an array - of course you'd need to check the bit-depth of the image and make sure you write the data correctly, plus you'd probably want to write the bitmap header as well... it's a fair bit of effort.
My current favourite word is: I'm starting to run out of fav. words!
-SK Genius
-
hi plz tell me the way to convert the Bitmap object to ByteArray in C#? I don't want to use the Memory stream as it causes problem with my System memory. reply me ASAP Regardz
Shanzay
Use a
LockBits
method. You should get something like this:unsafe static byte[] getBytes(BitmapData data)
{
byte* firstByte = (byte*)data.Scan0.ToPointer();
int size = data.Stride * data.Height;
byte[] bytes = new byte[size];
for (int i = 0; i < size; i++)
bytes[i] = firstByte[i];
return bytes;
}
unsafe static void setBytes(BitmapData data, byte[] bytes)
{
byte* firstByte = (byte*)data.Scan0.ToPointer();
int size = data.Stride * data.Height;
for (int i = 0; i < size; i++)
firstByte[i] = bytes[i];
}
static void Main()
{
using (Bitmap bmp = (Bitmap)Bitmap.FromFile(@"test.bmp")) {
BitmapData data = bmp.LockBits(new Rectangle(new Point(0), bmp.Size),
ImageLockMode.ReadOnly, bmp.PixelFormat);
byte[] bytes = getBytes(data);
bmp.UnlockBits(data);
using (Bitmap copy = new Bitmap(data.Width, data.Height, bmp.PixelFormat)) {
data = copy.LockBits(new Rectangle(new Point(0), copy.Size),
ImageLockMode.WriteOnly, copy.PixelFormat);
setBytes(data, bytes);
copy.UnlockBits(data);
copy.Save(@"test - copy.bmp");
}
}
}Compile with
/unsafe
. This code copies a BMP file. InstedgetBytes
andsetBytes
you may want to useSystem.Runtime.InteropServices.Marshal.Copy
method, but it has many function we don't need here so it's better to stick to a simplier version.Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.