|
My search ends here. Thanks Dhanushka
|
|
|
|
|
void CUserProfile::rotateimage( BITMAPINFO imageBitmapInfo, unsigned char *imageBitValues )
{
HDC dibDC_bmp = CreateCompatibleDC( m_backBuffer );
int angle = 270;
float radians = (2 * 3.1416 * angle) / 360;
float cosine = (float)cos(radians);
float sine = (float)sin(radians);
float Point1x = (- imageBitmapInfo.bmiHeader.biHeight * sine);
float Point1y = ( imageBitmapInfo.bmiHeader.biHeight * cosine) + 1;
float Point2x = ( imageBitmapInfo.bmiHeader.biWidth * cosine-imageBitmapInfo.bmiHeader.biHeight * sine);
float Point2y = ( imageBitmapInfo.bmiHeader.biHeight * cosine+imageBitmapInfo.bmiHeader.biWidth * sine) + 1;
float Point3x = ( imageBitmapInfo.bmiHeader.biWidth * cosine);
float Point3y = ( imageBitmapInfo.bmiHeader.biWidth * sine) + 1;
float minx = min( 0, min( Point1x, min( Point2x, Point3x ) ) );
float miny = min( 0, min(Point1y,min( Point2y, Point3y ) ) );
float maxx = max( 0,max( Point1x, max( Point2x, Point3x ) ) );
float maxy = max( 0, max( Point1y, max( Point2y, Point3y ) ) );
int DestBitmapWidth = (int)ceil(maxx - minx) - 1;
int DestBitmapHeight = (int)ceil(maxy - miny)- 1;
int SrcNumberOfBytesPerRow = (((( imageBitmapInfo.bmiHeader.biWidth * imageBitmapInfo.bmiHeader.biBitCount ) + 31)&~31) / 8 );
BYTE * destbits;
BITMAPINFO destibmpInfo;
BITMAPINFOHEADER destibmpInfoHeader;
destibmpInfoHeader.biWidth = DestBitmapWidth;
destibmpInfoHeader.biHeight = DestBitmapHeight;
destibmpInfoHeader.biPlanes = 1;
destibmpInfoHeader.biBitCount = 3* 8;
destibmpInfoHeader.biCompression = BI_RGB;
destibmpInfoHeader.biSizeImage = DestBitmapWidth*DestBitmapHeight;
destibmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
destibmpInfoHeader.biXPelsPerMeter = 10000;
destibmpInfoHeader.biYPelsPerMeter = 10000;
destibmpInfo.bmiHeader = destibmpInfoHeader;
if( m_rotateImage != NULL ) {
DeleteObject( m_rotateImage );
m_rotateImage = NULL;
}
m_rotateImage = CreateDIBSection( dibDC_bmp, &destibmpInfo, DIB_RGB_COLORS, (VOID**)&destbits, NULL, 0 );
int DestNumberOfBytesPerRow = (((( DestBitmapWidth * destibmpInfoHeader.biBitCount ) + 31)&~31) / 8 );
for( int y = 0; y < DestBitmapHeight; y++ )
{
for( int x = 0; x < DestBitmapWidth; x++ )
{
int SrcBitmapx = (int)(( x + minx ) * cosine + ( y + miny ) * sine );
int SrcBitmapy = (int)(( y + miny ) * cosine - ( x + minx )* sine );
if( SrcBitmapx >= 0 && SrcBitmapx < imageBitmapInfo.bmiHeader.biWidth&&SrcBitmapy >=0 &&
SrcBitmapy < imageBitmapInfo.bmiHeader.biHeight )
{
for ( int i = 0; i < imageBitmapInfo.bmiHeader.biBitCount /8; i++ )
{
*( destbits + (DestNumberOfBytesPerRow * y) +
( x * imageBitmapInfo.bmiHeader.biBitCount / 8 + i ) ) =
*( imageBitValues + (SrcNumberOfBytesPerRow * SrcBitmapy ) +
( SrcBitmapx * imageBitmapInfo.bmiHeader.biBitCount / 8 + i));
}
}
}
}
if( m_isOriginalImageRotated == true )
{
SelectObject( m_originalPhotoDC, m_rotateImage );
m_originalImageBitmapInfo.bmiHeader.biHeight = DestBitmapHeight;
m_originalImageBitmapInfo.bmiHeader.biWidth = DestBitmapWidth;
m_originalImageBitmapInfo.bmiHeader.biSizeImage = DestBitmapWidth * DestBitmapHeight;
m_poriginalImageBitValues = destbits;
m_width = DestBitmapWidth;
m_height = DestBitmapHeight;
updateUserProfileImage( m_imageLeft - 318, m_imageTop - 25, m_imageRight- m_imageLeft, m_imageBottom - m_imageTop );
}
else
{ RECT rect;
rect.left = 318;
rect.top = 25;
rect.bottom = rect.top + m_cropImageHeight;
rect.right = rect.left + m_cropImageWidth;
InvalidateRect( m_huserProfile, &rect, true );
getCropImageDimension( DestBitmapWidth, DestBitmapHeight );
m_cropImageBitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_cropImageBitmapInfo.bmiHeader.biWidth =m_cropImageWidth;
m_cropImageBitmapInfo.bmiHeader.biHeight =m_cropImageHeight;
m_cropImageBitmapInfo.bmiHeader.biPlanes = 1;
m_cropImageBitmapInfo.bmiHeader.biBitCount = 24;
m_cropImageBitmapInfo.bmiHeader.biCompression = BI_RGB;
m_cropImageBitmapInfo.bmiHeader.biXPelsPerMeter = 10000;
m_cropImageBitmapInfo.bmiHeader.biYPelsPerMeter = 10000;
m_cropImageBitmapInfo.bmiHeader.biSizeImage = m_cropImageBitmapInfo.bmiHeader.biWidth * m_cropImageBitmapInfo.bmiHeader.biHeight;
if( m_hbCropImage != NULL ) {
DeleteObject( m_hbCropImage );
m_hbCropImage = NULL;
}
m_hbCropImage = CreateDIBSection( m_cropPhotoDC, &m_cropImageBitmapInfo, DIB_RGB_COLORS, (VOID**)&m_pcropImageBitValues, NULL, 0 );
SelectObject( m_cropPhotoDC, m_hbCropImage );
SelectObject( dibDC_bmp, m_rotateImage );
POINT pt;
GetBrushOrgEx( m_cropPhotoDC, &pt);
SetStretchBltMode( m_cropPhotoDC, HALFTONE );
SetBrushOrgEx( m_cropPhotoDC, pt.x, pt.y, NULL);
StretchBlt( m_cropPhotoDC, 0, 0, m_cropImageWidth, m_cropImageHeight, dibDC_bmp, 0, 0, DestBitmapWidth, DestBitmapHeight, SRCCOPY );
}
InvalidateRect( m_huserProfile, NULL, true);
drawBitmap( m_huserProfile, m_imageLeft, m_imageTop, m_imageRight, m_imageBottom );
RECT rect;
rect.left = 318;
rect.top = 25;
rect.bottom = rect.top + m_cropImageHeight;
rect.right = rect.left + m_cropImageWidth;
InvalidateRect( m_huserProfile, &rect, true );
m_isImageChanged = true;
if( m_imageRight >= m_cropImageWidth + 318 )
{
m_imageRight = (m_cropImageWidth) + 318;
m_imageLeft = m_imageRight - m_rectangleWidth;
if( m_imageLeft < 318 ) {
m_imageLeft = 318;
}
}
if( m_imageBottom >= m_cropImageHeight+ 25 )
{
m_imageBottom = m_cropImageHeight + 25;
m_imageTop = m_imageBottom - m_rectangleHeight;
if( m_imageTop < 25 ) {
m_imageTop = 25;
}
}
DeleteDC( dibDC_bmp );
}
|
|
|
|
|
Exactly what I needed right now. Thanks!
|
|
|
|
|
You can rotate image freely not just 270 and 90 degree, you didn't talk about that way...
|
|
|
|
|
Reason for my vote of 5
Excellent - just what I needed to know.
|
|
|
|
|
Im new to CodeProject...just put it to check..
|
|
|
|
|
Reason for my vote of 1
pitifully
|
|
|
|