The MASM Forum

Microsoft 64 bit MASM => Examples => Topic started by: Vortex on May 29, 2018, 04:29:54 AM

Title: Handling bitmaps
Post by: Vortex on May 29, 2018, 04:29:54 AM
Here is a console application saving the desktop to a bitmap file. No any COM or GDI+ techniques and just plain API.

Steps to save the desktop to a bitmap :

- Get the width and height of the desktop
- Get the device context ( DC ) of the desktop and create a compatible DC
- Create a compatible bitmap.
- Copy the image to the new DC.
- Fill the BITMAPINFOHEADER and BITMAPFILEHEADER structures.
- Get the bits of the bitmap to be saved to disc.
- Save the bitmap structures and color data to disc.

Usage :

Code: [Select]
SaveDesktop.exe test.bmp
The default bit depth is 24.

Any third parameter to set the bit depth to 32 :

Code: [Select]
SaveDesktop.exe test2.bmp 32
Title: Re: Handling bitmaps
Post by: felipe on May 29, 2018, 11:58:19 AM
 :greenclp: This is great! I was wishing to have an app to make snapshots of my desktop, but i didn't wanted bloatware! You are a genius, thanks for sharing this!  :biggrin:

Title: Re: Handling bitmaps
Post by: 0000 on May 29, 2018, 12:12:17 PM
It works well.... however, when I run it from a batch file it captures the command window in front of the intended target.
How would one run the program and avoid such instances?
Title: Re: Handling bitmaps
Post by: felipe on May 29, 2018, 12:18:55 PM
Open a cmd, put it in a corner of the screen and then run the program (using the options needed)  :idea:

This program is also very useful to play a little pranks to my girl (who knows little about PCs...) When the .bmp is full sized you can move the mouse above of the image but nothing seems to work when you do clicks on the icons!  :badgrin:
Title: Re: Handling bitmaps
Post by: 0000 on May 29, 2018, 12:22:11 PM
Here is what it looks like using "run" instead of the cmd prompt. (converted it to .gif to reduce filesize)

Perhaps if you included a timer into the mix to allow closing the command window.....   ;)
But other than that I think it's a great little program.

I think if the timer was added, it could be a production-ready, commercially viable program.    :biggrin:
Title: Re: Handling bitmaps
Post by: hutch-- on May 29, 2018, 12:31:17 PM
Trick is with screen capture is to hide the calling app first, grab the screen then show the caller again.
Title: Re: Handling bitmaps
Post by: felipe on May 29, 2018, 12:32:36 PM
This is what i get when i simple resize the command line (cmd.exe). Sorry for the big image Hutch you can delete this later...
Title: Re: Handling bitmaps
Post by: 0000 on May 29, 2018, 12:36:54 PM
...hide the calling app first, grab the screen then show the caller again.

Sounds like you've done that before.  :t

A slight delay in the capture could also allow the caller to close. A few milliseconds should be plenty.  :bgrin:
I might just try to do that myself.... (but 32 bit...)


@ felipe...
but the cmd window still muddies up an otherwise good screen capture. That was my whole point. 



Title: Re: Handling bitmaps
Post by: 0000 on May 29, 2018, 12:43:56 PM
Or possibly add a little code to check when the calling program closes.
just after closing the command window for instance, the screen capture would take place.

Of course if a timer were use, it could lead to recording an animation of sorts. Possibly for software demo??   :idea:

This topic is bringing so many possibilities to mind.
Title: Re: Handling bitmaps
Post by: felipe on May 29, 2018, 12:46:31 PM
@ felipe...
but the cmd window still muddies up an otherwise good screen capture. That was my whole point.

Well, you know how it is, you can't satisfy to all... ;)

But it surely wholes my needs of screenshots  :icon14:. If i will have to choose (actually i already did  :biggrin:) between this nice assembly program and some bloatware...guess what... :idea:
Title: Re: Handling bitmaps
Post by: 0000 on May 29, 2018, 12:48:46 PM
If i will have to choose (actually i already did  :biggrin:) between this nice assembly program and some bloatware...guess what... :idea:

Oh, you are so right about that.   :t 

Such a simple little program is more than sufficient. Good job, Vortex.
Title: Re: Handling bitmaps
Post by: Vortex on May 30, 2018, 03:05:53 AM
Thanks for the feedback. The console window is now hidden while capturing the screen :

Code: [Select]
invoke  GetConsoleWindow
mov     hwnd,rax
invoke  ShowWindow,rax,SW_HIDE
invoke  Sleep,300
.
.
.
invoke  ShowWindow,hwnd,SW_SHOW
ret

New upload at the top.
Title: Re: Handling bitmaps
Post by: felipe on May 30, 2018, 04:10:46 AM
Thanks again... :t
Title: Re: Handling bitmaps
Post by: Vortex on May 30, 2018, 04:17:40 AM
Hi felipe,

About your image : sorry, if you encountered a problem could you please help me to reproduce the issue? Thanks.
Title: Re: Handling bitmaps
Post by: felipe on May 30, 2018, 04:41:03 AM
I don't understand vortex, i have found no problems...You mean in the future?  :idea:
Title: Re: Handling bitmaps
Post by: Vortex on May 30, 2018, 05:01:44 AM
Sorry felipe. My apologies. I could not understand the image you posted. What are those symbols? Everything is fine. Sorry again.
Title: Re: Handling bitmaps
Post by: 0000 on May 30, 2018, 05:55:06 AM
Thanks for the feedback.....
New upload at the top.

You're the man,  Vortex.    :t. Thank you very much, your correction is exactly what I had in mind.

I'll check it out.  when I get home
Title: Re: Handling bitmaps
Post by: 0000 on May 30, 2018, 07:26:22 AM
Quote from: zedd151

I'll check it out.  when I get home

yup, it works very well now.   :bgrin:

attached jpg reduced in size 50%. The full bmp is perfect in every respect.
Title: Re: Handling bitmaps
Post by: felipe on May 30, 2018, 10:32:20 AM
Oh i see Vortex, it's just i didn't wanted to show my desktop (is a mess, you don't want to see it  :redface:), so i did a snapshot of a stupid program that i did before (not trying to promete it, really... :idea:). Yes this app is real fantastic, it not only make snapshots of the desktop, it can do snapshots of running apps (as zedd151 show us too)!  :greenclp:
Title: Re: Handling bitmaps
Post by: 0000 on May 30, 2018, 01:05:57 PM
.... Yes this app is real fantastic, it not only make snapshots of the desktop, it can do snapshots of running apps...

Yes, it is perfect now.  H e knew exactly the fixes I was talking about.   :t


Quote from: felipe
of a stupid program that i did

Nothing is stupid if you gained experience from it. And besides it was one of the catalysts for hutch opening the Game Development forum
Title: Re: Handling bitmaps
Post by: 0000 on July 01, 2018, 10:42:36 PM
@ Vortex.
 
I made an sfx (self-extracting) executable for your screen capture tool. Now a shortcut for it can be placed on the taskbar. (That is how I use it)
 
The sfx executable contains your original program and a batch file to run it. The resulting .bmp is placed on the %userprofile%\Desktop.
 
 To use:
 
Click on the 'VortexScrCap.exe' file and it unzips in your Temp folder, runs the batch file which in turn runs your program to make a screen capture. After the screen capture is done, it automatically deletes the files from the Temp folder. - and places the resulting .bmp on the current users Desktop.   8)
 
And thans again, this is a very handy tool.
Title: Re: Handling bitmaps
Post by: Vortex on July 01, 2018, 10:53:41 PM
Hi zedd,

Thanks but the tool didn't save the captured image in my system. Could I propose some small modifications?

Code: [Select]
SaveDesktop.exe "%USERPROFILE%\desktop\capture.bmp"
The double quotes should solve the problems in Windows XP. For example :

Code: [Select]
C:\Documents and Settings\Administrator\Desktop
Title: Re: Handling bitmaps
Post by: 0000 on July 01, 2018, 10:59:09 PM
Quote
C:\Documents and Settings\Administrator\Desktop

What OS are you runnin? xp?
 
I only Have Windows 7 and WIndows 10 here. I made the sfx so I could have an easy shortcut on my taskbar. I have no way of testing it on older systems
 
I could post another with the double quotes lemme see...
 
Okay here it is, try this one...
Title: Re: Handling bitmaps
Post by: Vortex on July 01, 2018, 11:01:59 PM
Hi zedd,

Yes, my operating system is XP but no worries. I can try your SFX archive on Windows 7. Here is another version of my application rebuilt as Windows GUI application. No any annoying console window.
Title: Re: Handling bitmaps
Post by: 0000 on July 01, 2018, 11:09:29 PM
Doesn't work for me. Windows 7 Ultimate SP1   :(
 
I even tried running it from a batch file as in the previous version...
 
Nope, I take that back it runs from the batch file as the previous. I overlooked specifying the output file name.  ::)
 
No GUI though. Did you post the right version? I'm looking at your source code right now...
Title: Re: Handling bitmaps
Post by: 0000 on July 01, 2018, 11:14:34 PM
Must be a mistake on your part...
 
Code: [Select]
    lea     rsi,buffer
    invoke  ParseCmdLine,rsi
    cmp     rax,2
    jnb     @f
    invoke  StdOut,ADDR message  <--- not Window GUI.
    ret

 :(
Title: Re: Handling bitmaps
Post by: Vortex on July 01, 2018, 11:17:13 PM
Here is how I am testing :

Code: [Select]
H:\coding\savedesktop\savedesktop.exe H:\coding\savedesktop\desktop.bmp
The application is built as GUI but it has no any graphical interface like a dialog box. An easy trick :

Changed from :

Code: [Select]
\masm32\bin\polink /SUBSYSTEM:CONSOLE /LARGEADDRESSAWARE /ENTRY:start SaveDesktop.obj ParseCmdLine.obj
to

Code: [Select]
\masm32\bin\polink /SUBSYSTEM:WINDOWS /LARGEADDRESSAWARE /ENTRY:start SaveDesktop.obj ParseCmdLine.obj
This method avoids the flashing console screen.

Title: Re: Handling bitmaps
Post by: Vortex on July 01, 2018, 11:19:04 PM
Hi zedd,

Code: [Select]
invoke  StdOut,ADDR message  <--- not Window GUI.
You can omit that line. It has no any meaning for the version without console.
Title: Re: Handling bitmaps
Post by: 0000 on July 01, 2018, 11:28:51 PM
Duh! WHen you said GUI, I was expecting a window.   :redface:
 
So it is still a console 'type' program that still needs the CommandLine arg...  Now I got it. I know I didn't see CreateWindow anywhere,
I thouhgt you posted the wrong version inadvertantly. You just removed the console window from the mix.
 
So then it works as advertised.   :t
Title: Re: Handling bitmaps
Post by: 0000 on July 01, 2018, 11:43:38 PM
I just tried using the new version in an sfx archive. It captures not the command window from your program but now the command window from the batch file.  :(   I think I like the 'b' version better.
Title: Re: Handling bitmaps
Post by: Vortex on July 02, 2018, 05:32:08 AM
Hi zedd,

You can write a simple Visual Basic script to avoid the console window displayed by batch files :

Capture.vbs :

Code: [Select]
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("H:\masm32\SaveDesktop.exe H:\masm32\capture.bmp")
Set objShell = Nothing

Also, you have to take in account the paths with space characters in the script files. An example :

https://stackoverflow.com/questions/28379207/launch-program-from-vbscript?rq=1