簡體   English   中英

圖像處理:位圖旋轉(C ++ / GDI +)

[英]Image manipulation: bitmap rotation (C++/GDI+)

我花了很多時間試圖找到解決方案,但沒有。 我希望你能幫助我。 代碼較長,因此我在這里僅給出我遇到問題的部分。 我的代碼從窗口捕獲位圖並將其保存在HBitmap中。 我需要旋轉位圖。 所以我啟動GDI +並從HBitmap創建位圖pBitmap:

// 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包含窗口的圖片,我已經檢查過了。

在我看來,您的方法有些落后。 您需要執行以下操作:

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

好消息是,要確保您做對的事情正確,可以隔離一些步驟。 例如,您可以僅制作圖像和圖形並在其上繪制線條而無需進行變換(或者最好是帶有X的框)並將其保存。 如果您有期望,那么您就走對了。 接下來,將變換添加到框中。 在您的情況下,您既需要旋轉又需要平移。 接下來,獲得正確旋轉的目標圖像的尺寸(提示:請勿使用正方形進行測試)。 最后,使用您的實際圖像進行處理。

這將使您逐步獲得正確的輸出,而不是一目了然。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM