Image Processing Program (band) favorites Based on MFC SDI
This program mainly implements the opening and saving of Bitmap. jpg format is used as BMP format, and 24-bit BMP is converted to 8-bit, 4-bit, 1-bit (black and white) and grayscale. There are also some simple algorithms, such as reversed colors, Gaussian smoothing for image blur, and contour extraction by using the Canny operator (of course these algorithms are taught by my teacher, but they are doctors, I don't have the skills that I can express in computer languages. I have limited qualifications after all ). The ipicture interface is required for JPG display and conversion to BMP.
Any image processing book will never skip bitmap. I have summarized the following methods:
Method 1: DWORD filesize;
Cfile file;
File. Open (". // hr.bmp", cfile: moderead );
Filesize = (DWORD) file. getlength ();
Byte * Buf = new byte [filesize];
File. readhuge (BUF, filesize );
File. Close ();
Cwnd * hwnd = getdlgitem (idc_ipc); // idc_ipc is the ID of the image resource.
HDC = hwnd-> getdc ()-> m_hdc;
Crect rect;
Hwnd-> getclientrect (& rect );
Bitmapfileheader * pbmfh;
Pbmfh = (bitmapfileheader *) BUF;
Bitmapinfoheader * pbminfohead = (bitmapinfoheader *) (pbmfh + 1 );
Int I = stretchdibits (
HDC, // handle to DC
0, // X-coord of destination upper-left corner
0, // y-coord of destination upper-left corner
Rect. Width (), // width of destination rectangle
Rect. Height (), // height of destination rectangle
0, // X-coord of source upper-left corner
0, // y-coord of source upper-left corner
Pbminfohead-> biwidth, // width of source rectangle
Pbminfohead-> biheight, // height of source rectangle
Buf, // bitmap bits
(Bitmapinfo *) (pbminfohead), // bitmap data
Dib_rgb_colors, // usage options
Srccopy // raster operation code
);
/* Setdibitstodevice (HDC, 0, 0, rect. Width (), rect. Height (),
0, 0, 0, pbminfohead-> biheight, Buf, (bitmapinfo *) (pbminfohead ),
Dib_rgb_colors );*/
Delete Buf;
Method 2:
Hbitmap HBM = (hbitmap): LoadImage (null, (lpctstr) ". // hr.bmp", image_bitmap, 0, 0, lr_loadfromfile );
Cbitmap m_bitmap;
If (m_bitmap.attach (HBM ))
{
Bitmap bm;
M_bitmap.getbitmap (& BM );
M_bitmap.setbitmapdimension (BM. bmwidth, BM. bmheight );
}
CDC memdc;
Crect RT;
Getclientrect (& RT );
Memdc. createcompatibledc (PDC );
Cbitmap * pimage = memdc. SelectObject (& pdoc-> m_bitmap );
Csize imagesize = pdoc-> m_bitmap.getbitmapdimension ();
// This method is called by the ondraw function of the View class.
// PDC-> bitblt (0, 0, imagesize. CX, imagesize. Cy, & memdc, 0, 0, srccopy );
PDC-> stretchblt (0, 0, Rt. Width (), Rt. Height (), & memdc,
0, 0, imagesize. CX, imagesize. Cy, srccopy );
Memdc. SelectObject (pimage );
However, in this program, I used the classic CDIB class in the image processing textbook (MFC is not provided and needs to be compiled by myself). With the CDIB class, any processing of bitmap is so simple and convenient.
Next, I saved a 24-Bit Bitmap in the drawing using the screen to demonstrate how to convert it into a 4-Bit Bitmap (see if the effect of the 4-Bit Bitmap is much worse ?)
If you want source code can send me QQ mailbox (250540696@qq.com) Mail, hope to make progress together with software enthusiasts.
That's all!
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/qifeng020717/archive/2009/09/28/4603755.aspx