Taking Screenshot of Full Desktop with Qt5 - c++

I figured out how to take a screenshot of the desktop today with Qt5 from an included example which gets the primary screen, grabs it, and then saves it.
I'm translating the code from Python without testing so if there's a small syntax error, then yeah you know. So I can easily take a screenshot of the primary screen with:
QApplication a(argv, argc);
QScreen *screen = a.primaryScreen();
QPixmap screenshot = screen->grabWindow(0);
screenshot.save('screenshot.png', 'png');
This will (obviously) take a screenshot of the primary monitor. The problem is I need to take a screenshot of all of the monitors. So I came up with this:
QList<QScreen*> screens = a.screens();
QScreen *screen;
QPixmap screenshot;
for(int i = 0; i < screens.length(); i++){
screen = screens.at(i);
screenshot = screen->grabWindow(0);
screenshot.save(QString::number(i) + ".png", 'png');
}
//takes and saves two screenshots on my end
This finds both of my monitors but the saved images are all a screenshot of the primary monitor and I can't figure out how to get the others. I've been playing with this for a few hours now and still can't figure it out. So can someone help me out?

I figured out a simple fix for this problem. When I was looking through the documentation recently, I found that the 'getWindow' method had default parameters of
(x = 0, y = 0, width = -1, height = -1)
So no matter what screen I called the getWindow method with, it kept giving me the same geometry. So to fix this, it's simply:
//Screen geometry
QRect g = screen->geometry();
//Take the screenshot using the geometry of the screen
QPixmap screenShot = screen->grabWindow(0, g.x(), g.y(), g.width(), g.height());

Related

Cocos2d-X animation blinking red

When i start an Animation (using the code below), allof the sprites.png's are misscolored (red) except for the starting picture, so it looks like it's blinking red.
I wondered if anyone know what causes this?
I use TexturePacker to make the sprite sheet, with the settings: Pixel Format: RGBA8888, Texture Format: PVR+zlib.
Here is the code for my animation, which I have in my Player class and it's iniated when it's created:
FileUtils::getInstance()->addSearchResolutionsOrder("indian_spreedsheet/HD");
// load and cache the texture and sprite frames
auto cacher = SpriteFrameCache::getInstance();
cacher->addSpriteFramesWithFile("indian_walk.plist");
#include <sstream>
// load all the animation frames into an array
const int kNumberOfFrames = 4;
Vector<SpriteFrame*> frames;
for (int i = 1; i < kNumberOfFrames; i++)
{
std::string s = std::to_string( i );
SpriteFrame* aFrame =
cacher->getSpriteFrameByName( "indian_walk" + s + ".png" );
frames.pushBack(aFrame);
}
// play the animation
auto animation = Animation::createWithSpriteFrames(frames, 0.40f);
animation->setRestoreOriginalFrame(true);
moveAnimate = Animate::create(animation);
moveAnimate->retain();
I work in Visual Studio, with Cocos2d-x version 3.3.
I can include pictures of the event if anyone feel it's needed.
Thanks in advance!
check your "indian_walk.png", is there some red color on it?
as described in the official document:
Is there a trial version?
Yes, there is a 7-day trial mode with
unrestricted features. After that period TexturePacker goes into
Essential mode. It is restricted to basic features. If you use Pro
features in this mode, you will get a warning before publishing and
some of your sprites will turn red in the output.
link is here

Zoom the picture within the picturebox of Visual Studio C++

I have a problem about zooming into the picture displayed in a picturebox Picture Control. The expected result is similar to the following link: A scrollable, zoomable, and scalable picture box. The picture is zoomed in within the picturebox. Note the picturebox is zoomed in.
However, this can be implemented in .NET Framework 2.0. I have searched some information on the Internet, but none used C/C++ Windows API of Visual Studio. How can I zoom a picture within a the picturebox Picture Control when I work on Windows Forms of Visual Studio C++ 2010. Thank you for anyone's reply.
I have faced the same issue and I searched a lot but didn't get the solution in cpp(freely available in c# and VB).
So finally i decided to implement it by my own. If your Image size is bigger than the picture box then only 1 level of zoom(original size of image) you can do with my implementation.
I will help you with the implementation. In current implementation
if you do left click of mouse it will ZoomIn and
if you do right click of mouse it will ZoomOut.
Assuming you have read the image file(.png, .jpg, .bmp, ...) in bmpPicture.
Bitmap ^bmpPicture = gcnew Bitmap(strFilename);// if not it can help
private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {
MouseEventArgs ^ms = (MouseEventArgs^)e;
if (ms->Button == System::Windows::Forms::MouseButtons::Left)
{
int pbx = ms->X;
int pby = ms->Y;
int img_x = (pbx * mwidth / pbwidth * 1.0f);
int img_y = (pby * mheight / pbheight * 1.0f);
pictureBox1->Location = System::Drawing::Point(pbx - img_x, pby - img_y);
pictureBox1->SizeMode = PictureBoxSizeMode::AutoSize;
pictureBox1->Image = bmpPicture;
}
else if(ms->Button == System::Windows::Forms::MouseButtons::Right)
{
int pbx = ms->X;
int pby = ms->Y;
pictureBox1->Location = System::Drawing::Point(0, 0);
pictureBox1->SizeMode = PictureBoxSizeMode::StretchImage;
pictureBox1->Image = bmpPicture;
}
}
Hope this will help you...
Without any source code, it is difficult to comment.
StackOverflow is for problem solving; we can troubleshoot your starting code and get it working and during the course of which we can also fine tune design.
Anyway, I would like to give some pointers:
One or two picture boxes: entirely depends on your UI requirement.
Showing full or partial region of an image: StretchBlt is a powerful
API that can be used to achieve. See MSDN documentation for details.
Which class to use for picture box: if MFC is being used, then
derive a class from CStatic, overload the OnPaint() and use
StretchBlt to paint the image.
Classes to use for image: CBitmap, CImage, Gdiplus::Image.
Each have their own pros and cons.
If transparent images/PNGs are to be suported, better go for CImage or Gdiplus::Image.
You can get enough info here: CBitmap CImage Gdiplus::Image
Hope this would give you some starter.

Resizing image with SDL_gfx at specific size C++

I am using SDL_gfx trying to resize an image but i can't really get it done.
The case is that I have an :
SDL_Surface* screen = SDL_SetVideoMode(0,0,32,SDL_FULLSCREEN);
and I also have a:
SDL_Surface* back_img = SDL_Load("back.jpg");
As you can see, I set the videomode to full screen, so the size of the screen surface will vary from pc to pc. I want to find a way to make back_img FIT on screen but i have two major problems.
screen->w and screen->h return a false value!
I can't resize the image to the specific size I want.
Any help?
Your screen->w values return 0 because you set them to zero with SDL_SetVideoMode(0,0,...
Set them to some proper values like SDL_SetVideoMode(800,600,32,SDL_FULLSCREEN); you can change them later by calling the function again with different parametes.
Note; setting SDL fullscreen will automatically resize and stretch the image to fit the screen
you can use SDL_VideoInfo to get the current resolution. See the code below:
const SDL_VideoInfo *info = SDL_GetVideoInfo();
SDL_Surface *screen = SDL_SetVideoMode( info->current_w, info->current_h, 32, SDL_FULLSCREEN );
Now your screen's resolution will match every PC's resolution.

Changing image bitmap or drawing bitmap on image causes image to disappear

I'm using two bitmaps to draw graphs on them. After drawing I need to show bitmap pictures in two Images. Assigning bitmap to Image or drawing bitmap to Image sometimes causes Image to disappear (you can see form background).
I have tried this:
Image->Picture->Bitmap->Assign(bitmap1);
Image2->Picture->Bitmap->Assign(bitmap2);
Image->Picture->Graphic = bitmap1;....
Image->Canvas->Draw(0,0,bitmap1);....
Image->Picture->Bitmap->Canvas->Draw(0,0,bitmap1);
If I don't have Sleep(100) between Image and Image2 redrawing, Image2 isn't visible for the most of the time. Also adding Image2->Refresh helps, but the issue still sometimes occurs to both Images.
If I save created bitmaps or Images to .jpeg files, all .jpeg images are correct and none of them are empty. Also Image->height, Image->picture->bitmap->height and width is always correct.
Can anyone tell me, what I'm doing wrong?
After a while I realised, that bitmaps and Images, which I saved, weren't all correct. If I was unable to see picture, it wasn't fully drawn. There were no errors, it occurred randomly, but I found out, that once program started to ignore my drawing commands, it didn't draw anything until the end of the function, which does drawing.
So - to check, if I can still draw, before assigning bitmap to Image, I did this:
Image3->Canvas->Pixels[y][0] = clRed;
TColor test = Image3->Canvas->Pixels[y][0];
Image3->Canvas->TextOut(y, 0, Device1->name);
TColor test2 = Image3->Canvas->Pixels[y][0];
if(test == test2)
{
imageUpdated = false;
delete Image3;
return;
}
Image->Picture->Graphic = Image3;
imageUpdated = true;
It means - I changed one pixel red, and after that printed over text, which should make that changed pixel white. Based on that I checked, if the color changed (was able to change pixel color and print text).
I really don't know reason, why it sometimes starts to ignore drawing commands, but I hope, that if someone runs into the same issue as I did, this answer might help him/her.

Qt Screen Resolution Splash Screen

I have a splash screen image that I display with splash.showFullScreen() but it doesn't re size it to the screen resolution so it either comes out tiled or to large depending on the display. I have tried everything I can think of but nothing works. This might sound like a stupid question which it probably is but I can't find the answer so if any can just help me with this? If it makes a difference I use a QPixmap named pixmap for the splash image. By the way I want the image to be stretched to the screen resolution.
You should scale the pixmap to the size of the screen with QPixmap::scaled(). You can get the screen resolution by calling QDesktopWidget::screenGeometry(). The desktop widget can be obtained by QApplication::desktop().
You can try something like this:
QDesktopWidget* desktopWidget = qApp->desktop();
QRect screenGeometry = desktopWidget->screenGeometry();
int screenWidth = screenGeometry.width();
int screenHeight = screenGeometry.height();
QPixmap pixmapForSplash = yourPixmap.scaled(screenWidth, screenHeight);
QSplashScreen splashScreen(pixmapForSplash);
(I'm sorry, I can not check this, because I do not have a development environment on this computer... I hope it is correct.)
I think you should call resize() method for your splash screen by the size of the available desktop geometry that you can get using QDesktopWidget::availableGeometry method. The QApplication::desktop() function is used to get an instance of QDesktopWidget.
slpashScreen.resize(QApplication::desktop()->avaiableGeometry().size());
If you use a QLabel to display the image, make sure the label is in a layout that will cause it to fill the entire parent widget and set the label to scale its contents using setScaledContents(true).