ImageAttributes, SetColorKey, Windows and WindowsCE

WindowsCE supposedly should work under the windows (And I do need to run my program in Windows as well as CE).

However, when SetColorKey is used, it does work in WindowsCE and fails in Windows with "Out of Memory" exception. I found the solution (to add a Windows DLL with the following code) but I do not like it. Is there is a better way to deal with the problem?

publicstaticIntPtr CreateBitmap(

IntPtr phBitmap1,IntPtr phBitmap2,

int iLeft1,int iTop1,

int iLeft2,int iTop2,

int iSize

)

{

Rectangle rect =newRectangle(0, 0, iSize, iSize);

Bitmap pBitmap1 =Bitmap.FromHbitmap(phBitmap1);

Bitmap pBitmap2 =Bitmap.FromHbitmap(phBitmap2);

Bitmap pBitmapResult =newBitmap(iSize, iSize);

Graphics g =Graphics.FromImage(pBitmapResult);

ImageAttributes imgattr =newImageAttributes();

imgattr.SetColorKey(Color.White,Color.White);

g.DrawImage(pBitmap2, rect, iLeft2, iTop2, iSize, iSize,GraphicsUnit.Pixel);

g.DrawImage(pBitmap1, rect, iLeft1, iTop1, iSize, iSize,GraphicsUnit.Pixel, imgattr);

return pBitmapResult.GetHbitmap();

}

[2880 byte] By [Fiwel] at [2007-12-23]
# 1

Hi,

You mention that you are unable to use SetColorKey in a Compact Framework application running against the desktop CLR (is that the correct interpretation of your problem?). If you will post a sample of the code which does not work, then I may be able to better understand the problem and/or debug the issue for you.

Thanks,

-Chris Lorton

ChrisLorton at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2

Hi, I got the same error for the same situation. I don't kown if you have resolved it, and I just give my idea here.

I found that this error is not because of the usage of ImageAttributes, but the Bitmap.

I create a Bitmap using Bitmap.FromStream, and the wrong thing is that I closed the stream at once. This doen't bring any issue until I use the ImageAttributes.SetColor.

From MSDN, the remark about the Bitmap.FromStream says:

You must keep the stream open for the lifetime of the Image.

I removed the code of stream.close and keep the stream open as it says, and I resolved the "out of memory" issue. I don't know if you are also using a FromStream, but I hope this is helpful to you.

PandaRen at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3

Thank you!

Indeed, I send images to the CE device in a byte arrays:

CE code is:

static Bitmap LoadBitmap(byte [] pImage)

{

MemoryStream imageStream = new MemoryStream(pImage);

Bitmap image = new Bitmap(imageStream);

//imageStream.Close(); <- That was causing the assertion.

return image;

}

Fiwel at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...