`

VC旋转图片

阅读更多
转自:http://hi.baidu.com/sweetpigss/blog/item/0496efdc5d1967d08c10297e.html

如果你的应用程序仅工作在Windows NT下,那么你可以通过API函数旋转你的位图。
你或者使用world transformation和BitBlt()或者使用PlgBlt()旋转位图。一个
使用第一种方法的函数显示在下面。

如果你的目标是多平台的,那么你的任务变得非常困难。你只能通过旋转源位图中
每个象素或者直接操作DIB字节得到旋转位图。第一种方法通过每个点的处理是非
常慢的,第二种方法是很复杂的,但它有足够快的速度。注:下面的所有函数旋转
后产生新的位图,如果你需要直接绘制位图,请自已修改函数。 其中函数1仅工作
在NT环境下,它是最简单也是最快的,可惜它不支持Windows95。

所有的函数所接受的角度单位是弧度,如果是角度单位是度请用下面的公式转换。

radian = (2*pi *degree)/360

旋转步骤:

创建一对与设备兼容的显示设备。一个用于源位图,一个用于旋转后的目标位图。

预计算正弦和余弦函数值,这样可以避免重复计算。

用下面的公式计算旋转图像后的矩形
newx = x.cos(angle) + y.sin(angle)
newy = y.cos(angle) - x.sin(angle)

旋转后的位图将不能占用整个新位图,我们将用背景色填充它。

点阵转换公式
newx = x * eM11 + y * eM21 + eDx
newy = x * eM12 + y * eM22 + eDy
其中eM11和eM22是角度的余弦值,eM21是角度的正弦,eM12是eM21的负值。 eDx & eDy
目的是旋转后的位图在新的位图不被剪切。

函数一:适用于NT
// GetRotatedBitmapNT - Create a new bitmap with rotated image
// Returns  - Returns new bitmap with rotated image
// hBitmap  - Bitmap to rotate
// radians  - Angle of rotation in radians
// clrBack  - Color of pixels in the resulting bitmap that do
//     not get covered by source pixels
HBITMAP GetRotatedBitmapNT( HBITMAP hBitmap, float radians, COLORREF clrBack )
{
 // Create a memory DC compatible with the display
 CDC sourceDC, destDC;
 sourceDC.CreateCompatibleDC( NULL );
 destDC.CreateCompatibleDC( NULL );

 // Get logical coordinates
 BITMAP bm;
 ::GetObject( hBitmap, sizeof( bm ), &bm );

 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)(bm.bmHeight * sine);
 int y1 = (int)(bm.bmHeight * cosine);
 int x2 = (int)(bm.bmWidth * cosine + bm.bmHeight * sine);
 int y2 = (int)(bm.bmHeight * cosine - bm.bmWidth * sine);
 int x3 = (int)(bm.bmWidth * cosine);
 int y3 = (int)(-bm.bmWidth * sine);

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

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

 // Create a bitmap to hold the result
 HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), w, h);

 HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );
 HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );

 // Draw the background color before we change mapping mode
 HBRUSH hbrBack = CreateSolidBrush( clrBack );
 HBRUSH hbrOld = (HBRUSH)::SelectObject( destDC.m_hDC, hbrBack );
 destDC.PatBlt( 0, 0, w, h, PATCOPY );
 ::DeleteObject( ::SelectObject( destDC.m_hDC, hbrOld ) );

 // We will use world transform to rotate the bitmap
 SetGraphicsMode(destDC.m_hDC, GM_ADVANCED);
 XFORM xform;
 xform.eM11 = cosine;
 xform.eM12 = -sine;
 xform.eM21 = sine;
 xform.eM22 = cosine;
 xform.eDx = (float)-minx;
 xform.eDy = (float)-miny;

 SetWorldTransform( destDC.m_hDC, &xform );

 // Now do the actual rotating - a pixel at a time
 destDC.BitBlt(0,0,bm.bmWidth, bm.bmHeight, &sourceDC, 0, 0, SRCCOPY );

 // Restore DCs
 ::SelectObject( sourceDC.m_hDC, hbmOldSource );
 ::SelectObject( destDC.m_hDC, hbmOldDest );

 return hbmResult;
}

函数二: GetRotatedBitmap()使用 GetPixel & SetPixel
// GetRotatedBitmap - Create a new bitmap with rotated image
// Returns  - Returns new bitmap with rotated image
// hBitmap  - Bitmap to rotate
// radians  - Angle of rotation in radians
// clrBack  - Color of pixels in the resulting bitmap that do
//     not get covered by source pixels
// Note   - If the bitmap uses colors not in the system palette
//     then the result is unexpected. You can fix this by
//     adding an argument for the logical palette.
HBITMAP GetRotatedBitmap( HBITMAP hBitmap, float radians, COLORREF clrBack )
{
 // Create a memory DC compatible with the display
 CDC sourceDC, destDC;
 sourceDC.CreateCompatibleDC( NULL );
 destDC.CreateCompatibleDC( NULL );

 // Get logical coordinates
 BITMAP bm;
 ::GetObject( hBitmap, sizeof( bm ), &bm );

 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)(-bm.bmHeight * sine);
 int y1 = (int)(bm.bmHeight * cosine);
 int x2 = (int)(bm.bmWidth * cosine - bm.bmHeight * sine);
 int y2 = (int)(bm.bmHeight * cosine + bm.bmWidth * sine);
 int x3 = (int)(bm.bmWidth * cosine);
 int y3 = (int)(bm.bmWidth * 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 bitmap to hold the result
 HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), w, h);

 HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );
 HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );

 // Draw the background color before we change mapping mode
 HBRUSH hbrBack = CreateSolidBrush( clrBack );
 HBRUSH hbrOld = (HBRUSH)::SelectObject( destDC.m_hDC, hbrBack );
 destDC.PatBlt( 0, 0, w, h, PATCOPY );
 ::DeleteObject( ::SelectObject( destDC.m_hDC, hbrOld ) );

 // Set mapping mode so that +ve y axis is upwords
 sourceDC.SetMapMode(MM_ISOTROPIC);
 sourceDC.SetWindowExt(1,1);
 sourceDC.SetViewportExt(1,-1);
 sourceDC.SetViewportOrg(0, bm.bmHeight-1);

 destDC.SetMapMode(MM_ISOTROPIC);
 destDC.SetWindowExt(1,1);
 destDC.SetViewportExt(1,-1);
 destDC.SetWindowOrg(minx, maxy);

 // Now do the actual rotating - a pixel at a time
 // Computing the destination point for each source point
 // will leave a few pixels that do not get covered
 // So we use a reverse transform - e.i. compute the source point
 // for each destination point

 for( int y = miny; y < maxy; y++ )
 {
  for( int x = minx; x < maxx; x++ )
  {
   int sourcex = (int)(x*cosine + y*sine);
   int sourcey = (int)(y*cosine - x*sine);
   if( sourcex >= 0 && sourcex < bm.bmWidth && sourcey >= 0
     && sourcey < bm.bmHeight )
    destDC.SetPixel(x,y,sourceDC.GetPixel(sourcex,sourcey));
  }
 }

 // Restore DCs
 ::SelectObject( sourceDC.m_hDC, hbmOldSource );
 ::SelectObject( destDC.m_hDC, hbmOldDest );

 return hbmResult;
}

函数三: GetRotatedBitmap()使用DIB

          // GetRotatedBitmap - Create a new bitmap with rotated image
          // Returns - Returns new bitmap with rotated image
          // hDIB - Device-independent bitmap to rotate
          // radians - Angle of rotation in radians
          // clrBack - Color of pixels in the resulting bitmap that do
          //   not get covered by source pixels
          HANDLE GetRotatedBitmap( HANDLE hDIB, float radians, COLORREF clrBack)
          {
          // Get source bitmap info
          BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
          int bpp = bmInfo.bmiHeader.biBitCount; // Bits per pixel

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

          // Make sure height is positive and biCompression is BI_RGB or
          BI_BITFIELDS
          DWORD &compression = bmInfo.bmiHeader.biCompression;
          if( nHeight < 0 || (compression!=BI_RGB &&
          compression!=BI_BITFIELDS))
          return NULL;

          LPVOID lpDIBBits;
          if( bmInfo.bmiHeader.biBitCount > 8 )
          lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
          bmInfo.bmiHeader.biClrUsed) +
          ((compression == BI_BITFIELDS) ? 3 : 0));
          else
          lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);

             
          // 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)hDIB) ;
          HANDLE hDIBResult = GlobalAlloc(GMEM_FIXED,len+nHeaderSize);
          // Initialize the header information
          memcpy( (void*)hDIBResult, (void*)hDIB, nHeaderSize);
          BITMAPINFO &bmInfoResult = *(LPBITMAPINFO)hDIBResult ;
          bmInfoResult.bmiHeader.biWidth = w;
          bmInfoResult.bmiHeader.biHeight = h;
          bmInfoResult.bmiHeader.biSizeImage = len;

          LPVOID lpDIBBitsResult = (LPVOID)((LPBYTE)hDIBResult + nHeaderSize);

          // 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( bmInfo.bmiColors[i].rgbBlue ==  GetBValue(clrBack)
          && bmInfo.bmiColors[i].rgbGreen ==  GetGValue(clrBack)
          && bmInfo.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*)bmInfo.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;
          }

          // Now do the actual rotating - a pixel at a time
          // Computing the destination point for each source point
          // will leave a few pixels that do not get covered
          // So we use a reverse transform - e.i. compute the source point
          // for each destination point

          for( int y = 0; y < h; y++ )
          {
          for( int x = 0; x < w; x++ )
          {
          int sourcex = (int)((x+minx)*cosine + (y+miny)*sine);
          int sourcey = (int)((y+miny)*cosine - (x+minx)*sine);
          if( sourcex >= 0 && sourcex < nWidth && sourcey
          >= 0
          && sourcey < nHeight )
          {
          // Set the destination pixel
          switch(bpp)
          {
          BYTE mask;
          case 1: //Monochrome
          mask = *((LPBYTE)lpDIBBits + nRowBytes*sourcey +
          sourcex/8) & (0x80 >> sourcex%8);
          //Adjust mask for destination bitmap
          mask = mask ? (0x80 >> x%8) : 0;
          *((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
          (x/8)) &= ~(0x80 >> x%8);
          *((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
          (x/8)) |= mask;
          break;
          case 4:
          mask = *((LPBYTE)lpDIBBits + nRowBytes*sourcey +
          sourcex/2) & ((sourcex&1) ? 0x0f : 0xf0);
          //Adjust mask for destination bitmap
          if( (sourcex&1) != (x&1) )
          mask = (mask&0xf0) ? (mask>>4) : (mask<<4);
          *((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
          (x/2)) &= ~((x&1) ? 0x0f : 0xf0);
          *((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
          (x/2)) |= mask;
          break;
          case 8:
          BYTE pixel ;
          pixel = *((LPBYTE)lpDIBBits + nRowBytes*sourcey +
          sourcex);
          *((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
          (x)) = pixel;
          break;
          case 16:
          DWORD dwPixel;
          dwPixel = *((LPWORD)((LPBYTE)lpDIBBits +
          nRowBytes*sourcey + sourcex*2));
          *((LPWORD)((LPBYTE)lpDIBBitsResult +
          nResultRowBytes*y + x*2)) = (WORD)dwPixel;
          break;
          case 24:
          dwPixel = *((LPDWORD)((LPBYTE)lpDIBBits +
          nRowBytes*sourcey + sourcex*3)) & 0xffffff;
          *((LPDWORD)((LPBYTE)lpDIBBitsResult +
          nResultRowBytes*y + x*3)) |= dwPixel;
          break;
          case 32:
          dwPixel = *((LPDWORD)((LPBYTE)lpDIBBits +
          nRowBytes*sourcey + sourcex*4));
          *((LPDWORD)((LPBYTE)lpDIBBitsResult +
          nResultRowBytes*y + x*4)) = dwPixel;
          }
          }
          else
          {
          // Draw the background "color."
          tppabs="http://www.codeguru.com/bitmap/color." The
          background color
          // has already been drawn for 8 bits per pixel and less
          switch(bpp)
          {
          case 16:
          *((LPWORD)((LPBYTE)lpDIBBitsResult +
          nResultRowBytes*y + x*2)) =
          (WORD)dwBackColor;
          break;
          case 24:
          *((LPDWORD)((LPBYTE)lpDIBBitsResult +
          nResultRowBytes*y + x*3)) |= dwBackColor;
          break;
          case 32:
          *((LPDWORD)((LPBYTE)lpDIBBitsResult +
          nResultRowBytes*y + x*4)) = dwBackColor;
          break;
          }
          }
          }
          }

          return hDIBResult;
          }
分享到:
评论

相关推荐

    在vc++中实现图片的旋转和缩放

    在VC++中实现图片的旋转和缩放是一项常见的图像处理任务,涉及到计算机图形学和图像处理的知识。在这个过程中,我们需要理解位图(BMP)文件的结构,以及如何使用VC++的图形库来操作这些图像。 首先,位图(BMP)...

    VC图像旋转程序

    VC程序, 通过运算公式,将图像旋转一定角度,可实现图像的旋转功能,并最后保存出图片来

    VC图片处理 VC图片处理

    以下是对VC图片处理的一些关键知识点的详细解释: 1. **Windows API 图片处理**: - **BITMAPINFOHEADER** 结构:用于描述位图信息,包括宽度、高度、颜色深度等。 - **LoadImage()** 函数:可以加载BMP、ICO、...

    VB调用VC编写的DLL 实现图片360旋转源码.rar

    VB通过调用DLL实现图片实时旋转,可360度旋转,掩码色改变,甚至可以调整图片透明度、亮度、色相、饱和度、大小缩放等图片常用的值。程序很好的解决了GetDC引起内存泄露问题,兼容Win98至Win7环境。Dll文件由VC生成...

    vc实现BMP图片旋转90度

    在VC++环境中,实现BMP图像的90度旋转是一项常见的图像处理任务。BMP(Bitmap)是一种未经压缩的图像文件格式,广泛用于Windows操作系统。本文将深入探讨如何使用C++编程语言,尤其是Visual C++ (VC++) 工具来完成这...

    bitmap_rotate.rar_VC6 图片旋转_图片 旋转_图片旋转

    在本压缩包“bitmap_rotate.rar”中,我们有一个在Visual C++ 6(简称VC6)环境下实现的高质量图片旋转程序。这个实例旨在帮助开发者理解如何在C++中处理图像旋转,尤其适用于需要进行图像处理的项目。让我们深入...

    VC中使用GDI+库,实现图片旋转

    在本案例中,我们将探讨如何利用GDI+在VC++项目中实现图片的旋转,同时解决屏幕闪烁问题,以提高用户体验。 首先,你需要确保已经安装了Visual Studio,并且在项目中包含了GDI+库。在VC++项目中,这通常通过在工程...

    VC 加载图片显示

    在这个"VC 加载图片显示"的程序中,我们将探讨如何使用VC++来实现这一功能。 首先,我们需要理解基本的图像处理库。在VC++中,最常用的库之一是Windows GDI(Graphics Device Interface),这是一个用于创建和操纵...

    VC BMP位图图像旋转实例.rar

    【描述】"VC BMP位图图像旋转实例,本程序只支持256色的图片旋转实例,真彩色位图暂不支持。":这句描述提示我们,这个示例代码专门处理256色(8位)的BMP图像,并且不支持具有更多色彩(如24位或32位真彩色)的图像...

    vc实现三维图形的旋转

    在VC++环境中,实现三维图形的旋转涉及到计算机图形学的基本概念和MFC(Microsoft Foundation Classes)框架的应用。这里,我们通过一个具体的例子来探讨如何在VC++中创建一个旋转的立方体。 首先,我们需要理解...

    VC 图片特效播放程序

    【VC 图片特效播放程序】是一款使用VC6.0编程环境开发的图像处理软件,它具备展示图片并实现各种特效播放的功能。这个程序的核心在于它能够以独特的方式展示静态图片,通过按下F2键,用户可以轻松切换不同的特效,为...

    使用VC读取图片代码

    - `cv::Mat`对象可以直接进行图像处理操作,如缩放、旋转、颜色转换等。 例如,使用GDI读取BMP图片的简单代码如下: ```cpp #include void LoadBMP(const char* filePath) { HBITMAP hBitmap = (HBITMAP)...

    VC6.0环境实现图片播放的特效

    在本文中,我们将深入探讨如何在Visual C++ 6.0(简称VC6.0)环境中实现图片播放的特效。这对于初学者来说是一次宝贵的学习机会,能够帮助他们理解图形处理的基本原理,并掌握如何在MFC(Microsoft Foundation ...

    VC6 Picture 控件显示指定图片

    同时,通过覆写OnPaint方法,可以实现更复杂的图片处理,如缩放、裁剪、旋转等。 在VC6中,Picture控件虽然功能相对简单,但足以满足大部分基本的图像显示需求。对于更复杂的需求,如动画或高级图形处理,可能需要...

    VB通过调用VC生成的DLL实现图片360旋转

    本话题主要探讨如何使用VB调用VC生成的动态链接库(DLL)来实现图片360度旋转的功能。 DLL是一种可执行文件格式,它包含可被其他程序调用的函数和资源。通过创建DLL,开发者可以将代码模块化,提高代码重用性,减少...

    VC 图片封装类.zip

    "VC 图片封装 类 C++"这个主题涉及到如何在C++中创建一个类来处理图片,包括加载、显示、保存以及可能的转换和其他图像处理功能。下面我们将深入探讨这一话题。 首先,C++中的图像封装通常会涉及到以下几个关键知识...

    VC.DLL.image.360.rotation.rar_dll图片_vc 图片 旋转

    本话题主要关注如何使用VC++(Visual C++)来创建一个DLL,该DLL实现了图片的360度旋转功能。这是一个实用的编程实践,尤其在图像处理和多媒体应用中常见。 首先,我们需要了解VC++中的DLL项目设置。在Visual ...

    vc 动态加载显示jpg、bmp图片

    在VC++编程环境中,动态加载并...此外,对于更复杂的应用场景,如图片的缩放、旋转、裁剪等,可能需要使用GDI+或者Direct2D等更强大的图像处理库。而IPicture主要适用于简单的图片加载和显示,不涉及复杂的图像操作。

    VC6.0+GDI实现图片查看器

    通过理解GDI的绘图原理和VC6.0的工程结构,开发者可以进一步扩展这个图片查看器,增加如缩放、旋转、保存等功能,以满足更复杂的需求。同时,这也为学习Windows程序设计和图形处理提供了良好的实践平台。

    vc视频音频播放器及图片浏览

    【标题】"vc视频音频播放器及图片浏览"是一个基于Microsoft Foundation Class (MFC)库开发的多媒体应用程序,它集成了音频、视频播放以及图片查看的功能。MFC是微软为Windows平台提供的一种C++类库,它使得开发者...

Global site tag (gtag.js) - Google Analytics