aisa aisa - 2 months ago 41
C# Question

Difference between bitmap and bitmapdata

What is the difference between

in C#?

How to convert them to each other?


System.Drawing.Bitmap is an actual bitmap object. You can use it to draw to using a Graphics instance obtained from it, you can display it on the screen, you can save the data to a file, etc.

The System.Drawing.Imaging.BitmapData class is a helper object used when calling the Bitmap.LockBits() method. It contains information about the locked bitmap, which you can use to inspect the pixel data within the bitmap.

You can't really "convert" between the two per se, as they don't represent the same information. You can obtain a BitmapData object from a Bitmap object simply by calling LockBits(). If you have a BitmapData object from some other Bitmap object, you can copy that data to a new Bitmap object by allocating one with the same format as the original, calling LockBits on that one too, and then just copying the bytes from one to the other.