创建纯色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

相关文章

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Linux创建服务使用systemctl管理详解

《Linux创建服务使用systemctl管理详解》文章指导在Linux中创建systemd服务,设置文件权限为所有者读写、其他只读,重新加载配置,启动服务并检查状态,确保服务正常运行,关键步骤包括权... 目录创建服务 /usr/lib/systemd/system/设置服务文件权限:所有者读写js,其他

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

Python进行word模板内容替换的实现示例

《Python进行word模板内容替换的实现示例》本文介绍了使用Python自动化处理Word模板文档的常用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录技术背景与需求场景核心工具库介绍1.获取你的word模板内容2.正常文本内容的替换3.表格内容的

MySQL批量替换数据库字符集的实用方法(附详细代码)

《MySQL批量替换数据库字符集的实用方法(附详细代码)》当需要修改数据库编码和字符集时,通常需要对其下属的所有表及表中所有字段进行修改,下面:本文主要介绍MySQL批量替换数据库字符集的实用方法... 目录前言为什么要批量修改字符集?整体脚本脚本逻辑解析1. 设置目标参数2. 生成修改表默认字符集的语句3

Git打标签从本地创建到远端推送的详细流程

《Git打标签从本地创建到远端推送的详细流程》在软件开发中,Git标签(Tag)是为发布版本、标记里程碑量身定制的“快照锚点”,它能永久记录项目历史中的关键节点,然而,仅创建本地标签往往不够,如何将其... 目录一、标签的两种“形态”二、本地创建与查看1. 打附注标http://www.chinasem.cn

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 及其衍生注解