Win32应用程序正在获取要更改为完全蓝色、红色或绿色的像素

Win32 Application getting pixels to change to full blue,red or green color

本文关键字:红色 蓝色 像素 应用程序 获取 Win32      更新时间:2023-10-16

各位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