【发布时间】:2014-07-11 13:15:55
【问题描述】:
我花了很多时间试图找到解决方案,但找不到。我希望你能帮助我。代码有点长,所以我在这里只给出我遇到问题的部分。我的代码从窗口捕获位图并将其保存在 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 包含窗口图片,我已经检查过了。
【问题讨论】: