473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Can't create an 4bpp Bitmap (A generic error occurred in GDI+.)

Hi guys,

This text looks long and complicate but it is not, and I really really need
some help here.

For the last 24hs I'm trying to resolve this issue (last night I dreamed
about it) and I can't see what is the problem.

I have to 2 Bitmaps coming from an Icon. One is the image itself or XORBmp
and one is the Mask or ANDBmp, I just want to produce a third image
combining both, but the result image MUST be the same format as the original
XORBmp. (if source is 4bpp then destination must be 4bpp)

The following code works, but the result always is a 32bpp image. (Same as
my DISPLAY color depth)

IntPtr hDCScreen = GetDC(IntPtr.Zero);

Bitmap XORBmp = srcBitmap; //<= 1, 4, 8, 24 bpp usually is 4bpp

IntPtr hBitmapXORBmp = XORBmp.GetHbitmap();

IntPtr hDCScreenXORBmp = CreateCompatibleDC(hDCScreen);

SelectObject(hDCScreenXORBmp, hBitmapXORBmp);

int format1 = GetDeviceCaps(hDCScreenXORBmp, GetDeviceCapsFlag.BITSPIXEL);
// Gives that the HDC is 32 bpp

Bitmap ANDBmp = mskBitmap; // <= 1bpp B&W Image or Mask

IntPtr hBitmapANDBmp = ANDBmp.GetHbitmap();

IntPtr hDCScreenANDBmp = CreateCompatibleDC(hDCScreen);

SelectObject(hDCScreenANDBmp, hBitmapANDBmp);

IntPtr hDCScreenOUTBmp = CreateCompatibleDC(hDCScreen);

//IntPtr hBitmapOUTBmp = CreateBitmap(XORBmp.Width, XORBmp.Height, 1,
BitsFromPixelFormat(XORBmp.PixelFormat), IntPtr.Zero);

IntPtr hBitmapOUTBmp = CreateCompatibleBitmap(hDCScreenOUTBmp,
XORBmp.Width, XORBmp.Height); // Creates a 32bpp becuase of
hDCScreenOUTBmp... not good

SelectObject(hDCScreenOUTBmp, hBitmapOUTBmp);

int format2 = GetDeviceCaps(hDCScreenOUTBmp, GetDeviceCapsFlag.BITSPIXEL);
// Gives again that the HDC is 32 bpp

bool r1 = BitBlt(hDCScreenOUTBmp, 0, 0, XORBmp.Width, XORBmp.Height,
IntPtr.Zero, 0, 0, PatBltTypes.WHITENESS);

bool r2 = BitBlt(hDCScreenOUTBmp, 0, 0, XORBmp.Width, XORBmp.Height,
hDCScreenANDBmp, 0, 0, PatBltTypes.SRCAND);

bool r3 = BitBlt(hDCScreenOUTBmp, 0, 0, XORBmp.Width, XORBmp.Height,
hDCScreenXORBmp, 0, 0, PatBltTypes.SRCPAINT);

Bitmap OutputBmp = Bitmap.FromHbitmap(hBitmapOUTBmp);

....

....

It is because the following line is creating the output image as 32bpp
(32bpp because is using the HDC created from my DISPLAY).

IntPtr hBitmapOUTBmp = CreateCompatibleBitmap(hDCScreenOUTBmp,
XORBmp.Width, XORBmp.Height);

So. If I replace that line by:

IntPtr hBitmapOUTBmp = CreateBitmap(XORBmp.Width, XORBmp.Height, 1,
BitsFromPixelFormat(XORBmp.PixelFormat), IntPtr.Zero);

I got what I want.. (BitsFromPixelFormat the only thing it does is return
the number of bits from the PixelFormat enum)

But now the following line crash

Bitmap OutputBmp = Bitmap.FromHbitmap(hBitmapOUTBmp);

Keeps giving "A generic error occurred in GDI+.".

QUESTION 1: So, why it fails, or how can I create the output .net Bitmap
with the same format as the incoming bitmap.

Please if you know what is wrong let me know, this is killing me.


What I suspect is that the next:

The MSDN help tell very clear this:

(CreateBitmap)

"After a bitmap is created, it can be selected into a device context by
calling the SelectObject function. HOWEVER, the bitmap can only be selected
into a device context if the bitmap and the DC have the same format.

QUESTION 2: Now how I'm supposed to create a Memory DC with the same format
as the output bitmap?????

My incoming bitmap is 4bpp and I have NO WAY to create a Memory DC other
than use the function CreateCompatibleDC(HDC.) where I have to put the
DISPLAY HDC. so the result HDC ALWAYS will be the same as the DISPLAY 16 or
32 bpp and not the format as the incoming bitmap "4bpp".

I tried to find the Whole internet how to create a Memory DC other than the
same as the DISPLAY color depth.. but no luck and no a single line how it
could be done.

If you think to use CreateDC.. I'm pretty sure that is not the way because
in the whole Google I can't find a single example how to use CreateDC with a
different DEVMode. So I'm assuming that not the right path at all.

Thanks,

Gustavo.


Oct 12 '06 #1
0 2584

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Greg | last post by:
Hello, I'm trying to load a 16bit multiframe TIFF-file in a bitmap, but it doesn't work. Error message: 'System.Runtime.InteropServices.ExternalException' 'A generic error occurred in GDI+' ...
3
by: Mats Boberg | last post by:
Hi, I have problems with saving a bitmap to hdd from my asp.net page I get the following error: "A generic error occurred in GDI+." Code: Bitmap bmp = new Bitmap(240,120);
2
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular...
3
by: CSH | last post by:
Hi all, I've run into a small problem opening and saving bitmaps. Concider the following code: Dim oBM as Bitmap Dim cFileName as String ... some code to get the filename etc...
0
by: Jørn Jensen | last post by:
Hey! I have an issue with creating bitmap from an IntPtr. The IntPtr is created from a Twain scanner, which seem to work perfectly in it's original state (the app is originally written by somebody...
0
by: manini | last post by:
Hello Everyone I m getting the generic GDI + error,Please help me to resolve this error ************** Exception Text ************** System.Runtime.InteropServices.ExternalException: A generic...
7
by: j4richard | last post by:
Help please, I am getting this "Unhandled Exception has occurred in your application" " A Generic error occurred in GDI+" See the end of this message for details on...
1
by: Jeff | last post by:
hi asp.net 2.0 I'm experimenting with GDI+ in asp.net and get an "A generic error occurred in GDI+." exception. Below is my code, I've marked a line with "<<<<-- here". It's here the...
3
by: uday1302 | last post by:
Hi Dear, Here I am trying to upload a photo. protected void LoadImage() { string UserName = Session.ToString(); byte Data = Profile.GetImageData(UserName);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.