2014-05-22 122 views
0

我花了很多時間試圖找到解決方案,但不能。我希望你能幫助我。代碼有點長,所以我只給出我遇到問題的部分。我的代碼從窗口捕獲位圖並將其保存在HBitmap中。我需要旋轉位圖。所以我啓動GDI +並從HBitmap創建位圖pBitmap:圖像處理:位圖旋轉(C++/GDI +)

// INIT GDI 
ULONG_PTR gdiplusToken; 
GdiplusStartupInput gdiplusStartupInput; 
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 
if (!gdiplusToken) return 3; 

// Gdip_GetRotatedDimensions: 
GpBitmap* pBitmap; 
int result = Gdiplus::DllExports::GdipCreateBitmapFromHBITMAP(HBitmap, 0, &pBitmap); 

然後我計算旋轉所需的變量。然後,我創建的圖形對象,並試圖旋轉圖像:

GpGraphics * pG; 
result = Gdiplus::DllExports::GdipGetImageGraphicsContext(pBitmap, &pG); 
Gdiplus::SmoothingMode smooth = SmoothingModeHighQuality; 
result = Gdiplus::DllExports::GdipSetSmoothingMode(pG, smooth); 
Gdiplus::InterpolationMode interpolation = InterpolationModeNearestNeighbor; 
result = Gdiplus::DllExports::GdipSetInterpolationMode(pG, interpolation); 
MatrixOrder MatrixOrder_ = MatrixOrderPrepend; 
result = Gdiplus::DllExports::GdipTranslateWorldTransform(pG, xTranslation, yTranslation, MatrixOrder_); 
MatrixOrder_ = MatrixOrderPrepend; 
result = Gdiplus::DllExports::GdipRotateWorldTransform(pG, ROTATION_ANGLE, MatrixOrder_); 
GpImageAttributes * ImgAttributes; 
result = Gdiplus::DllExports::GdipCreateImageAttributes(&ImgAttributes); // create an ImageAttribute object 
result = Gdiplus::DllExports::GdipDrawImageRectRect(pG,pBitmap,0,0,w,h,0,0,w,h,UnitPixel,ImgAttributes,0,0); // Draw the original image onto the new bitmap 
result = Gdiplus::DllExports::GdipDisposeImageAttributes(ImgAttributes); 

最後,我想檢查圖像,所以我說:

CLSID pngClsid; 
GetEncoderClsid(L"image/png", &pngClsid); 
result = Gdiplus::DllExports::GdipCreateBitmapFromGraphics(w, h, pG, &pBitmap); 
result = Gdiplus::DllExports::GdipSaveImageToFile(pBitmap, L"justest.png", &pngClsid, NULL); // last voluntary? GDIPCONST EncoderParameters* encoderParams 

但我的形象是空白。我發現GdipCreateBitmapFromGraphics會創建空白圖像,但我應該如何完成它以檢查我所做的圖紙?難道這些步驟正確(不只是在這裏,但上方,GdipCreateBitmapFromHBITMAP()和GdipGetImageGraphicsContext(附近)或我需要添加一些如何得到它的工作

PS:?我相信HBITMAP包含窗口的畫面,我已經。檢查它

回答

0

我的眼睛,你有一些事情向後你的方法 你需要做的是以下幾點:

  1. 讀你的圖片(SRC)
  2. 找到最小邊界矩形將包含旋轉的圖像(即,旋轉角和最小和最大x和y之間的距離是尺寸。
  3. 用這些尺寸和像素格式創建一個新的圖像對象(可能與src相同,但可能需要一個alpha通道)和背景顏色(dst)
  4. 創建一個基於dst new Graphics(dst)
  5. 設置在顯卡適當的變換
  6. 抽獎SRC DST上
  7. 出口DST

的好消息是,確保你正在做正確的事情,你可以隔離出來的步驟。 例如,您可以製作圖像和圖形,然後在不進行轉換的情況下繪製一條線(或者更好的是帶有X的框)並保存。如果你有你所期望的,那麼你就走在正確的道路上。接下來將變換添加到框中。在你的情況下,你需要旋轉和翻譯。接下來,獲取該旋轉的dest圖像的尺寸(protip:不要使用方形來測試)。最後,用你的實際圖像來做。

這將讓你一步一步地找到正確的輸出,而不是試圖一次性完成整個事情。

+0

我按照kon的步驟寫了這個文件在AHK http://ahkscript.org/boards/viewtopic.php?f=5&t=3458。我測試了AHK代碼,它工作。然後我使用AHK庫中的函數,並以相同的順序將它們寫入C++。但是C++有點不同。恕我直言,報表的順序應該保持不變,就像在AHK中一樣。 Ad3)在GdipGetImageGraphicsContext中完成(在GdipCreateBitmapFromHBITMAP ad4)中完成(但這可能是錯誤的) – user1141649