c语言调用这个方法在24位bmp位图上画一个矩形(动态二维数组)

#include<stdio.h>
#include<windows.h>

typedef struct tagPoint{
    int  x;
    int  y;
}Point;

typedef struct tagRGB{
    BYTE    rgbBlue;
    BYTE    rgbGreen;
    BYTE    rgbRed;
}RGB;

/**************************************************************
功能:在24位位图上通过传入的参数画一个矩形
参数:  pfin 要绘制的位图
    savePath 保存路径
    LUpoint  矩形左上顶点
    RDpoint  矩形右下顶点
    colour  线框的颜色
作者:airduce
时间:2018-9-27 15:48:27
修改时间:无
修改内容:新建
**************************************************************/
int drawRectangle(FILE * pfin,char * savePath,Point LUpoint,Point RDpoint,RGB colour){
    BITMAPFILEHEADER fileHeader;
    BITMAPINFOHEADER infoHeader;
    //读取文件头
    fread(&fileHeader,sizeof(BITMAPFILEHEADER),1,pfin);
    //读取信息头
    fread(&infoHeader,sizeof(BITMAPINFOHEADER),1,pfin);

    if(infoHeader.biBitCount==24)
    {
        //int size=infoHeader.biWidth*infoHeader.biHeight;
        //RGBQUAD img[256][256]; 

        //创建动态数组
        RGB **img;
        img = (RGB**)malloc(sizeof(RGB*)*infoHeader.biHeight);//创建一个指针数组,把指针数组的地址赋值给
        for (int i = 0; i < infoHeader.biHeight; i++)
            img[i] = (RGB*)malloc(sizeof(RGB)*infoHeader.biWidth);//给第二维分配空间

        //读取位图信息
        for(int i = 0; i<infoHeader.biHeight;i++){
            fread(img[i],sizeof(RGB)*infoHeader.biWidth,1,pfin);
        }
        //fread(img,sizeof(RGBQUAD),size,pfin);

        //上横线
        for(int i=0;i<infoHeader.biWidth;i++){    
            if((i>LUpoint.x||i==LUpoint.x)&&(i<RDpoint.x||i==RDpoint.x)){
                img[RDpoint.y][i].rgbBlue=colour.rgbBlue;
                img[RDpoint.y][i].rgbGreen=colour.rgbGreen;
                img[RDpoint.y][i].rgbRed=colour.rgbRed;
                //img[100][i].rgbReserved=0;
            }

        }

        //下横线
        for(int i=0;i<infoHeader.biWidth;i++)
        {    
            if((i>LUpoint.x||i==LUpoint.x)&&(i<RDpoint.x||i==RDpoint.x)){
                img[LUpoint.y][i].rgbBlue=colour.rgbBlue;
                img[LUpoint.y][i].rgbGreen=colour.rgbGreen;
                img[LUpoint.y][i].rgbRed=colour.rgbRed;
                //img[100][i].rgbReserved=0;
            }
        }
        //左横线
        for(int i=0;i<infoHeader.biWidth;i++)
        {    if(i>LUpoint.y&&i<RDpoint.y){
            img[i][LUpoint.x].rgbBlue=colour.rgbBlue;
            img[i][LUpoint.x].rgbGreen=colour.rgbGreen;
            img[i][LUpoint.x].rgbRed=colour.rgbRed;
            //img[150][i].rgbReserved=0;
        }
        }
        //右横线
        for(int i=0;i<infoHeader.biWidth;i++)
        {
            if(i>LUpoint.y&&i<RDpoint.y){
                img[i][RDpoint.x].rgbBlue=colour.rgbBlue;
                img[i][RDpoint.x].rgbGreen=colour.rgbGreen;
                img[i][RDpoint.x].rgbRed=colour.rgbRed;
                //img[150][i].rgbReserved=0;
            }
        }

        //将修改后的图片保存到文件
        FILE * pfout = fopen(savePath,"wb");
        fwrite(&fileHeader,sizeof(fileHeader),1,pfout);
        fwrite(&infoHeader,sizeof(infoHeader),1,pfout);
        //fwrite(img,sizeof(RGBQUAD),size,pfout);
        for(int i = 0; i<infoHeader.biHeight;i++){
            fwrite(img[i],sizeof(RGB)*infoHeader.biWidth,1,pfout);
        }
        //释放空间
        for(int i = 0; i<infoHeader.biHeight;i++){
            free(img[i]);
        }
        free(img);
        fclose(pfin);
        fclose(pfout);

        return 0;
    }
}


int main(void){
    errno_t err;
    FILE *pfin;
    err=fopen_s(&pfin ,"F:\pic940.bmp","rb");
    if( err!=0 )
    {
        printf_s("cannot open this file");
        return 0;

    }
    char * myPath = "F:\haha.bmp";

    Point pPoint1,pPoint2;
    pPoint1.x = 20;
    pPoint1.y = 20;
    pPoint2.x = 180;
    pPoint2.y = 200;
    //RGBQUAD colour;
    RGB colour;
    colour.rgbRed=255;
    colour.rgbGreen=0;
    colour.rgbBlue=255;
    //colour.rgbReserved=0;
    drawRectangle(pfin,myPath,pPoint1,pPoint2,colour);
    return 0;
}

 运行效果:

原文地址:https://www.cnblogs.com/airduce/p/9713593.html