Solved

DIBitmap

Posted on 2004-04-07
5
291 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now