Search code examples
cwindowsgraphicsgdi

Windows GDI: Difference Between a Pattern Brush & BitBlt of a Bitmap


Is there any functional difference between creating a GDI Pattern Brush with a BitMap then filling in a rect with that brush, and Blitting directly from a Device Independent Bitmap?

for clarification what i mean by the first scenario is creating a pattern brush using a bitmap, then just filling the entire screen with a patblt using PATCOPY. I mean certainly Blitting directy from the source Bitmap using bitblit seems a lot more efficient, but I'm not sure if they are functionally the same (very new to Windows, so sorry if this is a little vague or hard to understand)


Solution

  • Method 1: Create a pattern brush, selected it into the DC, and use PatBlt with PATCOPY

    Method 2: Select a DIB section into a memory DC and use BitBlt.

    The main differences between these methods are:

    • Method 1 will tile the image for you if the destination rectangle is larger than the source. With Method 2, you'd have to call BitBlt repeatedly.

    • With Method 2, you have to create and manage a memory DC.

    In terms of performance, they are probably approximately the same in modern versions of Windows. The mapping of the DIB colors to the destination's color format happens just once when selected into the DC. Given enough memory on the card, the image should be transferred over the graphics bus just once. Both methods probably have optimized paths for special cases.

    With PatBlt, you can re-use a monochrome pattern brush and set different colors, just by changing the text and background colors in the DC. With BitBlt, you'd have to update the bitmap in the memory DC first.

    If I recall correctly, in the old days, pattern brushes were limited in size to something pretty small (like hatch brushes). Pattern brushes were often monochrome (1 bit per pixel) and used to fill backgrounds by setting the text and background colors and quickly tiling them with PatBlt.