Solved

DIBitmap

Posted on 2004-04-07
5
295 Views
Last Modified: 2012-05-04
How can I create a DIBitmap from a Device Context (example code, please)?
0
Comment
Question by:rossryan
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10781570
Give more details, please. Do you want to get inmanaged DIB handle from unmanaged Device Context handle using C# code, something like:

IntPtr CreateDINFromHDC(IntPtr hDC, int nWidth, int nHeight)
0
 

Author Comment

by:rossryan
ID: 10787164
Yes. :)
0
 

Author Comment

by:rossryan
ID: 10788755
           
                  this._hdcSrc = User32.GetDC(HWnd);
                  this._hdcDest = GDI32.CreateCompatibleDC(HdcSrc);
                  this._hBitmap = GDI32.CreateCompatibleBitmap(HdcSrc,
                        GDI32.GetDeviceCaps(HdcSrc,8),GDI32.GetDeviceCaps(HdcSrc,10));       
                  GDI32.SelectObject(HdcDest,HBitmap);
                  GDI32.BitBlt(HdcDest,0,0,GDI32.GetDeviceCaps(HdcSrc,8),
                        GDI32.GetDeviceCaps(HdcSrc,10),
                        HdcSrc,0,0,0x00CC0020);

I wish to use DIBitmap in place of the current bitmap extraction code.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10789535
CreateCompatibleBitmap creates DDB (device-dependent bitmap). It has the same structute as DC passed to it. For example, if DC is screen DC, changing of computer screen settings changes also DDB structure.
DIB is created with known structure, defined by programmer. To create DIB in C++ we need to fill the BITMAPINFO structure with all parameters required for DIB (color mode, compression etc.). In .NET we can create Bitmap class using this constructor:

public Bitmap(
   int width,
   int height,
   PixelFormat format
);

Appying GetHbitmap to this bitmap gives required DDB handle. To fill it you can select it in the memory device context and BitBlt from window device context.

I am confused with your code (what is source, destination, this._hBitmap, HBitmap). However, my guess using your code (not tested) is:

this._hdcSrc = User32.GetDC(HWnd);    // source DC
this._hdcDest = GDI32.CreateCompatibleDC(HdcSrc);    // destination memory DC

// Create bitmap with some pixel format (decide what you need)
Bitmap bmp = new Bitmap(GDI32.GetDeviceCaps(HdcSrc,8),GDI32.GetDeviceCaps(HdcSrc,10), PixelFormat.Format24bppRgb);

Int32 hBitmap = bmp.GetHbitmap();  // I beleive this is DDB handle, now we need to fill it

GDI32.SelectObject(HdcDest,hBitmap);

GDI32.BitBlt(HdcDest,0,0,GDI32.GetDeviceCaps(HdcSrc,8),
                   GDI32.GetDeviceCaps(HdcSrc,10),
                   HdcSrc,0,0,0x00CC0020);                     // Now hBitmap is filled


0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 10789537
Addition: don't forget to unselect hBitmap from HdcDest.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question