In the use of fromhbitmap you will find out the figure is with black edge, this is because this function has a bug, the solution is to use the following function to convert, the general meaning is that you memcpy do not use Fromhbitmap function.
bitmap* Createbitmapfromhbitmap (in Hbitmap hbitmap) {Bitmap bmp = {0}; if (0 = = GetObject (hbitmap, sizeof (BITMAP), (LPVOID) &bmp)) {return FALSE; } BYTE *piexlssrc = NULL; LONG cbsize = bmp.bmwidthbytes * bmp.bmheight; PIEXLSSRC = new Byte[cbsize]; Bitmapinfo Bmpinfo = {0}; bmpInfo.bmiHeader.biSize = sizeof (Bitmapinfoheader); BmpInfo.bmiHeader.biWidth = Bmp.bmwidth; BmpInfo.bmiHeader.biHeight = Bmp.bmheight; BmpInfo.bmiHeader.biPlanes = Bmp.bmplanes; BmpInfo.bmiHeader.biBitCount = Bmp.bmbitspixel; BmpInfo.bmiHeader.biCompression = Bi_rgb; HDC hdcscreen = CreateDC (L "DISPLAY", NULL, Null,null); LONG cbcopied = GetDIBits (hdcscreen, hbitmap, 0, Bmp.bmheight, PIEXLSSRC, &bmpinfo, dib_rgb_colors); DeleteDC (Hdcscreen); if (0 = = cbcopied) {delete [] piexlssrc; return FALSE; } Bitmap *pbitmap = new Bitmap (Bmp.bmwidth, Bmp.bmheight, Pixelformat32bpppargb); BitmapData BitmapData; Rect rect (0, 0, bmp.bmwidth, bmp.bmheight); if (Ok! = Pbitmap->lockbits (&rect, Imagelockmoderead, Pixelformat32bpppargb, &bitmapdata)) {Safe_delete (PBITMAP); return NULL; } BYTE *pixelsdest = (byte*) bitmapdata.scan0; int nlinesize = bmp.bmwidth * sizeof (UINT); int nheight = Bmp.bmheight; for (int y = 0; y < nheight; y++) {memcpy_s (pixelsdest + y * nlinesize), nlinesize, (Piexlssrc + (NHEIGHT-Y-1) * nlinesize), nlinesize); } if (Ok! = pbitmap->unlockbits (&bitmapdata)) {delete pbitmap; } delete [] piexlssrc; return pbitmap; }
Reference information: http://blog.sina.com.cn/s/blog_5f8817250100g1dj.html
Https://groups.google.com/forum/#!topic/microsoft.public.win32.programmer.gdi/stjG06_EPfM
Fromhbitmap This function will lose transparency information.