博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Invert (mirror) a bitmap
阅读量:4357 次
发布时间:2019-06-07

本文共 4253 字,大约阅读时间需要 14 分钟。

Rotate a bitmap image

Invert (mirror) a bitmap

Posted by Zafir Anjum on August 5th, 1998

We can invert an image either laterally or vertically. The effect of the inversions is shown in the image below. The image on the top left is the normal image, the image on the top right is laterally inverted, the image on the bottom left is vertically inverted and the image on the bottom right has been inverted twice - once laterally and once vertically. The effect of applying both kinds of inversion is that the image gets rotated by 180 degrees.

Two functions have been listed below, one of them will draw the image onto a device context after inverting it and the other will create a new bitmap with the inverted image.

Function 1: DrawInvertedBitmap

The DrawInvertedBitmap() function draws an inverted image of a bitmap at the specified location. It uses StretchBlt() to do the actual inversion. When the signs of the source width and the destination width are different then the image is inverted along the x-axis. When the signs of the source height and the destination height are different then the image is inverted along the y-axis.

// DrawInvertedBitmap    - Draws the bitmap after inverting it// hBimtap        - Bitmap handle// hPal            - Palette to use when drawing the bitmap// bLateral        - Flag to indicate whether to invert laterally or vertically// xDest        - X coordinate of top left corner to draw at// yDest        - Y coordinate of top left corner to draw atvoid DrawInvertedBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, BOOL bLateral,                             int xDest, int yDest ){    // Create a memory DC compatible with the destination DC    CDC memDC;    memDC.CreateCompatibleDC( pDC );     // Get logical coordinates    BITMAP bm;    ::GetObject( hBitmap, sizeof( bm ), &bm );     //memDC.SelectObject( &bitmap );    HBITMAP hBmOld = (HBITMAP)::SelectObject( memDC.m_hDC, hBitmap );            // Select and realize the palette    if( hPal && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)    {        SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );        pDC->RealizePalette();    }    if( bLateral )        pDC->StretchBlt( xDest, yDest, bm.bmWidth, bm.bmHeight, &memDC,                 bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );    else        pDC->StretchBlt( xDest, yDest, bm.bmWidth, bm.bmHeight, &memDC,                 0, bm.bmHeight-1, bm.bmWidth, -bm.bmHeight, SRCCOPY );     // Restore the old bitmap    ::SelectObject( memDC.m_hDC, hBmOld );}

Function 2: GetInvertedBitmap

The GetInvertedBitmap() function creates a new bitmap to store the inverted image. This function too uses the StretchBlt() function for the actual inversion. In this case the destination device context is another memory device context so that the image is rendered into a bitmap.

// GetInvertedBitmap    - Creates a new bitmap with the inverted image// Returns        - Handle to a new bitmap with inverted image// hBitmap        - Bitmap to invert// bLateral        - Flag to indicate whether to invert laterally or verticallyHBITMAP GetInvertedBitmap( HBITMAP hBitmap, BOOL bLateral ){    // 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 );     // Create a bitmap to hold the result    HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL),                         bm.bmWidth, bm.bmHeight);     // Select bitmaps into the DCs    HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );    HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );        if( bLateral )        destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &sourceDC,                 bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );    else        destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &sourceDC,                 0, bm.bmHeight-1, bm.bmWidth, -bm.bmHeight, SRCCOPY );     // Reselect the old bitmaps    ::SelectObject( sourceDC.m_hDC, hbmOldSource );    ::SelectObject( destDC.m_hDC, hbmOldDest );     return hbmResult;}

 

 

转载于:https://www.cnblogs.com/shangdawei/archive/2013/05/10/3071062.html

你可能感兴趣的文章
linux如何查看jmp esp地址,Linux pwn从入门到熟练(二)
查看>>
自己制作Linux镜像,CentOS 6.5 Docker自制CentOS镜像
查看>>
linux配置scp交互传输,Linux间传输文件的几种方法scp、sftp
查看>>
linux安装nginx映射目录,centos8自定义目录安装nginx(教程详解)
查看>>
linux cpu scheduler,A Temporal Partition-Based Linux CPU Scheduler
查看>>
c语言怎么写最小公倍数的函数,C语言 · 最小公倍数
查看>>
c语言中的头文件string.h的作用,C语言常用头文件及库函数——string.h
查看>>
c语言字符-1代表什么,玩儿转C语言:符号和字符(1)
查看>>
知道商洛学院c语言章节答案,C语言程序设计(商洛学院)知到章节答案
查看>>
c语言酒精检测仪程序代码,单片机酒精浓度测试仪,代码,原理图
查看>>
单路电压表c语言编程,单片机数字电压表的设计
查看>>
精通c语言的标准,《精通Unix下C语言编程与项目实践》之七——标准I/O重定向
查看>>
蓝桥杯c语言试题 高职,2011l蓝桥杯c语言高职真题附加答案2.doc
查看>>
c 语言 设计算法 例题,C语言程序设计例题.pdf
查看>>
p4 linux 命令行,《linux就该这么学》学习笔记25期2020-P4
查看>>
android9.0官方下载,安卓9.0系统刷机包 官方正式版
查看>>
android 模块封装,Android开发之针对联系人的封装
查看>>
android 构建期间错误,Android构建错误
查看>>
Key(Windows Android),Windows环境 Android Studio 生成SSH key
查看>>
android new date参数,Android向Rest服务Post数据遇到的Date类型数据问题
查看>>