Return to Snippet

Revision: 21629
at December 16, 2009 16:09 by jimfred


Initial Code
#define _USE_MATH_DEFINES // needed only for M_PI
#include "math.h" // needed only for sin()
// Standard MFC includes define CImage, CMitmap and Gdiplus
.
.
.

LPCSTR filename = "c:\\testBmp.bmp";
int width = 640;
int height = 480;

CBitmap bitmap;
bitmap.CreateBitmap(width, height, 1, 1, NULL ); // 1,1 means 1 mono-chrome layer.

CImage image;
image.Attach(bitmap);

// For each word in array, set a pixel. In this case, draw a sine wave
for ( int i=0; i<width; i++  )
{
   int y = (int)((sin((double)i*2.0*M_PI/width )+1.0)*(height-1)/2.0);
   image.SetPixel( i, y, RGB( 0xFF, 0xFF, 0xFF ) );
}

image.Save( filename, Gdiplus::ImageFormatBMP);

Initial URL

                                

Initial Description
For C#, see http://snipplr.com/view/27706/c-draw-sine-wave-on-bitmap-and-save-to-a-file/

Most examples seem to include a DC but I needed simple code to store MICR data to a BMP file.

Initial Title
MFC, CImage, CMitmap, draw a sine wave and save to a BMP file.

Initial Tags

                                

Initial Language
C++