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
Related
Hello I'm studying MFC and I wanna know how to save a specific part of image which is in Picture Control when I click.
Here is the sequence what I want to do
I already make a function that when I click grab button, it shows the image in picture control.
After that, I want to save image when I click specific area.
I attach the image to help understand.
"I want to know how to save the surrounding area as an image when I click a specific part of the picture control as shown above."
It would be easy problem, but I'd appreciate it if you understand because it's my first time doing this.
I tryed to use ScreenToClient function, but I failed.
There are quite a few ways to do this, but the general idea is usually pretty much the same:
Get a DC to the screen or the window's client area (aka DC1).
Create a DC compatible with DC1 (aka DC2).
create a CBitmap compatible with DC1, of the size you want to copy/save.
select the bitmap into DC2.
bitblt the area you care about from DC1 to DC2.
Create a CImage object.
Detach the bitmap from the CBitmap, and attach it to the CImage.
Use CImage::Save to save the bitmap to a file.
There are variations (e.g., writing the data to a file without using a CImage), but they tend to be tedious, and unless you're willing to put quite a bit of effort into things, fairly limited as well. e.g., writing a .BMP file on your own isn't terribly difficult, but is a bit tedious.If you want to support writing JPEG, TIFF, PNG, etc., you either need to integrate a number of libraries, or else write quite a bit of code for the file formats. CImage is somewhat limited as well, but at least supports a half dozen or so of the most common formats (and most people find just JPEG, PNG and possibly GIF sufficient).
Here's a bit of tested sample code for saving a 200x200 chunk of whatever's displayed in the current view window into a file named capture.jpg:
void CPicCapView::OnLButtonDown(UINT nFlags, CPoint point) {
// width and height of area to capture
static const uint32_t width = 200;
static const uint32_t height = 200;
CClientDC dc(this);
CDC temp;
temp.CreateCompatibleDC(&dc);
CBitmap bmp;
bmp.CreateCompatibleBitmap(&dc, width, height);
temp.SelectObject(&bmp);
// x-width/2, y-width/2 to get area centered at click point
temp.BitBlt(0, 0, width, height, &dc, point.x- width/2, point.y-height/2, SRCCOPY);
CImage dest;
dest.Attach((HBITMAP)bmp.Detach());
// for the moment, just saving to a fixed file name
dest.Save(L".\\capture.jpg");
CView::OnLButtonDown(nFlags, point);
}
It might initially seem like you should be able to create the CImage object, use its Create to create a bitmap, and then blit directly from the screen bitmap to the CImage bitmap. Unfortunately, CImage's BitBlt uses the CImage as a source, not a destination, so although there may be some way to get this to work, the obvious method doesn't.
I am new to C++Builder 6, but I have used C++. My question is, how do I set a background for my form? When I open the application I want the background to have a specific image. I tried with Graphics::TBitmap but it says that the image is not valid even if it is bmp.
Graphics::TBitmap *BmpTabla = new Graphics::TBitmap;
try {
BmpTabla->LoadFromFile("board.bmp");
}
__finally
{
delete BmpTabla;
}
Edit: I just changed the picture and worked fine.
Drop TImage component to the form, send it to back of the all controls, set the Align property to alClient.
In runtime
Image1->Picture->LoadFromFile("board.bmp");
The easiest way to have a custom background is to use a client-aligned TImage, like #serge suggested.
Another way is to load the BMP image into a Graphics::TBitmap object (like you are already attempting to do) and then use the Form's OnPaint event to draw the TBitmap onto the Form's Canvas.
The "image is not valid" error means you are trying to load a .bmp file that is not a valid BMP image. Double check the contents of the file.
I have a framegrabber (Silicon Software ) and I managed to show the grabbed images from a pointer in memory buffer using OpenCV.
Now I want to create an application (Win32) and place the openCV window as a child window of the main application window.
Anybody has any idea?
I found the answer, it was soo easy. Just couple of lines of code.
here it is :
cv::namedWindow("test",cv::WINDOW_AUTOSIZE);
hWnd2 = (HWND) cvGetWindowHandle("test");
hParent = ::GetParent(hWnd2);
::SetParent(hWnd2, hParent);
::ShowWindow(hParent, SW_HIDE);
if you want your own window, it's probably better, to skip the whole highgui stuff,
and do your own blitting as well.
have a look at the src of cvShowImage(), highgui/src/window_w32.cpp,l 1384, to see, what they're doing here
Don't know exactly what you mean by "child window", but you could grab the pixel information from OpenCV's IplImage format and convert it to whatever format you need it for your window.
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.
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()