I have a very strange issue.
Here is a simplified code of mine to explain :
class Bitmap1
{
public Bitmap nImage;
public IntPtr data;
public Bitmap1()
{
int w = 2450;
int h = 2450;
this.data = Marshal.AllocHGlobal(w*h);
nImage = new Bitmap(w, h, w, PixelFormat.Format8bppIndexed, data);
}
}
When w and h are equal to 2448, everything works well if I invocate the constructor.
But when h and w are equal to 2450, I have an ArgumentException which seems launch by the “new Bitmap(…);”
I can’t understand, and the documentation doesn’t say there is a limited size for Marshal.AllocHGlobal.
What’s wrong? Are there other ways to do what I want ?
Thank you very much.
http://msdn.microsoft.com/en-us/library/zy1a2d14.aspx
So you need something like the following:
This means there are 2 bytes between every line that are just padding and not part of the bitmap itself. When doing pointer arithmetic you obviously need to do
s*y+xand notw*y+xto account for the padding.