创建纯色bitmap和替换bitmap颜色

2024-03-04 17:58

本文主要是介绍创建纯色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颜色的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/773982

相关文章

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

C#高效实现Word文档内容查找与替换的6种方法

《C#高效实现Word文档内容查找与替换的6种方法》在日常文档处理工作中,尤其是面对大型Word文档时,手动查找、替换文本往往既耗时又容易出错,本文整理了C#查找与替换Word内容的6种方法,大家可以... 目录环境准备方法一:查找文本并替换为新文本方法二:使用正则表达式查找并替换文本方法三:将文本替换为图

Python批量替换多个Word文档的多个关键字的方法

《Python批量替换多个Word文档的多个关键字的方法》有时,我们手头上有多个Excel或者Word文件,但是领导突然要求对某几个术语进行批量的修改,你是不是有要崩溃的感觉,所以本文给大家介绍了Py... 目录工具准备先梳理一下思路神奇代码来啦!代码详解激动人心的测试结语嘿,各位小伙伴们,大家好!有没有想

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注

MySQL的触发器全解析(创建、查看触发器)

《MySQL的触发器全解析(创建、查看触发器)》MySQL触发器是与表关联的存储程序,当INSERT/UPDATE/DELETE事件发生时自动执行,用于维护数据一致性、日志记录和校验,优点包括自动执行... 目录触发器的概念:创建触www.chinasem.cn发器:查看触发器:查看当前数据库的所有触发器的定

创建springBoot模块没有目录结构的解决方案

《创建springBoot模块没有目录结构的解决方案》2023版IntelliJIDEA创建模块时可能出现目录结构识别错误,导致文件显示异常,解决方法为选择模块后点击确认,重新校准项目结构设置,确保源... 目录创建spChina编程ringBoot模块没有目录结构解决方案总结创建springBoot模块没有目录

linux批量替换文件内容的实现方式

《linux批量替换文件内容的实现方式》本文总结了Linux中批量替换文件内容的几种方法,包括使用sed替换文件夹内所有文件、单个文件内容及逐行字符串,强调使用反引号和绝对路径,并分享个人经验供参考... 目录一、linux批量替换文件内容 二、替换文件内所有匹配的字符串 三、替换每一行中全部str1为st