【问题标题】:Image manipulation: bitmap rotation (C++/GDI+)图像处理:位图旋转 (C++/GDI+)
【发布时间】: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 包含窗口图片,我已经检查过了。

【问题讨论】:

    标签: c++ bitmap rotation gdi+


    【解决方案1】:

    在我看来,你的方法有些倒退。 您需要做的是:

    1. 读入您的图片 (src)
    2. 找到包含旋转图像的最小边界矩形(即旋转角,最小和最大 x 和 y 之间的距离是尺寸。
    3. 使用这些尺寸和您想要的像素格式(可能与 src 相同,但也许您想要一个 Alpha 通道)和您想要的背景颜色 (dst) 创建一个新的 Image 对象
    4. 基于 dst (new Graphics(dst)) 创建图形
    5. 在图形上设置适当的变换
    6. 将 src 绘制到 dst 上
    7. 出口目的地

    好消息是,为了确保您做的事情正确,您可以隔离步骤。 例如,您可以只制作一个图像和一个图形并在其上绘制一条不带变换的线(或者最好是一个带有 X 的框)并保存它。如果你有你所期望的,那么你就在正确的道路上。接下来为盒子添加一个变换。在您的情况下,您将需要旋转和平移。接下来获取适合该旋转的目标图像的尺寸(提示:不要使用正方形进行测试)。最后,用你的实际图像来做。

    这将使您逐步获得正确的输出,而不是试图一次性获得全部内容。

    【讨论】:

    • 我按照 kon 的步骤进行操作,它是用 AHK ahkscript.org/boards/viewtopic.php?f=5&t=3458 写的。我确实测试了 AHK 代码并且它有效。然后我使用 AHK 库中的函数并以相同的顺序用 C++ 编写它们。但是 C++ 有点不同。恕我直言,语句的顺序应与 AHK 中的相同。 Ad3) 在 GdipCreateBitmapFromHBITMAP 中完成 ad4) 在 GdipGetImageGraphicsContext 中完成(但这可能是错误的)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多