Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Q] Converting DC drawn objects to a DIB

1 view
Skip to first unread message

Michael D. Riston

unread,
Sep 27, 1996, 3:00:00 AM9/27/96
to

Hello,

I have a question relating to using DIBs. I am currently writing
an Win32 VC4.1/MFC application that requires me to take selected
graphics objects and convert them to a DIB format so I can place
them on the clipboard.

One approach is to create a DIB DC using the DIB.DRV driver, and then
draw all my objects to the DIB DC. This works (with some bugs in
the driver), but is it the best way? I would like to not have
to use DIB.DRV, because I have to distribute it with my application
and it does have bugs. I would like to use a Win32 built in DIB
mechanism.

I have seen documentation on using CreateDIBSection() but I am
confused how I can select it in to a DC that I can draw to. If
I use CreateCompatibleDC using the screen DC then as I draw I
will use the screens device and not the DIB device. This doesn't
seem to be the right approach.

In Summary: My questions are:

1) Can I use CreateDIBSection() somehow? I need to
draw all my objects to some DIB aware DC. How
do I do this?

2) If 1) doesn't work, then is there another way.

3) If 1) and 2) don't work, then I guess I must use
DIB.DRV's driver. Whay is the lastest version
available and where might I find it.

Thanks. You can e-mail to me at ris...@halcyon.com or post.

Mike...:)
--
--------------------------------------------------------------------------
Mike and Hilda Riston Email - ris...@halycon.com
Web - http://www.halcyon.com/riston

Mike Enright

unread,
Sep 30, 1996, 3:00:00 AM9/30/96
to

ris...@coho.halcyon.com (Michael D. Riston) wrote:

>I have seen documentation on using CreateDIBSection() but I am
>confused how I can select it in to a DC that I can draw to. If
>I use CreateCompatibleDC using the screen DC then as I draw I
>will use the screens device and not the DIB device. This doesn't
>seem to be the right approach.
>

It is the right approach, though. If I'm not mistaken, the display
driver would be in charge of your compatible DC and it will use some
well-tested code to carry out your TextOut or Polygon operations. For
example, in Win95 the driver would probably be a mini driver that used
the dib engine.


--
Mike Enright
menr...@cts.com
http://www.users.cts.com/sd/m/menright
Cardiff-by-the-Sea, California, USA


dave smith

unread,
Oct 3, 1996, 3:00:00 AM10/3/96
to


Michael D. Riston <ris...@coho.halcyon.com> wrote in article
<52gvc5$k...@news1.halcyon.com>...


> Hello,
>
> I have a question relating to using DIBs. I am currently writing
> an Win32 VC4.1/MFC application that requires me to take selected
> graphics objects and convert them to a DIB format so I can place
> them on the clipboard.


I'm a little confused about the question but here's what I make of a
solution:
Draw whatever you want to some DC, even a memory DC which is cfreated
from a compatible bitmap (I can show how this is done some other time).

2) get the bitmap from the DC.

3) construct a DIB from that bitmap as follows:

CDib *CPictureView::BitmapToDIB(CDC* pDC, CRect section)

{
BITMAP bm; // bitmap structure

DWORD dwLen; // size of memory block


int paletteSize;

paletteSize = 256 * sizeof(RGBQUAD) ; // later adjust for ncolor

// m_bitmap is the bitmap from CreateCompatibleBitmap

if (!GetObject(m_bitmap, sizeof(bm), (LPSTR)&bm))
return NULL;
CSize bsz;
BITMAP stBitmap;
m_bitmap.GetObject(sizeof(BITMAP), &stBitmap);


int sizeImage = 0;
int biBits = 8; // 8 bit color
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)

bsz = WIDTHBYTES((DWORD)stBitmap.bmWidth * biBits) * stBitmap.Height;


dwLen = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD) + bsz ;

int mBufferSize = dwLen + 1000L;
// create a slightly larger buffer for the bits
HGLOBAL hGlobal = ::GlobalAlloc (GMEM_FIXED, mBufferSize);
if (!hGlobal){

return (NULL);
}
// set the buffer to the dib bits:
unsigned char *MemoryBuffer =
(unsigned char *) ::GlobalLock (hGlobal);
m_bitmap.GetBitmapBits (mBufferSize, MemoryBuffer);
;

UINT pal[256][4];
GetSystemPalette(pDC, pal);

// now you have the bitmap in MemoryBuffer
// and the palette in pal
// its easy to construct a dib from this

::GlobalUnlock (hGlobal);
::GlobalFree(hGlobal);

hGlobal = NULL;


}
return 1;
}

> 1) Can I use CreateDIBSection() somehow? I need to
> draw all my objects to some DIB aware DC. How
> do I do this?

I would use CreateDIBSection() for the opposite, to go from a dib to a
bitmap:

BYTE *ptrbits;
HBITMAP hbmp;

hbmp = CreateDIBSection (dc.m_hDC, pDoc->GetBitmapInfo (),
DIB_RGB_COLORS,
(void**) &ptrbits, NULL, 0);
// if successful, install DIB Section bitmap into the memory DC

SelectObject (m_memDC.m_hDC,m_hDibSection);

// copy all DIB bits directly into the DIB Section bitmap
memcpy (ptrbits, GetDibBitsAddr(), sz.cy *
sz.cx);

0 new messages