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


/ Published in: C++
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. #define _USE_MATH_DEFINES // needed only for M_PI
  2. #include "math.h" // needed only for sin()
  3. // Standard MFC includes define CImage, CMitmap and Gdiplus
  4. .
  5. .
  6. .
  7.  
  8. LPCSTR filename = "c:\\testBmp.bmp";
  9. int width = 640;
  10. int height = 480;
  11.  
  12. CBitmap bitmap;
  13. bitmap.CreateBitmap(width, height, 1, 1, NULL ); // 1,1 means 1 mono-chrome layer.
  14.  
  15. CImage image;
  16. image.Attach(bitmap);
  17.  
  18. // For each word in array, set a pixel. In this case, draw a sine wave
  19. for ( int i=0; i<width; i++ )
  20. {
  21. int y = (int)((sin((double)i*2.0*M_PI/width )+1.0)*(height-1)/2.0);
  22. image.SetPixel( i, y, RGB( 0xFF, 0xFF, 0xFF ) );
  23. }
  24.  
  25. image.Save( filename, Gdiplus::ImageFormatBMP);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.