473,796 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Ze ro);

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

IntPtr hBitmapXORBmp = XORBmp.GetHbitm ap();

IntPtr hDCScreenXORBmp = CreateCompatibl eDC(hDCScreen);

SelectObject(hD CScreenXORBmp, hBitmapXORBmp);

int format1 = GetDeviceCaps(h DCScreenXORBmp, GetDeviceCapsFl ag.BITSPIXEL);
// Gives that the HDC is 32 bpp

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

IntPtr hBitmapANDBmp = ANDBmp.GetHbitm ap();

IntPtr hDCScreenANDBmp = CreateCompatibl eDC(hDCScreen);

SelectObject(hD CScreenANDBmp, hBitmapANDBmp);

IntPtr hDCScreenOUTBmp = CreateCompatibl eDC(hDCScreen);

//IntPtr hBitmapOUTBmp = CreateBitmap(XO RBmp.Width, XORBmp.Height, 1,
BitsFromPixelFo rmat(XORBmp.Pix elFormat), IntPtr.Zero);

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

SelectObject(hD CScreenOUTBmp, hBitmapOUTBmp);

int format2 = GetDeviceCaps(h DCScreenOUTBmp, GetDeviceCapsFl ag.BITSPIXEL);
// Gives again that the HDC is 32 bpp

bool r1 = BitBlt(hDCScree nOUTBmp, 0, 0, XORBmp.Width, XORBmp.Height,
IntPtr.Zero, 0, 0, PatBltTypes.WHI TENESS);

bool r2 = BitBlt(hDCScree nOUTBmp, 0, 0, XORBmp.Width, XORBmp.Height,
hDCScreenANDBmp , 0, 0, PatBltTypes.SRC AND);

bool r3 = BitBlt(hDCScree nOUTBmp, 0, 0, XORBmp.Width, XORBmp.Height,
hDCScreenXORBmp , 0, 0, PatBltTypes.SRC PAINT);

Bitmap OutputBmp = Bitmap.FromHbit map(hBitmapOUTB mp);

....

....

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 = CreateCompatibl eBitmap(hDCScre enOUTBmp,
XORBmp.Width, XORBmp.Height);

So. If I replace that line by:

IntPtr hBitmapOUTBmp = CreateBitmap(XO RBmp.Width, XORBmp.Height, 1,
BitsFromPixelFo rmat(XORBmp.Pix elFormat), IntPtr.Zero);

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

But now the following line crash

Bitmap OutputBmp = Bitmap.FromHbit map(hBitmapOUTB mp);

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 CreateCompatibl eDC(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 2616

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

Similar topics

0
1488
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+' For 8bit multiframe TIFF-file it is working. Does anybody know if bitmap class supports 16bit multiframe TIFF files?
3
2309
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
9902
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 bitmap file (filename.bmp) I am experiencing some difficulties with writing a bitmap to a http response stream. I load the bitmap from a file. and i use the following snippet of code to write to HTTP response stream
3
8641
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
3956
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 else). It's when I add the I get an error. Extracts of my code and the error statement from the Visual Studio Debug bellow: Select Case cmd Case TwainCommand.CloseRequest EndingScan()
0
1400
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 error occurred in GDI+. at System.Drawing.Graphics.CheckErrorStatus(Int32 status) at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format) at...
7
10397
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 invoking just-in-time (JIT) debugging instead of this dialog box. ************** Exception Text ************** System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
1
3991
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 exception is thrown.
3
3323
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); //Stmt Correct where profile is class if (Data != null)
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10172
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.