创建纯色bitmap和替换bitmap颜色
1、替换bitmap颜色
//-------------------------------------------------------------------------------
// ReplaceColor
//
// Author : Dimitri Rochette drochette@coldcat.fr
// Specials Thanks to Joe Woodbury for his comments and code corrections
//
// Includes : Only <windows.h>//
// hBmp : Source Bitmap
// cOldColor : Color to replace in hBmp
// cNewColor : Color used for replacement
// hBmpDC : DC of hBmp ( default NULL ) could be NULL if hBmp is not selected
//
// Retcode : HBITMAP of the modified bitmap or NULL for errors
//
//-------------------------------------------------------------------------------
HBITMAP ReplaceColor(HBITMAP hBmp, COLORREF cOldColor, COLORREF cNewColor, HDC hBmpDC)
{HBITMAP RetBmp = NULL;if (hBmp){HDC BufferDC = CreateCompatibleDC(NULL); // DC for Source Bitmapif (BufferDC){HBITMAP hTmpBitmap = (HBITMAP)NULL;if (hBmpDC)if (hBmp == (HBITMAP)GetCurrentObject(hBmpDC, OBJ_BITMAP)){hTmpBitmap = CreateBitmap(1, 1, 1, 1, NULL);SelectObject(hBmpDC, hTmpBitmap);}HGDIOBJ PreviousBufferObject = SelectObject(BufferDC, hBmp);// here BufferDC contains the bitmapHDC DirectDC = CreateCompatibleDC(NULL); // DC for workingif (DirectDC){// Get bitmap sizeBITMAP bm;GetObject(hBmp, sizeof(bm), &bm);// create a BITMAPINFO with minimal initilisation // for the CreateDIBSectionBITMAPINFO RGB32BitsBITMAPINFO;ZeroMemory(&RGB32BitsBITMAPINFO, sizeof(BITMAPINFO));RGB32BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);RGB32BitsBITMAPINFO.bmiHeader.biWidth = bm.bmWidth;RGB32BitsBITMAPINFO.bmiHeader.biHeight = bm.bmHeight;RGB32BitsBITMAPINFO.bmiHeader.biPlanes = 1;RGB32BitsBITMAPINFO.bmiHeader.biBitCount = 32;// pointer used for direct Bitmap pixels accessUINT * ptPixels;HBITMAP DirectBitmap = CreateDIBSection(DirectDC,(BITMAPINFO *)&RGB32BitsBITMAPINFO,DIB_RGB_COLORS,(void **)&ptPixels,NULL, 0);if (DirectBitmap){// here DirectBitmap!=NULL so ptPixels!=NULL no need to testHGDIOBJ PreviousObject = SelectObject(DirectDC, DirectBitmap);BitBlt(DirectDC, 0, 0,bm.bmWidth, bm.bmHeight,BufferDC, 0, 0, SRCCOPY);// here the DirectDC contains the bitmap// Convert COLORREF to RGB (Invert RED and BLUE)cOldColor = COLORREF2RGB(cOldColor);cNewColor = COLORREF2RGB(cNewColor);// After all the inits we can do the job : Replace Colorfor (int i = ((bm.bmWidth*bm.bmHeight) - 1); i >= 0; i--){if (ptPixels[i] == cOldColor) ptPixels[i] = cNewColor;}// little clean up// Don't delete the result of SelectObject because it's // our modified bitmap (DirectBitmap)SelectObject(DirectDC, PreviousObject);// finishRetBmp = DirectBitmap;}// clean upDeleteDC(DirectDC);}if (hTmpBitmap){SelectObject(hBmpDC, hBmp);DeleteObject(hTmpBitmap);}SelectObject(BufferDC, PreviousBufferObject);// BufferDC is now uselessDeleteDC(BufferDC);}}return RetBmp;
}
2、创建纯色bitmap
//cNewColor:指定颜色
//width:宽 height:高
HBITMAP CreatePureColorBitmap(COLORREF cNewColor, LONG width, LONG height)
{HBITMAP RetBmp = NULL;HDC DirectDC = CreateCompatibleDC(NULL); // DC for workingif (DirectDC){// create a BITMAPINFO with minimal initilisation // for the CreateDIBSectionBITMAPINFO RGB32BitsBITMAPINFO;ZeroMemory(&RGB32BitsBITMAPINFO, sizeof(BITMAPINFO));RGB32BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);RGB32BitsBITMAPINFO.bmiHeader.biWidth = width;RGB32BitsBITMAPINFO.bmiHeader.biHeight = height;RGB32BitsBITMAPINFO.bmiHeader.biPlanes = 1;RGB32BitsBITMAPINFO.bmiHeader.biBitCount = 32;// pointer used for direct Bitmap pixels accessUINT * ptPixels;HBITMAP DirectBitmap = CreateDIBSection(DirectDC,(BITMAPINFO *)&RGB32BitsBITMAPINFO,DIB_RGB_COLORS,(void **)&ptPixels,NULL, 0);if (DirectBitmap){// here DirectBitmap!=NULL so ptPixels!=NULL no need to testHGDIOBJ PreviousObject = SelectObject(DirectDC, DirectBitmap);cNewColor = COLORREF2RGB(cNewColor);// After all the inits we can do the job : Replace Colorfor (int i = ((width*height) - 1); i >= 0; i--){ptPixels[i] = cNewColor;}// little clean up// Don't delete the result of SelectObject because it's // our modified bitmap (DirectBitmap)SelectObject(DirectDC, PreviousObject);// finishRetBmp = DirectBitmap;}// clean upDeleteDC(DirectDC);}return RetBmp;
}
本文不算原创,只是参考了别人代码然后验证并修改为自己想要的功能。由于对Bitmap不熟悉,查找代码也耗费了不少时间,因此在这里做个记录。
原代码地址地址:https://www.codeproject.com/articles/2841/how-to-replace-a-color-in-a-hbitmap
创建纯色bitmap和替换bitmap颜色相关推荐
- android bitmap 替换指定颜色,Android 实现把bitmap图片的某一部分的颜色改成其他颜色...
把bitmap图片的某一部分的颜色改成其他颜色 private Bitmap ChangeBitmap(Bitmap bitmap){ int bitmap_h; int bitmap_w; int ...
- android bitmap 替换指定颜色,Android实现把bitmap图片的某一部分的颜色改成其他颜色的方法...
Android实现把bitmap图片的某一部分的颜色改成其他颜色的方法 发布时间:2020-07-29 14:11:15 来源:亿速云 阅读:107 作者:小猪 这篇文章主要讲解了Android实现把 ...
- 【Android自定义View】Bitmap的绘制和颜色滤镜
前言 Bitmap在开发中是经常遇到的,因为他用到的地方有很多,比如Android的图片预览,自定义相机,自定义美颜相机,图片滤镜,图像算法....,既然使用场景这么多,今天就先讲一下Android中 ...
- 【Android 内存优化】Bitmap 内存缓存 ( Bitmap 内存复用 | 弱引用 | 引用队列 | 针对不同 Android 版本开发不同的 Bitmap 复用策略 | 工具类代码 )
文章目录 一.Bitmap 复用池 二.弱引用 Bitmap 内存释放 三.从 Bitmap 复用池中获取对应可以被复用的 Bitmap 对象 1.Android 2.3.3(API 级别 10)及以 ...
- BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...
- canvas.drawBitmap()画出来的bitmap和原bitmap大小不同,有一部分缺失了
造成这个问题的原因就在于安卓系统会根据bitmap的density和当前运行设备的density进行比较,不同会进行缩放. 项目里的图片叫 ic_launcher.png,大小是72*72,只有一张, ...
- 如何给证件照替换背景颜色?一键替换证件照背景色的方法
证件照换背景的优点 在申请各种证件时,一张合格的证件照是必不可少的.然而,在拍摄证件照时,往往因为背景.光线等问题导致照片质量不佳.因此,将证件照的背景更换为统一的纯色背景就显得尤为重要. 证件照换背 ...
- R语言创建自定义颜色(分类变量与颜色形成稳定映射)实战:设置因子变量(分类变量)到可视化颜色的稳定映射
R语言创建自定义颜色(分类变量与颜色形成稳定映射)实战:设置因子变量(分类变量)到可视化颜色的稳定映射 目录
- java base64转bitmap,如何将Bitmap位图与base64字符串相互转换
先引用delphi自带的单元 uses EncdDecd; 然后就可以使用下面二个函数了: ///将Bitmap位图转化为base64字符串 function BitmapToString(img:T ...
最新文章
- 对人工智能的灵魂一问,它这样回答
- 这年头居然连MSDN Library都靠不住呀
- mac系统装mysql还是mariadb_Mac上安装mariadb
- SSM中怎样使用JUnit4+spring-test编写单元测试
- boost::dynamic_bitset模块实现ambiguous set的测试程序
- Java通过IText导出word和pdf
- Go出现警告struct doesn‘t have any exported fields, nor custom marshaling
- linux权限管理之用户和组管理
- [转]EXCEL截取字符串中某几位的函数——LeftMIDRight及Find函数的使用
- Python编程基础19:封装、继承与多态
- 精选CSDN的ACM-ICPC活跃博客
- 什么是「数独」,简单介绍
- 栅栏密码(Fence)——python解密
- thinkphp内核独立版商城-萤火微信小程序商城(YoShop)
- C语言程序设计基础(02)—— Visual Studio Code 软件安装与使用
- Scintilla开源库使用指南
- 90后绝对不是用来管的!
- python如何速成_怎样速成python?
- MATLAB图中图绘制(局部放大图)
- 自己动手写一个小型的TCP/IP协议