Click to See Complete Forum and Search --> : Gdiplus rotate image around center


dave2k
June 20th, 2007, 04:44 AM
i take a jpeg file and i want to roate it by 45 degrees for example, and then save this new image to file. so far i have been able to work out the new dimensions of the image, and rotate it. the trouble is i need to rotate the image around its center, at the moment it's not doing this. any ideas?

const double pi = 3.141592653;
const double pi2 = pi*2;

void GetBoundingBoxDimensions(int oldWidth,
int oldHeight,
double theta,
int& iBoxWidth,
int& iBoxHeight)
{
double adjacentTop, oppositeTop;
double adjacentBottom, oppositeBottom;

if( (theta >= 0.0 && theta < pi2) ||
(theta >= pi && theta < (pi + pi2) ) )
{
adjacentTop = abs(cos(theta)) * oldWidth;
oppositeTop = abs(sin(theta)) * oldWidth;

adjacentBottom = abs(cos(theta)) * oldHeight;
oppositeBottom = abs(sin(theta)) * oldHeight;
}
else
{
adjacentTop = abs(sin(theta)) * oldHeight;
oppositeTop = abs(cos(theta)) * oldHeight;

adjacentBottom = abs(sin(theta)) * oldWidth;
oppositeBottom = abs(cos(theta)) * oldWidth;
}

double newWidth = adjacentTop + oppositeBottom;
double newHeight = adjacentBottom + oppositeTop;

iBoxWidth = (int) ceil(newWidth);
iBoxHeight = (int) ceil(newHeight);
}

int _tmain(int argc, _TCHAR* argv[])
{
GdiPlusInitialize();

Bitmap* pBm = new Bitmap(L"test.jpg");

int w = pBm->GetWidth();
int h = pBm->GetHeight();

int wbox;
int hbox;
GetBoundingBoxDimensions(w, h, (45*pi/180), wbox, hbox);

Bitmap* pBm2 = new Bitmap(wbox, hbox);

Graphics* pG = Graphics::FromImage(pBm2);

//pG->Transl

//pG->tr
pG->RotateTransform(45);

pG->TranslateTransform(wbox/2,hbox/2);


//pG->TranslateTransform(0,0);

Rect rc(0, 0, w, h);

pG->DrawImage(pBm, rc, 0, 0, w, h, UnitPixel);

delete pG;

//////////////////////////////////////////////////////////////////////////
// Save

CLSID GifCodec;
GetCodecClsid(L"image/jpeg", &GifCodec);
pBm2->Save(L"res.jpg", &GifCodec, NULL);
return 0;
}