【发布时间】:2012-01-12 04:43:02
【问题描述】:
您好所有 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 被初始化用于其他目的,所以我认为它不相关。感谢所有的cmets,谢谢。
谢谢!如下是代码。
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];
}
【问题讨论】:
-
请在函数和二元运算符(例如
*)的参数之间放置空格。它使所有内容都更易于阅读。 -
bfh、bih、hfile 等的类型声明在哪里?您初始化 hfile2 而不是 hfile?您尝试过的代码在哪里,尝试时发生了什么?
-
编辑代码并添加到我的 cmets 中,感谢!
-
请编辑和更新您的问题,不要将代码放入 cmets。另外,确保所有相关的函数定义都存在,例如 get_pixel 是什么?
-
已更改并更新我的问题,谢谢!
标签: c++ file winapi image-processing bitmap