Solved

DIBitmap

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 30
C# TextBox 11 30
SQL Exceptions 3 39
C#.NET and microsoft certification. 3 34
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 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

17 Experts available now in Live!

Get 1:1 Help Now