Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DIBitmap

Posted on 2004-04-07
5
Medium Priority
?
302 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 2000 total points
ID: 10789537
Addition: don't forget to unselect hBitmap from HdcDest.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

916 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