Display IPLimage/ MAT (opencv) in MFC picture box - c++

I want to display an IPLimage / MAT (opencv) formats on a MFC picture box in Visual C++

Here is a complete solution describing how to display an IplImage in a MCF/C++ form application using OpenCV http://ac-it.pl/display-an-iplimage-in-a-mcfc-form-application-using-opencv

Picture boxes can only display bitmaps I think. You'll need to convert IPLimage to a bitmap.
Assuming you can first convert IPLimage to a buffer of RGB values, see Creating HBITMAP from memory buffer
Then when you get the HBITMAP, you'll need to load it in the Picture Box. Assuming by picture box you mean a static control, add SS_BITMAP to its styles, and then load the HBITMAP using
SendMessage(hStatic, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);

Related

Transfer image from CDC to Windows Imaging Component (WIC)?

I have some old C++/MFC code which uses GDI to create an image in a device context (CDC), using functions like Rectangle. The image is then painted to the screen, by the destructor of CPaintDC. Everything works as expected.
Now I want to store the image in a PNG file. For this, I am using Windows Imaging Component (WIC). I can create an image in WIC by creating a bitmap frame, storing pixels in a buffer, and copying the pixels into it using WritePixels. Then WIC stores the image as a PNG file via Commit. All this also works.
The question is, how can I transfer the image I created using GDI, which is in a CDC, to something WIC recognizes? I'm looking for something more efficient than a CDC::GetPixel loop storing into a buffer followed by WritePixels.
Windows 7, Visual Studio 2015, C++.
Update: I'm trying to get the CImage method suggested by #Andrew Komiagin. My code is
CDC memdc;
memdc.CreateCompatibleDC(nullptr);
myControl.RenderToDC(memdc); // this draws the image onto the dc
CBitmap bmp;
BOOL ok=bmp.CreateCompatibleBitmap(&memdc, w, h); // w=h=292 defined elsewhere
CBitmap *pOldBitmap=memdc.SelectObject(&bmp);
ok=memdc.BitBlt(0, 0, w, h, &memdc, 0, 0, SRCCOPY);
CImage tempImageObj;
tempImageObj.Attach((HBITMAP)bmp.Detach());
CString outputFilename("outputImage.png");
HRESULT hr=tempImageObj.Save(outputFilename);
This produces a PNG image file with an all-black square, w x h pixels, rather than the image I drew. I know the image is being drawn correctly by RenderToDC (see below).
For comparison, this is in MyControl::OnPaint for the control (derived from CStatic):
CPaintDC dc(this); // device context for painting
RenderToDC(dc); // this draws the image onto the dc
// Painted to screen by CPaintDC's destructor
The image appears correctly.
I'd suggest using standard CImage class from ATL/MFC that works just great with GDI device context and natively supports the ability to load and save images in JPEG, GIF, BMP, and Portable Network Graphics (PNG) formats.
Here is an example on how to use it:
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
CBitmap Bmp;
Bmp.CreateCompatibleBitmap(&dc,ClientRect.Width(),ClientRect.Height());
MemDC.SelectObject(&Bmp);
MemDC.BitBlt(0,0,ClientRect.Width(),ClientRect.Height(),&dc,0,0,SRCCOPY);
CImage TempImageObj;
TempImageObj.Attach((HBITMAP)Bmp.Detach());
TempImageObj.Save(sFilePath);

Save wxWidgets window screenshot

I have a wxWidgets application. I wan't to get the window screen as image and save it into a file.
I know that the wxImage can be saved easily:
wxImage im;
im.Create(window_width, window_height)
/**assign some data**/
im.SaveFile("path/screen.png", wxBITMAP_TYPE_PNG);
However, how do I gather the window (and I want only the window!) pixel data?
You can use wxClientDC to access the window data. Then just Blit() them to a wxMemoryDC with a wxBitmap selected into it and finally convert the bitmap to wxImage.
Here is some detailed code showing how to do this - it is the second article down.
http://forums.wxwidgets.org/viewtopic.php?p=32313

Draw WICBitmap to window?

I have a WICBitmap that contains an image in my MFC program. Is there any way to draw this bitmap onto a device context?
void DrawBitmap(IWICBitmapSource source, HDC targetDC, int X, int Y, int cx, int cy)
{
//source: The Bitmap to draw
//TargetDC: The HDC to draw onto (e.g. a printer or screen DC)
//X,Y: The location on the DC to draw the bitmap
}
There are a number of samples on MSDN that show how to display a WIC bitmap using, for example, GDI, GDI+, Direct2D etc.
MSDN: Image Decoding Samples (archive)
Introduction to WIC: How to use WIC to load an image, and draw it with GDI? (archive)
Microsoft provides a WIC Image Viewer Using GDI Sample (GitHub mirror)
The basic idea is:
convert IWicBitmapSource into a standard Windows BITMAP (CreateDibSectionFromBitmapSource)
draw using the BITMAP using your standard tools
The file can be edited separately as bmp file, and after you can load WIC object as you can see in How to Load a Bitmap from a File.

How to Place the image at specific location of the Picture Conrol in VC++

I m trying to overlay an image on a live video.The IDE used is Visual Studio Professional 2010. The code is developed in C++.When I try to overlay image on the live video, I m loading the image using a handle i.e.
HANDLE hBitmap;
hBitmap = LoadImage(NULL, L"C:\\Users\\User\\Documents\\Visual Studio 2010\\Projects\\BMP_Image.bmp",IMAGE_BITMAP, 0,0, LR_LOADFROMFILE );
I am using the mechanism of Alpha Blending to overlay the image on to the video. Problem I have encountered is, The image is overlayed five consecutive times on to the video .
I m passing the ID of a picture control, placed on the dialog box in the rc file, to load the live video and display it. I m not using any MFCs in the code. The video is rendered by using Direct 2D mechanism
The video resolution is 640*480 and bitmap image resolution is 128*128. If I take this bitmap , the video will produce five times of overlayed image on to the video..
I got the coordinates of dialog box using GetWindowRect() and ScreentoClient(). But I want the coordinates of the Picture control.
I have following doubts:
How to get the coordinates of the picture control???
How to place the image at specific location in a picture control???
Looking forward for ur positive response as earliest...
Regards,
Vivek
You can use the following function for retreiving the handle of your picture control:
::GetDlgItem( hWnd of your dialog, Id of your picture control)
By retreiving its handle, you can get to other info like its position, size and
::GetWindowRect(hWnd, &rc)
If you overlay the image through some API calls such as StretchBlt, you can place it anywhere you want.

Show Win32 popup menu with PNG icons from resources

It's been a long time since I've had to deal with Win32 menus. I need to add some PNG icons to a Win32 context popup menu. Naturally, I want to preserve PNG transparency and all the per-pixel-alpha in the process. Is this possible?
I was thinking on using SetMenuItemBitmaps. Is that the way to go?
I imported my PNGs as "PNG" resources but I can't seem to load them neither with LoadBitmap nor with LoadImage. I found some suggestions about using Gdi+ but obviously I won't be drawing the menu - the system will.
There seems to be a way to get a HBITMAP from a Gdi+ Bitmap but it looks as if all the alpha is getting lost in the process. AFAIK, a HBITMAP can happily host alpha information.
You need GDI+ to load a PNG. Then you need to create a 32-bit alpha bitmap of the correct size, create a Graphics on the bitmap, and use DrawImage to copy the PNG to the bitmap. That gives you a bitmap with an alpha channel.
Something like this:
Image* pimgSrc = Image::FromFile("MyIcon.png"L, FALSE);
Bitmap* pbmpImage = new Bitmap(
iWidth, iHeight,
PixelFormat32bppARGB
);
Graphics* pgraphics = Graphics::FromImage(bmpImage))
{
// This draws the PNG onto the bitmap, scaling it if necessary.
// You may want to set the scaling quality
graphics->DrawImage(
imageSrc,
Rectangle(0,0, bmpImage.Width, bmpImage.Height),
Rectangle(0,0, imgSrc.Width, imgSrc.Height),
GraphicsUnitPixel
);
}
// You can now get the HBITMAP from the Bitmap object and use it.
// Don't forget to delete the graphics, image and bitmap when done.
Perhaps you could use icon instead?
Here are my reasons for using icons instead of PNGs:
The Win32 API has good support icons, and it relatively much easier to draw icons, since GDI+ is not required.
Icons also support 8-bit transparency (just like PNGs).
Icons can be any size in pixels (just like PNGs).
Icons can easily be embedded in the executable as a resource.
Icons can be edited via Visual Studio.
To load an icon from a resource or a file use:
LoadImage()
To draw the icon, use:
DrawIcon() or DrawIconEx()