none
模拟写渐变 红色bitmap 却变成R G B 三色渐变了 RRS feed

  • 问题

  • struct RGB24
    				{
    					unsigned char r;
    					unsigned char g;
    					unsigned char b;
    				};
    				RGB24 *src = new RGB24[pCodecCtx->width * pCodecCtx->height];
    				RGB24 *s = src;
    				for (int i = 0; i < pCodecCtx->height; i++)
    				{
    					for (int j = 0; j < pCodecCtx->width; j++)
    					{
    						s[j].r = (unsigned char )(j % 255);
    						s[j].g = 0;
    						s[j].b = 0;
    					}
    					s += pCodecCtx->width;
    				}
    
    				SaveAsBMP((unsigned char *)src, pCodecCtx->width, pCodecCtx->height, i++, 24);
    
    
    void SaveAsBMP(unsigned char *frame, int width, int height, int index, int bpp)
    {
    	char buf[5] = { 0 };
    	BITMAPFILEHEADER bmpheader;
    	BITMAPINFOHEADER bmpinfo;
    	FILE *fp;
    
    	char *filename = new char[255];
    
    	//文件存放路径,根据自己的修改  
    	//sprintf_s(filename, 255, "%s%d.bmp", "E:/temp/", index);
    	sprintf_s(filename, 255, "temp/%d.bmp", index);
    	if ((fp = fopen(filename, "w+")) == NULL) {
    		printf("open file failed!\n");
    		return;
    	}
    
    	bmpheader.bfType = 0x4d42;
    	bmpheader.bfReserved1 = 0;
    	bmpheader.bfReserved2 = 0;
    	bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    	bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp/8;
    
    	bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
    	bmpinfo.biWidth = width;
    	bmpinfo.biHeight = height;
    	bmpinfo.biPlanes = 1;
    	bmpinfo.biBitCount = bpp;
    	bmpinfo.biCompression = BI_RGB;
    	bmpinfo.biSizeImage = width*height * 3;// (width*bpp + 31) / 32 * 4 * height;
    	bmpinfo.biXPelsPerMeter = 100;
    	bmpinfo.biYPelsPerMeter = 100;
    	bmpinfo.biClrUsed = 0;
    	bmpinfo.biClrImportant = 0;
    
    	//RGBQUAD *pColorTable = new RGBQUAD[256];
    	
    	
    
    	fwrite(&bmpheader, sizeof(bmpheader), 1, fp);
    	fwrite(&bmpinfo, sizeof(bmpinfo), 1, fp);
    
    	unsigned char *src = frame;
    	for (int i = 0; i < height; i++)
    	{
    		fwrite(src, width*bpp / 8, 1, fp);
    		src += width * 3;
    	}
    
    	fclose(fp);
    }
    写出的位图如下 , 本来应该只有红色渐变,为什么成了这样
    2016年7月20日 10:06

答案

全部回复

  • 渐变色需要设置RGB各个分量的值,而不仅仅是R的值

    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.

    2016年7月21日 3:37
    版主
  • Hi wpf萌新,

    感谢在MSDN论坛发帖。

    请提供一些pCodecCtx定义的具体信息。看起来你在fwrite的时候取的长度不对,导致色板选择不正确。

    我建议你直接使用GradientFill function函数填充矩形渐变。

    这边有一个例子你可以参考一下。

    https://msdn.microsoft.com/en-us/library/windows/desktop/dd162485(v=vs.85).aspx

    希望对你有所帮助。

    Best Regards,

    Sera Yu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    2016年7月21日 5:50
  • 谢谢,已经解决了,是写文件的问题,图像是正确的
    2016年8月3日 6:52