vc怎么让图片旋转,点一会儿就旋转一定角度

帮忙看看... 帮忙看看 展开
 我来答
百度网友297bcb5
2011-05-23
知道答主
回答量:10
采纳率:0%
帮助的人:9.5万
展开全部
/ RotateDIB - Create a new bitmap with rotated image
// Returns - Returns new bitmap with rotated image
// hDIB - Device-independent bitmap to rotate
// fDegrees - Angle of rotation in degree
// clrBack - Color of pixels in the resulting bitmap that do
// not get covered by source pixels
HDIB RotateDIB(HDIB hDIB, double fDegrees, COLORREF clrBack)
{
WaitCursorBegin();

// Get source bitmap info
LPBITMAPINFO lpBmInfo = (LPBITMAPINFO)GlobalLock(hDIB);
int bpp = lpBmInfo->bmiHeader.biBitCount; // Bits per pixel

int nColors = lpBmInfo->bmiHeader.biClrUsed ? lpBmInfo->bmiHeader.biClrUsed :
1 << bpp;
int nWidth = lpBmInfo->bmiHeader.biWidth;
int nHeight = lpBmInfo->bmiHeader.biHeight;
int nRowBytes = ((((nWidth * bpp) + 31) & ~31) / 8);

// Make sure height is positive and biCompression is BI_RGB or BI_BITFIELDS
DWORD compression = lpBmInfo->bmiHeader.biCompression;
if( nHeight < 0 || (compression!=BI_RGB))
{
GlobalUnlock(hDIB);
WaitCursorEnd();
return NULL;
}

LPBYTE lpDIBBits = FindDIBBits((LPBYTE)lpBmInfo);

// Convert angle degree to radians
#define PI 3.1415926
double radians = (fDegrees/90.0)*(PI/2);

// Compute the cosine and sine only once
float cosine = (float)cos(radians);
float sine = (float)sin(radians);

// Compute dimensions of the resulting bitmap
// First get the coordinates of the 3 corners other than origin
int x1 = (int)(-nHeight * sine);
int y1 = (int)(nHeight * cosine);
int x2 = (int)(nWidth * cosine - nHeight * sine);
int y2 = (int)(nHeight * cosine + nWidth * sine);
int x3 = (int)(nWidth * cosine);
int y3 = (int)(nWidth * sine);

int minx = min(0,min(x1, min(x2,x3)));
int miny = min(0,min(y1, min(y2,y3)));
int maxx = max(x1, max(x2,x3));
int maxy = max(y1, max(y2,y3));

int w = maxx - minx;
int h = maxy - miny;

// Create a DIB to hold the result
int nResultRowBytes = ((((w * bpp) + 31) & ~31) / 8);
long len = nResultRowBytes * h;
int nHeaderSize = ((LPBYTE)lpDIBBits-(LPBYTE)lpBmInfo) ;
HANDLE hDIBResult = GlobalAlloc(GHND,len+nHeaderSize);
// Initialize the header information
LPBITMAPINFO lpBmInfoResult = (LPBITMAPINFO)GlobalLock(hDIBResult);
memcpy( (void*)lpBmInfoResult, (void*)lpBmInfo, nHeaderSize);
lpBmInfoResult->bmiHeader.biWidth = w;
lpBmInfoResult->bmiHeader.biHeight = h;
lpBmInfoResult->bmiHeader.biSizeImage = len;

LPBYTE lpDIBBitsResult = FindDIBBits((LPBYTE)lpBmInfoResult);

// Get the back color value (index)
ZeroMemory( lpDIBBitsResult, len );
DWORD dwBackColor;
switch(bpp)
{
case 1: //Monochrome
if( clrBack == RGB(255,255,255) )
memset( lpDIBBitsResult, 0xff, len );
break;
case 4:
case 8: //Search the color table
int i;
for(i = 0; i < nColors; i++ )
{
if( lpBmInfo->bmiColors[i].rgbBlue == GetBValue(clrBack)
&& lpBmInfo->bmiColors[i].rgbGreen == GetGValue(clrBack)
&& lpBmInfo->bmiColors[i].rgbRed == GetRValue(clrBack) )
{
if(bpp==4) i = i | i<<4;
memset( lpDIBBitsResult, i, len );
break;
}
}
// If not match found the color remains black
break;
case 16:
// Windows95 supports 5 bits each for all colors or 5 bits for red & blue
// and 6 bits for green - Check the color mask for RGB555 or RGB565
if( *((DWORD*)lpBmInfo->bmiColors) == 0x7c00 )
{
// Bitmap is RGB555
dwBackColor = ((GetRValue(clrBack)>>3) << 10) +
((GetRValue(clrBack)>>3) << 5) +
(GetBValue(clrBack)>>3) ;
}
else
{
// Bitmap is RGB565
dwBackColor = ((GetRValue(clrBack)>>3) << 11) +
((GetRValue(clrBack)>>2) << 5) +
(GetBValue(clrBack)>>3) ;
}
break;
case 24:
case 32:
dwBackColor = (((DWORD)GetRValue(clrBack)) << 16) |
(((DWORD)GetGValue(clrBack)) << 8) |
(((DWORD)GetBValue(clrBack)));
break;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式