There are a lot of information about bitmap rotation, but it is very clear (I didn't look carefully). So I also wrote one, I hope to give me a little help to me.
The first step, you must know the structure of the file of the BMP format.
The bitmap (BMP) file consists of several parts:
1.BitmapfileHeader, its definition is as follows:
Typedef struct tagbitmapfileHeader {Word bftype; // must be 'bm' dword bfsize; // file size word bfreserved1; // must be 0 word bfreserved2; // must be 0 dword bfoffbits; // From ITMAPFileHeader to the bias of BMP data Shift
} BitmapfileHeader, * PbitmapfileHeader;
2.BitmapInfoHeader, its definition is as follows:
Typedef struct tagbitmapinfoheader {dword bisize; // This structure can be used to get long biwidth; // bitmap width, in pixels in line biheight; // bitmap height, in pixels Word Biplanes ; // must be 1 Word Bibitcount; // bit image pixel bit number, can be 0, 1, 4, 8, 24, 32 DWORD BICOMPRESSION; DWORD BISIZEIMAGE; / / (for compression only) Long Bixpelspermeter; // One-meter lateral pixels long biypelspermeter; // one meter portrait pixel DWORD biclrused; // (non-zero words short color table) DWORD BICLRIMPORTANT;} BitmapInfoHeader, * PbitmapInfoHeader;
Since the above information can be found directly from the MSDN, just simply introduce, you can view the NSDN help yourself, which has a very detailed introduction.
3. DIB bit image. It is placed here that real bitmap data.
Know the storage format of bitmaps, let's make it easy to read it like memory.
Step 2, read into the BMP image
LPCTSTR LPSZFILENAME4 = "Untitled.bmp"; // File path cfile file; // Used to read BMP file BitmapFileHeader bfhheader; // BMP file header
BitMapInfoHeader Bmiheader; // BMP format header
LPbitMapInfo lpbitmapinfo; // BMP format specific information int bmpWidth = 0; // Picture width int bmpheight = 0; // Picture Height IF (! File.open (lpsz (lpsz (lpsz (lpszead) Return; // Open file file. Read (& BFHHEADER, SIZEOF (BitmapFileHeader); // Read file header if (bfhheader.bfType! = ((Word) ('M' << 8) | 'b') // Decision is "BM" Return ; If (bfhheader.bfsize! = File.getlength ()) Return; IF (File.Read ((LPSTR) & bmiheader, sizeof (bmiheader)) Return; bmpheight = bmiheader.biheight; // get The height and width bmpWidth = bmiHeader.biWidth; file.SeekToBegin (); file.Read (& bfhHeader, sizeof (BITMAPFILEHEADER)); UINT uBmpInfoLen = (UINT) bfhHeader.bfOffBits-sizeof (BITMAPFILEHEADER); lpBitmapInfo = (lPBITMAPINFO) new BYTE [ Ubmpinfolen]; file.read ((lpvoid) lpbitmapinfo, ubmpinfolen; if (* (lpbitmapinfo))! = sizeof (BitmapInfoHeader)) Return; dword dwbitlen = bfhheader.bfSize - bfhheader.bfoffbits; lpvoid lpsrcbits = new byte [dwbitlen]; // read data into LPSRCBITS array file.readhuge (lpsrcbits, dwbitlen); file.close (); // Close file
Let's display the picture on the screen:
Step 3, display pictures
CClientDC HDC (this); Stretchdibits (HDC, 0, 0, Bmpwidth, Bmpheight, 0, 0, BmpWidth, Bmpheight,
LPSRCBITS, LPBITMAPINFO, DIB_RGB_COLORS, SRCCOPY;
In the fourth step, read pictures into the memory device environment
HDC dcSrc; HBITMAP bitmap; dcSrc = CreateCompatibleDC (hDC); // get a memory device context bitmap = CreateCompatibleBitmap (hDC, bmpWidth, bmpHeight); SelectObject (dcSrc, bitmap); BitBlt (dcSrc, 0,0, bmpWidth, bmpHeight, HDC, 0, 0, SRCCOPY; // This step is important to fifth, realize the bitmap
We assume that the function of the rotating bit map is as follows:
Void RotateBitmap (HDC DCSRC, Int SrcWidth, Int Srcheight, Double Angle, HDC PDC);
/ * Interpretation is as follows: /
HDC DCSRC: The memory device environment of the bitmap to rotate is created in the fourth step.
INT SRCWIDTH: Wandering to Rotate the Width
INT SRCHEIGHT: To rotate the height of the bitmap
Double Angle: The angle to rotate, in an arc
HDC PDC: The current screen device environment obtained by the third step
* ///
/ / The following is the function implementation details
void RotateAnyAngle (HDC dcSrc, int SrcWidth, int SrcHeight, double angle) {double x1, x2, x3; double y1, y2, y3; double maxWidth, maxHeight, minWidth, minHeight; double srcX, srcY; double sinA, cosA; double DSTWIDTH; Double DStheight; HDC DCDST; // Rotate Memory Device Environment HBitMap NewbitMap; SINA = SIN (Angle); x1 = COS (Angle); x1 = -srcheight * sina; y1 = srcHeight * Cosa; x2 = srcwidth * COSA - SRCHEIGHT * COSA; Y2 = srcHeight * COSA SrcWidth * sina; x3 = srcwidth * cosa; y3 = srcwidth * sina; minWidth = x3> (x1> x2? x2: x1)? (x1> x2? x2: x1 : x3; minWidth = minWidth> 0? 0: minWidth; minheight = Y3> (Y1> Y2? Y2: Y1): Y3; minheight = minheight> 0? 0: minheight; MaxWidth = x3> (x1> x2? x1: x2)? x3: (x1> x2? x1: x2); maxwidth = maxwidth> 0? MaxWidth: 0; maxheight = Y3> (Y1> Y2? Y1: Y2)? y3: (? y1> y2 y1: y2); maxHeight = maxHeight> 0 maxHeight:? 0; DstWidth = maxWidth - minWidth; DstHeight = maxHeight - minHeight; dcDst = CreateCompatibleDC (dcSrc); newBitmap = CreateCompatibleBitmap (dcSrc, (int) Dstwidth, (int) dstheight); SELE CTOBJECT (DCDST, NewbitMap); for (int i = 0; i
Bitblt (HDC, 200, 200, (INT) Dstwidth, (int) DStheight, DCDST, 0, 0, SRCCopy;
DeleteObject (newbitmap);
Deletedc (DCDST);
}
Finally, we can call:
Double Angle = (45 / 180.0) * 3.14159; // Rotate 45deGree, can be arbitrary
Rotateanyangle (DCSRC, BMPWIDTH, BMPHEIGHT, ANE,);
I'm big here.