怎么样将BMP格式的图片以二进制的形式放在二维数组中,再讲修改后的二维数组转换成BMP图片进行输出

C++语言 码拜 7年前 (2017-05-08) 1908次浏览
说明:
现有图像1.bmp,本人定义一个二维数组int PIC[640][480];然后本人想讲bmp中的每个像素通过PIC[640][480]进行展开(不要说图像怎么怎样,假定为30W像素了),然后本人想将PIC[200~600][100~400]这个数组之间的位置输出出来,即为(截图),然后将输出出来的新的二维数组转换成图像,并且进行保存。有木有高手会的!感觉已经把要求说的很明确了,有代码的狂砸向本人。怎么样将BMP格式的图片以二进制的形式放在二维数组中,再讲修改后的二维数组转换成BMP图片进行输出
解决方案

20

BMP百度下就有文件格式说明
每个像素使用3byte存储,即RGB的值,假如每一行不足4(还是什么)整数倍,需要补足
本人照着格式写个读取的代码吧,BMP文件头指明了各种信息
例如宽高,是使用RGB存储颜色信息还是颜色索引表(这个需要比较注意)等

20

仅供参考:

#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;
}

20

再供参考:

#include <math.h>
#include <string.h>
#include <stdio.h>
#include <windows.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
wchar_t formats[5][11]={
    L"image/bmp",
    L"image/jpeg",
    L"image/gif",
    L"image/tiff",
    L"image/png",
};
wchar_t exts[5][5]={
    L".bmp",
    L".jpg",
    L".gif",
    L".tif",
    L".png",
};
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
   UINT  num = 0;          // number of image encoders
   UINT  size = 0;         // size of the image encoder array in bytes
   ImageCodecInfo* pImageCodecInfo = NULL;
   GetImageEncodersSize(&num, &size);
   if(size == 0) return -1;  // Failure
   pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
   if(pImageCodecInfo == NULL) return -1;  // Failure
   GetImageEncoders(num, size, pImageCodecInfo);
   for (UINT j = 0; j < num; ++j) {
      if ( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) {
         *pClsid = pImageCodecInfo[j].Clsid;
         free(pImageCodecInfo);
         return j;  // Success
      }
   }
   free(pImageCodecInfo);
   return -1;  // Failure
}
int wmain(int argc,wchar_t *argv[]) {
    int r=1;
    if (argc<4) {
    USAGE:
        wprintf(L"%s srcimg.{bmp|jpg|gif|tif|png|wmf|emf|ico}  desimg.{bmp|jpg|gif|tif|png}  angle\n",argv[0]);
        return r;
    }
    int i;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[1]+wcslen(argv[1])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    for (i=0;i<5;i++) {
        if (0==_wcsicmp(argv[2]+wcslen(argv[2])-4,exts[i])) break;
    }
    if (i>=5) goto USAGE;
    GdiplusStartupInput gdiplusstartupinput;
    ULONG_PTR gdiplustoken;
    GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, NULL);
    {
        Image img(argv[1]);
        if (Ok==img.GetLastStatus()) {
            UINT height = img.GetHeight();
            UINT width  = img.GetWidth();
            REAL angle;
            if (1==swscanf_s(argv[3],L"%f",&angle)) {
                REAL size;
                size=(REAL)sqrt(1.0*width*width+1.0*height*height);
                Matrix mat;
                mat.Translate(size / -2.0f, size / -2.0f);
                mat.Rotate(-angle, MatrixOrderAppend);
                mat.Translate(size / 2.0f, size / 2.0f, MatrixOrderAppend);
                PointF pfTL((size-width)/2.0f      ,(size-height)/2.0f       );
                PointF pfTR((size-width)/2.0f+width,(size-height)/2.0f       );
                PointF pfBL((size-width)/2.0f      ,(size-height)/2.0f+height);
                PointF pfBR((size-width)/2.0f+width,(size-height)/2.0f+height);
                Graphics tgp(&img);
                Bitmap bmp((UINT)size,(UINT)size,&tgp);//Let bmp Resolution equal to img Resolution
                Graphics gp(&bmp);
                gp.SetTransform(&mat);
                gp.DrawImage(&img,pfTL);
                REAL xmin,ymin,xmax,ymax,x,y,rw,rh;
                mat.TransformPoints(&pfTL);
                xmin=xmax=pfTL.X;
                ymin=ymax=pfTL.Y;
                mat.TransformPoints(&pfTR);
                if (xmin>pfTR.X) xmin=pfTR.X;
                if (xmax<pfTR.X) xmax=pfTR.X;
                if (ymin>pfTR.Y) ymin=pfTR.Y;
                if (ymax<pfTR.Y) ymax=pfTR.Y;
                mat.TransformPoints(&pfBL);
                if (xmin>pfBL.X) xmin=pfBL.X;
                if (xmax<pfBL.X) xmax=pfBL.X;
                if (ymin>pfBL.Y) ymin=pfBL.Y;
                if (ymax<pfBL.Y) ymax=pfBL.Y;
                mat.TransformPoints(&pfBR);
                if (xmin>pfBR.X) xmin=pfBR.X;
                if (xmax<pfBR.X) xmax=pfBR.X;
                if (ymin>pfBR.Y) ymin=pfBR.Y;
                if (ymax<pfBR.Y) ymax=pfBR.Y;
                x=xmin;
                y=ymin;
                rw=xmax-x;
                rh=ymax-y;
                Bitmap* clone;
                clone = bmp.Clone(x,y,rw,rh,PixelFormat24bppRGB);//bmp.GetPixelFormat()
                CLSID encoderClsid;
                if (0<=GetEncoderClsid(formats[i],&encoderClsid)) {
                    if (Ok==clone->Save(argv[2],&encoderClsid)) {
                        wprintf(L"OK to %s  %s  %s  %s\n",argv[0],argv[1],argv[2],argv[3]);
                        r=0;
                    } else {
                        wprintf(L"Error to save %s\n",argv[2]);
                        r=4;
                    }
                } else {
                    wprintf(L"Error to GetEncoderClsid(%s,...)\n",formats[i]);
                    r=3;
                }
                delete clone;
            } else {
                wprintf(L"Error to get angle %s\n",argv[3]);
                r=2;
            }
        } else {
            wprintf(L"Error to load %s\n",argv[1]);
            r=5;
        }
    }
    GdiplusShutdown(gdiplustoken);
    return r;
}

20

再供参考:

//有两个每一项都是0或1组成的二维数组:A[50][50]和B[2][2]
//找出在A里包含有多少与B相同的项,并标出坐标
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int A[50][50];
int B[2][2];
int y,x;
int j,i;
int n;
int main() {
    srand(time(NULL));
    for (y=0;y<50;y++) {
        for (x=0;x<50;x++) {
            A[y][x]=rand()%2;
        }
    }
    for (y=0;y<2;y++) {
        for (x=0;x<2;x++) {
            B[y][x]=rand()%2;
        }
    }
    n=0;
    for (y=0;y<50-2+1;y++) {
        for (x=0;x<50-2+1;x++) {
            for (j=0;j<2;j++) {
                for (i=0;i<2;i++) {
                    if (A[y+j][x+i]!=B[j][i]) goto NE;
                }
            }
            n++;
            printf("n    y,x=%4d %2d,%2d\n",n,y,x);
            NE:;
        }
    }
    return 0;
}
//n    y,x=   1  0, 1
//n    y,x=   2  0, 3
//n    y,x=   3  0,17
//n    y,x=   4  1,12
//n    y,x=   5  1,20
//n    y,x=   6  1,33
//n    y,x=   7  2, 4
//n    y,x=   8  2,32
//n    y,x=   9  3,13
//n    y,x=  10  3,31
//n    y,x=  11  3,48
//n    y,x=  12  4, 0
//n    y,x=  13  5,14
//n    y,x=  14  5,32
//n    y,x=  15  5,36
//n    y,x=  16  5,47
//n    y,x=  17  6, 7
//n    y,x=  18  6,17
//n    y,x=  19  6,45
//n    y,x=  20  7, 1
//n    y,x=  21  7,31
//n    y,x=  22  8, 0
//n    y,x=  23  8,16
//n    y,x=  24  8,18
//n    y,x=  25  8,25
//n    y,x=  26  9, 9
//n    y,x=  27  9,27
//n    y,x=  28 10,12
//n    y,x=  29 10,26
//n    y,x=  30 12,12
//n    y,x=  31 12,20
//n    y,x=  32 13,36
//n    y,x=  33 13,45
//n    y,x=  34 13,48
//n    y,x=  35 14, 5
//n    y,x=  36 14, 7
//n    y,x=  37 14, 9
//n    y,x=  38 14,21
//n    y,x=  39 15, 4
//n    y,x=  40 15,20
//n    y,x=  41 16,10
//n    y,x=  42 16,23
//n    y,x=  43 16,45
//n    y,x=  44 17,16
//n    y,x=  45 17,22
//n    y,x=  46 17,40
//n    y,x=  47 18, 1
//n    y,x=  48 18, 5
//n    y,x=  49 18,26
//n    y,x=  50 18,43
//n    y,x=  51 19,40
//n    y,x=  52 19,46
//n    y,x=  53 20,31
//n    y,x=  54 20,38
//n    y,x=  55 21,27
//n    y,x=  56 21,41
//n    y,x=  57 21,48
//n    y,x=  58 22,10
//n    y,x=  59 22,15
//n    y,x=  60 22,20
//n    y,x=  61 22,24
//n    y,x=  62 23, 0
//n    y,x=  63 23, 3
//n    y,x=  64 23,40
//n    y,x=  65 24,11
//n    y,x=  66 24,14
//n    y,x=  67 24,29
//n    y,x=  68 24,34
//n    y,x=  69 24,43
//n    y,x=  70 25,16
//n    y,x=  71 25,19
//n    y,x=  72 25,25
//n    y,x=  73 26,18
//n    y,x=  74 26,23
//n    y,x=  75 27, 9
//n    y,x=  76 27,44
//n    y,x=  77 28, 7
//n    y,x=  78 28,22
//n    y,x=  79 28,24
//n    y,x=  80 28,33
//n    y,x=  81 28,48
//n    y,x=  82 29, 0
//n    y,x=  83 29,35
//n    y,x=  84 29,41
//n    y,x=  85 29,44
//n    y,x=  86 30, 7
//n    y,x=  87 30,23
//n    y,x=  88 31,35
//n    y,x=  89 31,41
//n    y,x=  90 31,45
//n    y,x=  91 33,21
//n    y,x=  92 33,27
//n    y,x=  93 34, 2
//n    y,x=  94 34,19
//n    y,x=  95 34,25
//n    y,x=  96 34,47
//n    y,x=  97 35,14
//n    y,x=  98 36, 6
//n    y,x=  99 36,13
//n    y,x= 100 36,25
//n    y,x= 101 36,31
//n    y,x= 102 36,41
//n    y,x= 103 36,47
//n    y,x= 104 37,11
//n    y,x= 105 37,16
//n    y,x= 106 37,24
//n    y,x= 107 37,30
//n    y,x= 108 38,18
//n    y,x= 109 38,20
//n    y,x= 110 38,28
//n    y,x= 111 38,38
//n    y,x= 112 39,32
//n    y,x= 113 39,43
//n    y,x= 114 39,45
//n    y,x= 115 39,48
//n    y,x= 116 40,35
//n    y,x= 117 40,38
//n    y,x= 118 41,33
//n    y,x= 119 42, 9
//n    y,x= 120 42,13
//n    y,x= 121 42,22
//n    y,x= 122 42,25
//n    y,x= 123 43,21
//n    y,x= 124 44,25
//n    y,x= 125 45,15
//n    y,x= 126 45,20
//n    y,x= 127 45,42
//n    y,x= 128 46, 4
//n    y,x= 129 47, 3
//n    y,x= 130 47,22
//n    y,x= 131 47,33
//n    y,x= 132 47,43
//n    y,x= 133 47,45
//n    y,x= 134 47,48
//n    y,x= 135 48, 0
//

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么样将BMP格式的图片以二进制的形式放在二维数组中,再讲修改后的二维数组转换成BMP图片进行输出
喜欢 (0)
[1034331897@qq.com]
分享 (0)