Win32应用程序正在获取要更改为完全蓝色、红色或绿色的像素
Win32 Application getting pixels to change to full blue,red or green color
各位C++专家,
又是我。我将开门见山。
我已经成功地得到了位图图像rgb彩色像素,它是(蓝色-178,绿色-130和红色131)。
我接下来想做的是循环像素,使其成为完全蓝色的图片。(例如蓝色-255,绿色-0,红色-0)
我确实尝试了几个for循环,但不起作用,因此需要帮助!
/*for (int i = 0; i < test; i++)
{
image[i].rgbtBlue;
image[i].rgbtGreen;
image[i].rgbtRed;
}*/
但很明显它不起作用,这就是我需要帮助的原因。补充一点,hfile被初始化用于其他目的,所以我认为它不相关。感谢所有的评论,谢谢。
谢谢!下面是代码。
p.S:使用Microsoft Visual Studio 2010-Win32应用程序,而不是控制台。
#include "stdafx.h"
#include "winmain.h"
#include "Resource.h"
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <CommDlg.h>
#include <stdint.h>
#include <string>
#include <WinGDI.h>
HANDLE hfile2;
DWORD written;
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
RGBTRIPLE *image;
hfile2 = CreateFile(ofn.lpstrFile`, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_OVERLAPPED, NULL);
//Read the header
ReadFile(hfile, &bfh, sizeof(bfh), &written, NULL);
ReadFile(hfile, &bih, sizeof(bih), &written, NULL);
// Read image
int imagesize = bih.biWidth * bih.biHeight; // Helps you allocate memory for the image
image = new RGBTRIPLE[imagesize]; // Create a new image (I'm creating an array during runtime
ReadFile(hfile, image, imagesize * sizeof(RGBTRIPLE), &written, NULL); // Reads it off the disk
get_pixel(bih.biWidth, bih.biHeight);
int test = bih.biHeight * bih.biWidth * bih.biBitCount;
RGBTRIPLE get_pixel(int x,int y)
{
// Image define from earlier
return image[(bih.biHeight-1-y)*bih.biWidth+x];
}
您的注释非常正确。每个元素image[i]
都是一个RGBTRIPLE
变量,您可以读取和写入它。因此,setpixel
很简单:
void set_pixel(int x,int y, RGBTRIPLE color)
{
image[(bih.biHeight-1-y)*bih.biWidth+x] = color;
}
您可以看到,相同的代码用于选择图像的右侧像素。您可能想要定义一个const RGBTRIPLE blue;
常量。
我假设文件是.BMP
。除了像素数据外,它还有需要读取的标题,还需要注意像素线填充到32bti边界。你需要考虑到这一切。在线查找示例代码/片段,例如:
- 读取BMP文件
- 在windows应用程序中读写BMP
相关文章:
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 无法输出对象的红色/绿色/蓝色值
- 计数用红色着色m灰色块的方法,其中红色块应以计数至少为n的组进行
- 如何通过组合红色和蓝色无符号字节来优化混合?
- glTexImage2D 不起作用,但将 FragColor 设置为 vec4(1.0, 0, 0, 1.0) 会给我红色图像
- WICConvertBitmapSource + CopyPixel结果为蓝色图像
- 如何将控制台输出的文本颜色更改为红色,但如果从Powershell或CMD运行,背景将保持相同的颜色
- 使用 C++ 库 GLEW 在 openGl 中绘制一个红色三角形
- 红色伪像通过GSTREAMER和QT5可视化RTSP流
- 用高斯Seidel红色黑色求解1D泊松方程
- D3D11:渲染(深度)到纹理会导致红色方块,正常渲染有效
- 用SDL2和OpenGL显示蓝色
- OPENCV:从RGB转换为灰度只有蓝色
- 如何实现std :: set(红色/黑色树)前迭代
- 在OpenGL中打印一个红色三角形
- 如何使用 glDrawPixel 时更改像素的颜色,我的像素始终为红色
- 红色药丸检测虚拟化
- 红色黑树插入代码显示分割故障11
- Win32应用程序正在获取要更改为完全蓝色、红色或绿色的像素
- 用于c++的Colormap库,它将给定的值转换为红色,绿色和蓝色值