2014-05-22 124 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