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

相关文章

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in

怎么用idea创建一个SpringBoot项目

《怎么用idea创建一个SpringBoot项目》本文介绍了在IDEA中创建SpringBoot项目的步骤,包括环境准备(JDK1.8+、Maven3.2.5+)、使用SpringInitializr... 目录如何在idea中创建一个SpringBoot项目环境准备1.1打开IDEA,点击New新建一个项

如何使用Maven创建web目录结构

《如何使用Maven创建web目录结构》:本文主要介绍如何使用Maven创建web目录结构的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录创建web工程第一步第二步第三步第四步第五步第六步第七步总结创建web工程第一步js通过Maven骨架创pytho

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间