What is alphablend and what functions are used for it? - winapi

What is alphablend and what functions are used for it?

The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.
And the function used for alphablend is :
BOOL AlphaBlend(HDC hdcDest,
int nXOriginDest,
int nYOriginDest,
int nWidthDest,
int nHeightDest,
HDC hdcSrc,
int nXOriginSrc,
int nYOriginSrc,
int nWidthSrc,
int nHeightSrc,
BLENDFUNCTION blendFunction
);
Please refer the following link-> http://msdn.microsoft.com/en-us/library/aa452850.aspx

The AlphaBlend function displays bitmaps that have transparent or semitransparent pixels.

Related

How to get pixel color from a Pixmap

I can use XGetPixel() to get a pixel from an XImage. What do I use to get a pixel from a Pixmap?
It would be nice if there was at least one function to pull a pixel from the server's drawable. Sadly, there isn't one. However the following might be of some use.
Since Pixmap is a Drawable you can pass XGetImage() the Pixmap which will return a pointer to an XImage. Now that you have an XImage you can use XGetPixel().
XGetImage Parameters:
XImage *XGetImage(display, d, x, y, width, height, plane_mask, format)
Display *display;
Drawable d;
int x, y;
unsigned int width, height;
unsigned long plane_mask;
int format;
Better yet you could have a pre-created XImage and pass it, along with the Pixmap to XGetSubImage(). You can grab a single pixel by passing a width and height both set to 1, and then use XGetPixel() on your XImage.
XGetSubImage Parameters:
XImage *XGetSubImage(display, d, x, y, width, height, plane_mask, format, dest_image, dest_x,
dest_y)
Display *display;
Drawable d;
int x, y;
unsigned int width, height;
unsigned long plane_mask;
int format;
XImage *dest_image;
int dest_x, dest_y;
Note: XGetSubImage() returns a pointer to the same XImage structure specified by dest_image.
Generally it's a bad idea from performance point of view: never read from screen, only push to it. If you need to get pixel state, maintain local buffer of screen. If you can't modify program you are using, then +1 to Jonny Henly's answer: do a GetImage request first do download a region containing your pixel first, then read locally. If you want to access multiple pixels in a loop it's better to grab them all in one request

Conversion of unsigned char array to bitmap in MFC

I tried to convert from unsigned char array to Bitmap in MFC as follow.
BITMAPFILEHEADER* bmfh;
bmfh = (BITMAPFILEHEADER*)rgbByte;
BITMAPINFOHEADER* bmih;
bmih = (BITMAPINFOHEADER*)(rgbByte + sizeof(BITMAPFILEHEADER));
BITMAPINFO* bmi;
bmi = (BITMAPINFO*)bmih;
void* bits;
bits = (void*)(rgbByte + bmfh->bfOffBits);
HDC hdc = ::GetDC(NULL);
HBITMAP hbmp = CreateDIBitmap(hdc, bmih, CBM_INIT, bits, bmi, DIB_RGB_COLORS) ;
rgbByte is my unsigned char array and the problem is hbmp is always unused and never get data. What is the problem?
Thanks
HDC hdc = ::GetDC(NULL);
gets the DC of the entire screen. I doubt that is what you want. More likely you have a window or image control into which you want to place the bitmap. You need to get the DC of that window instead by passing to GetDC() it's m_hWnd member.

how to use ScreenToClient in win32, not MFC?

As MSDN said
BOOL ScreenToClient(
_In_ HWND hWnd,
LPPOINT lpPoint
);
the ScreenToClient's second para is a pointer to POINT,
and PINT said by MSDN is
typedef struct tagPOINT {
LONG x;
LONG y;
} POINT, *PPOINT;
it has only x and y. It's NOT like MFC ScreenToClient function, the para is a rect, and rect has width and height.
I am confused how to use win32 ScreenToClient function.
You can use MapWindowPoints() to convert a RECT in a single operation:
RECT r = ...;
MapWindowPoints(NULL, hWnd, (LPPOINT)&r, 2);
MFC actually has two methods, they're overloaded. One accepts a POINT structure, just like the Win32 function, the other accepts a RECT structure, both work the same way: it maps each point from screen-to-client.
If you have a RECT that you want to get client coordinates of without using MFC then just do it manually, like so:
RECT rect = GetMyRect();
POINT rectTL;
rectTL.x = rect.left;
rectTL.y = rect.top;
ScreenToClient( hWnd, &rectTL );
POINT rectBR;
rectBR.x = rect.right
rectBR.y = rect.bottom;
ScreenToClient( hWnd, &rectBR );
rect.left = rectTL.x;
rect.top = rectTL.y;
rect.right = rectBR.x;
rect.bottom = rectBR.y;
Note that RECT is
typedef struct _RECT {
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *PRECT;
and looks like two consecutive POINTs in memory. Therefore you can do what the MFC source code does, which is approx. the following (don't have the MFC source in front of me right now):
::ScreenToClient(hWnd, (POINT*)&rect->left);
::ScreenToClient(hWnd, (POINT*)&rect->right);
which is not the cleanest thing from a C point of view, but those structures are bound to remain binary compatible.

Rectangle manipulation is missing in Direct2D

Direct2D has D2D1_RECT_F
{
FLOAT left;
FLOAT top;
FLOAT right;
FLOAT bottom;
}
It's similar to GDI RECT structure except it uses float values.
typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT;
GDI provides all those RECT manipulation functions like
BOOL IntersectRect(
_Out_ LPRECT lprcDst,
_In_ const RECT *lprcSrc1,
_In_ const RECT *lprcSrc2
);
BOOL SubtractRect(
_Out_ LPRECT lprcDst,
_In_ const RECT *lprcSrc1,
_In_ const RECT *lprcSrc2
);
I can't believe Direct2D doesn't provide similar functions for D2D1_RECT_F.
I guess I can create rectangular geometries and combine them any way I want but that's creating and allocating objects instead of doing simple math.
Or I guess I may just create my own versions of them.
Am I missing something? Thanks.
For IntersectRect, Direct2D has ID2D1Geometry::CompareWithGeometry which will determine the relationship between two geometries, that's will work for you. note, this function only return the relationship of the two geometries, such as overlap, contains, it will not return the intersection rectangle as what IntersectRect did.
For SubtractRect, Direct2D has no such function, you need to write it yourself.

BitBlt from a color hDC to a monochrome hDC?

I am doing BitBlt from one 8 bit color DC to a monochrome DC. The problem is only pixel with values 255 are converted to white in the monochrome DC. How do I specify threshold value, so that any pixel with value greater than 128 is displayed as white in the monochrome DC.
You can't.
According to the MDSN page of MFC CDC::BitBlt :
To convert color to monochrome, BitBlt sets pixels that match the background color to white and sets all other pixels to black. BitBlt uses the foreground and background colors of the color device context to convert from color to monochrome.
and CDC::BitBlt calls ::BitBlt:
_AFXWIN_INLINE BOOL CDC::BitBlt(int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop)
{
ASSERT(m_hDC != NULL);
return ::BitBlt(m_hDC, x, y, nWidth, nHeight, pSrcDC->GetSafeHdc(), xSrc, ySrc, dwRop);
}

Resources