2012-01-12 88 views
0

你好所有的C++专家,Win32应用程序将像素变成全蓝,红或绿色

这是我再次。我会直指点。

我已经成功地获得了位图图像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]; 
} 
+0

请把参数之间的空格功能和二进制运算符,如'*'。它使一切方式更容易阅读。 – 2012-01-12 05:02:21

+0

其中是bfh,bih,hfile等的类型声明?你初始化hfile2但不是hfile?你尝试过的代码在哪里?当你尝试过时发生了什么? – fileoffset 2012-01-12 05:08:43

+0

编辑了代码并添加了我的评论,非常感谢! – Newbie 2012-01-12 05:24:18

回答

0

您的评论是非常正确的。每个元素image[i] a RGBTRIPLE变量,您可以读取和写入。因此,setpixel很简单:

void set_pixel(int x,int y, RGBTRIPLE color) 
{ 
    image[(bih.biHeight-1-y)*bih.biWidth+x] = color; 
} 

你看,同样的代码被用于选择图像的右侧像素。你可能想要定义一个const RGBTRIPLE blue;常量。

相关问题