本文介绍了使用Win32显示位图时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好我正在尝试显示图像&使用win32应用程序在我的窗口中旋转并显示它。参考文献[ http://www.codeguru。 com / cpp / gm / bitmap / specialeffects / article.php / c1743 / Rotate-a-bitmap-image.htm [ ^ ] 我的问题是图像显示为单色图像&不是彩色图像。否则此绘图操作无法返回true或false。 任何人都可以在我的代码中指出错误吗? 谢谢 问候, Balaji.R HBITMAP GetRotatedBitmap(HWND hWnd,HBITMAP hBitmapSource, float radians) { radians =( 2 * 3 。 14567 *弧度)/ 360; HDC sourceDC,destDC; sourceDC = CreateCompatibleDC(NULL); destDC = CreateCompatibleDC(NULL); COLORREF clrBack = 0 ; // 获取逻辑坐标 BITMAP bm; GetObject(hBitmapSource, sizeof (bm),& bm); float cosine =( float )cos(弧度); float sine =( float )sin(弧度); // 计算结果位图的尺寸 // 首先获取除原点以外的3个角的坐标 int x1 =( int )(bm.bmHeight * sine); int y1 =( int )(bm.bmHeight * cosine); int x2 =( int )(bm.bmWidth * cosine + bm.bmHeight * sine ); int y2 =( int )(bm.bmHeight * cosine - bm.bmWidth * sine ); int x3 =( int )(bm.bmWidth * cosine); int y3 =( int )( - bm.bmWidth * sine); int minx = min( 0 ,min(x1,min(x2) ,X3))); int miny = min( 0 ,min(y1,min(y2,y3)) ); int maxx = max( 0 ,max(x1,max(x2,x3)) ); int maxy = max( 0 ,max(y1,max(y2,y3)) ); int w = maxx - minx; int h = maxy - miny; // 创建一个位图来保存结果 HBITMAP hbmResult = CreateCompatibleBitmap(destDC,w,h); HBITMAP hbmOldSource =(HBITMAP)SelectObject(sourceDC,hBitmapSource); HBITMAP hbmOldDest =(HBITMAP)SelectObject(destDC,hbmResult); // 在我们更改映射模式之前绘制背景颜色 HBRUSH hbrBack = CreateSolidBrush(clrBack); HBRUSH hbrOld =(HBRUSH)SelectObject(destDC,hbrBack); PatBlt(destDC, 0 , 0 ,w,h,SRCCOPY); DeleteObject(hbrOld); // 我们将使用世界变换来旋转位图 SetGraphicsMode(destDC,GM_ADVANCED); XFORM xform; xform.eM11 =余弦; xform.eM12 = -sine; xform.eM21 =正弦; xform.eM22 =余弦; xform.eDx =( float ) - minx; xform.eDy =( float ) - miny; SetWorldTransform(destDC,& xform); BitBlt(destDC, 0 , 0 ,bm.bmWidth,bm。 bmHeight,sourceDC, 0 , 0 ,SRCCOPY); / / 最后,绘制它 SelectObject(sourceDC,hbmOldSource); SelectObject(destDC,hbmOldDest); return hbmResult; } void DrawImageWindow(HWND hWnd, float angle) { HDC hDC = GetWindowDC(hWnd); HBITMAP hBitmapDestination; HBITMAP hBitmapSource =(HBITMAP)LoadImage(NULL,L SourceImage.bmp,IMAGE_BITMAP , 0 , 0 ,LR_LOADFROMFILE | LR_CREATEDIBSECTION); hBitmapDestination =(HBITMAP)GetRotatedBitmap(hWnd,hBitmapSource,angle); BITMAP BMP; GetObject(hBitmapDestination, sizeof (BMP),& BMP); // 这里我们得到BMP标题信息。 HDC BMPDC = CreateCompatibleDC(NULL); // 这将保留BMP图像本身。 SelectObject(BMPDC,hBitmapDestination); // 将图像放入DC。 BitBlt(hDC, 0 , 0 ,BMP.bmWidth,BMP.bmHeight,BMPDC, 0 , 0 ,SRCCOPY); // 最后,绘制它 ReleaseDC(hWnd,hDC); // 别忘了清理! DeleteDC(BMPDC); DeleteObject(hBitmapSource); DeleteObject(hBitmapDestination); } LRESULT CALLBACK WndProc(HWND hWnd,UINT消息,WPARAM wParam,LPARAM lParam) { ... case WM_PAINT: hdc = BeginPaint(hWnd,& ps); DrawImageWindow(hWnd,angle); EndPaint(hWnd,& ps); break ; .... } 解决方案 我通过更改此行修复了此错误。 HBITMAP hbmResult = CreateCompatibleBitmap(destDC,w,h); to HBITMAP hbmResult = CreateCompatibleBitmap(GetDC(NULL),w,h); / / Hi i am trying to display a Image & Rotate and Display it in my window using win32 application. Ref[http://www.codeguru.com/cpp/g-m/bitmap/specialeffects/article.php/c1743/Rotate-a-bitmap-image.htm[^]My problem is the Image is appearing as a Monochrome Image & not a color Image. Otherwise this drawing operation fails returning true or false.Could anyone please point the bug in my code?Thank youRegards,Balaji.RHBITMAP GetRotatedBitmap( HWND hWnd,HBITMAP hBitmapSource, float radians){radians = (2 * 3.14567 *radians)/360;HDC sourceDC, destDC;sourceDC = CreateCompatibleDC(NULL);destDC = CreateCompatibleDC(NULL);COLORREF clrBack=0;// Get logical coordinates BITMAP bm;GetObject(hBitmapSource,sizeof( bm ), &bm );float cosine = (float)cos(radians);float sine = (float)sin(radians);// Compute dimensions of the resulting bitmap// First get the coordinates of the 3 corners other than originint x1 = (int)(bm.bmHeight * sine);int y1 = (int)(bm.bmHeight * cosine);int x2 = (int)(bm.bmWidth * cosine + bm.bmHeight * sine);int y2 = (int)(bm.bmHeight * cosine - bm.bmWidth * sine);int x3 = (int)(bm.bmWidth * cosine);int y3 = (int)(-bm.bmWidth * sine);int minx = min(0,min(x1, min(x2,x3)));int miny = min(0,min(y1, min(y2,y3)));int maxx = max(0,max(x1, max(x2,x3)));int maxy = max(0,max(y1, max(y2,y3)));int w = maxx - minx;int h = maxy - miny;// Create a bitmap to hold the resultHBITMAP hbmResult = CreateCompatibleBitmap(destDC, w, h);HBITMAP hbmOldSource = (HBITMAP)SelectObject( sourceDC, hBitmapSource ); HBITMAP hbmOldDest = (HBITMAP)SelectObject( destDC, hbmResult );// Draw the background color before we change mapping mode HBRUSH hbrBack = CreateSolidBrush( clrBack ); HBRUSH hbrOld = (HBRUSH)SelectObject( destDC, hbrBack );PatBlt(destDC,0,0, w, h, SRCCOPY ); DeleteObject(hbrOld);// We will use world transform to rotate the bitmapSetGraphicsMode(destDC, GM_ADVANCED);XFORM xform;xform.eM11 = cosine;xform.eM12 = -sine;xform.eM21 = sine;xform.eM22 = cosine;xform.eDx = (float)-minx;xform.eDy = (float)-miny;SetWorldTransform( destDC, &xform );BitBlt(destDC, 0, 0,bm.bmWidth, bm.bmHeight,sourceDC, 0, 0, SRCCOPY );// Finally, Draw itSelectObject( sourceDC, hbmOldSource ); SelectObject( destDC, hbmOldDest );return hbmResult;}void DrawImageWindow(HWND hWnd,float angle){HDC hDC = GetWindowDC(hWnd);HBITMAP hBitmapDestination;HBITMAP hBitmapSource = (HBITMAP)LoadImage(NULL, L"SourceImage.bmp", IMAGE_BITMAP,0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);hBitmapDestination = (HBITMAP)GetRotatedBitmap(hWnd,hBitmapSource,angle);BITMAP BMP;GetObject(hBitmapDestination, sizeof(BMP), &BMP); // Here we get the BMP header info.HDC BMPDC = CreateCompatibleDC(NULL); // This will hold the BMP image itself.SelectObject(BMPDC, hBitmapDestination); // Put the image into the DC.BitBlt(hDC, 0, 0, BMP.bmWidth, BMP.bmHeight, BMPDC, 0, 0, SRCCOPY); // Finally, Draw itReleaseDC(hWnd,hDC);// Don't forget to clean up!DeleteDC(BMPDC);DeleteObject(hBitmapSource);DeleteObject(hBitmapDestination);}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){...case WM_PAINT:hdc = BeginPaint(hWnd, &ps);DrawImageWindow(hWnd,angle);EndPaint(hWnd, &ps);break;....} 解决方案 I have fixed this bug by changing this line.HBITMAP hbmResult = CreateCompatibleBitmap(destDC, w, h);toHBITMAP hbmResult = CreateCompatibleBitmap(GetDC(NULL), w, h);// 这篇关于使用Win32显示位图时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 19:58