【问题标题】:Win32 Application getting pixels to change to full blue,red or green colorWin32 应用程序获取像素以更改为全蓝色、红色或绿色
【发布时间】: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


【解决方案1】:

您的评论非常正确。每个元素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; 常量。

【讨论】:

    【解决方案2】:

    我假设文件是​​.BMP。除了像素数据,它还有你需要阅读的标题,还要注意像素线被填充到 32-bti 边界。你需要把这一切都考虑进去。在线查找示例代码/sn-ps,例如:

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 2017-07-09
      • 2011-10-28
      • 2019-02-07
      • 2014-08-26
      • 1970-01-01
      相关资源
      最近更新 更多