C语言中,怎么读取一个灰度(仅含黑白二色)位图的各个像素灰度值矩阵,并进行替换,输出替换后灰度矩阵所表示的位图

Persistenter 2015-04-30 10:26:17
如题,望各位高手不吝赐教
...全文
575 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-04-30
  • 打赏
  • 举报
回复
再供参考: Bitmap::SetPixel Method -------------------------------------------------------------------------------- The SetPixel method sets the color of a specified pixel in this bitmap. Syntax Status SetPixel( INT x, INT y, const Color &color ); Parameters x [in] int that specifies the x-coordinate (column) of the pixel. y [in] int that specifies the y-coordinate (row) of the pixel. color [in] Reference to a Color object that specifies the color to set. Return Value If the method succeeds, it returns Ok, which is an element of the Status enumeration. If the method fails, it returns one of the other elements of the Status enumeration. Remarks Depending on the format of the bitmap, GetPixel might not return the same value as was set by SetPixel. For example, if you call SetPixel on a Bitmap object whose pixel format is 32bppPARGB, the RGB components are premultiplied. A subsequent call to GetPixel might return a different value because of rounding. Also, if you call SetPixel on a Bitmap whose color depth is 16 bits per pixel, information could be lost in the conversion from 32 to 16 bits, and a subsequent call to GetPixel might return a different value. Example The following example creates a Bitmap object based on a JPEG file. The code draws the bitmap once unaltered. Then the code calls the SetPixel method to create a checkered pattern of black pixels in the bitmap and draws the altered bitmap. Show Example VOID Example_SetPixel(HDC hdc) { Graphics graphics(hdc); // Create a Bitmap object from a JPEG file. Bitmap myBitmap(L"Climber.jpg"); // Draw the bitmap. graphics.DrawImage(&myBitmap, 0, 0); // Create a checkered pattern with black pixels. for (UINT row = 0; row < myBitmap.GetWidth(); row += 2) { for (UINT col = 0; col < myBitmap.GetHeight(); col += 2) { myBitmap.SetPixel(row, col, Color(255, 0, 0, 0)); } } // Draw the altered bitmap. graphics.DrawImage(&myBitmap, 200, 0); } Method Information Stock Implementation gdiplus.dll Header Declared in Gdiplusheaders.h, include gdiplus.h Import library gdiplus.lib Minimum availability GDI+ 1.0 Minimum operating systems Windows 98/Me, Windows XP, Windows 2000, Windows NT 4.0 SP6 See Also Color, Bitmap::GetPixel, Image, Using Images, Bitmaps, and Metafiles, Images, Bitmaps, and Metafiles --------------------------------------------------------------------------------
赵4老师 2015-04-30
  • 打赏
  • 举报
回复
仅供参考:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")

using namespace std;
using namespace Gdiplus;

int main() {
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);

    wstring infilename(L"1.jpg");
    string outfilename("color.txt");

    Bitmap* bmp = new Bitmap(infilename.c_str());
    UINT height = bmp->GetHeight();
    UINT width  = bmp->GetWidth();
    cout << "width " << width << ", height " << height << endl;

    Color color;
    ofstream fout(outfilename.c_str());

    for (UINT y = 0; y < height; y++)
    for (UINT x = 0; x < width ; x++) {
            bmp->GetPixel(x, y, &color);
            fout << x << "," << y << ";"
                 << (int)color.GetRed()   << ","
                 << (int)color.GetGreen() << ","
                 << (int)color.GetBlue()  << endl;
    }

    fout.close();

    delete bmp;
    GdiplusShutdown(gdiplustoken);
    return 0;
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧