Solved

DIBitmap

Posted on 2004-04-07
5
296 Views
Last Modified: 2012-05-04
How can I create a DIBitmap from a Device Context (example code, please)?
0
Comment
Question by:rossryan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Suggested Courses

752 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