Display image in the MFC window

Source: Internet
Author: User

On the first day, I made this thing (implemented by opencv ):

First, you can display images on the left. After selecting different images, click the capture button to save the images in a small window on the right.

Solve the problem of an adaptive window for image display today:

Generally, when creating an image, we need to assign the image length and width. Can we assign the window length and width to the image? The answer is yes.

First, we need to get the window handle:

Crect r_show;
Getdlgitem (ID)-> getclientrect (r_show );

The ID is the ID of the window (the right-click window property will be displayed)

Then, get the length and width:

Int widthpos = r_show.width ();
Int heightpos = r_show.height ();

Just assign them to the image.

Iplimage * IMG = 0;
Cvsize dst_cvsize;

Dst_cvsize.width = widthpos;
Dst_cvsize.height = heightpos;
IMG = cvcreateimage (dst_cvsize, Src-> depth, Src-> nchannels );

At first, I was scared when I saw the handle. Later I realized that it was really useful!

Below is the function code for displaying images (for your reference only ):

// Myshowmfc (iplimage * SRC, CDC * PDC, uint ID) the first parameter is the image to be displayed,
// The second parameter is the window handle, and the third parameter is the ID of the control that displays the image.
Void cexamineeyedlg: myshowmfc (iplimage * SRC, CDC * PDC, uint ID)
{
Crect r_show;
Getdlgitem (ID)-> getclientrect (r_show); // get the handle of the Image Display Control.
Int xpos = r_show.left;
Int ypos = r_show.top;
Int widthpos = r_show.width ();
Int heightpos = r_show.height ();

Iplimage * IMG = 0;
Cvsize dst_cvsize; // target image size
Dst_cvsize.width = widthpos;
Dst_cvsize.height = heightpos;
IMG = cvcreateimage (dst_cvsize, Src-> depth, Src-> nchannels); // construct the Target Image
Cvresize (SRC, IMG, cv_inter_linear); // scale the source image to the Target Image
// Src = cvcloneimage (IMG );
DWORD Height = IMG-> height;
DWORD width = IMG-> width;
DWORD linebytes = (width * 8 + 31)/32*4;
DWORD linebytes24 = (width * 24 + 31)/32*4;
Byte * temp = new byte [img-> height * linebytes24];
If (! Temp) return;
Memset (temp, 0, height * linebytes24 );
Word bitcount;
If (IMG-> nchannels = 1)
{
Bitcount = 8;
}
Else if (IMG-> nchannels = 3)
{
Bitcount = 24;
}
Else
{
Delete [] temp;
Temp = NULL;
Return;
}
If (bitcount = 8)
{
For (INT I = 0; I height; I ++)
{
For (Int J = 0, n = 0; j width * 3; j ++, N ++)
{
* (Temp + linebytes24 * (height-1-i) + J) = (byte) (uchar *) (IMG-> imagedata + IMG-> widthstep * I) [N];
J ++;
* (Temp + linebytes24 * (height-1-i) + J) = (byte) (uchar *) (IMG-> imagedata + IMG-> widthstep * I) [N];
J ++;
* (Temp + linebytes24 * (height-1-i) + J) = (byte) (uchar *) (IMG-> imagedata + IMG-> widthstep * I) [N];
}
}
}
Else
{
For (INT I = 0; I height; I ++)
{
For (Int J = 0, n = 0; j width * 3; j ++, N ++)
{
* (Temp + linebytes24 * (height-1-i) + J) = (byte) (uchar *) (IMG-> imagedata + IMG-> widthstep * I )) [3 * n];
J ++;
* (Temp + linebytes24 * (height-1-i) + J) = (byte) (uchar *) (IMG-> imagedata + IMG-> widthstep * I )) [3 * n + 1];
J ++;
* (Temp + linebytes24 * (height-1-i) + J) = (byte) (uchar *) (IMG-> imagedata + IMG-> widthstep * I )) [3 * n + 2];
}
}
}
Bitmapinfo Bi;
Bi. bmiheader. bibitcount = 24;
Bi. bmiheader. biclrimportant = 0;
Bi. bmiheader. biclrused = 0;
Bi. bmiheader. bicompression = 0l;
Bi. bmiheader. biheight = height;
Bi. bmiheader. biplanes = 1;
Bi. bmiheader. bisize = sizeof (bitmapinfoheader );
Bi. bmiheader. bisizeimage = height * linebytes;
Bi. bmiheader. biwidth = width;
Bi. bmiheader. bixpelspermeter = 0;
Bi. bmiheader. biypelspermeter = 0;
Stretchdibits (PDC-> m_hdc, width, height, width, height, temp, (bitmapinfo *) & BI, dib_rgb_colors, srccopy );
Cvreleaseimage (& IMG );
Delete [] temp;
Temp = NULL;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.