News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Screen shot to jpg

Started by Magnum, December 25, 2012, 04:11:29 PM

Previous topic - Next topic

Magnum

I could not find any code in assembly that simulated a print screen and saved it as a jpeg, and  but found one in C


#include <windows.h>
#include <stdio.h>
#include <gdiplus.h>

using namespace Gdiplus;


int GetEncoderClsid(WCHAR *format, CLSID *pClsid)
{
unsigned int num = 0,  size = 0;
GetImageEncodersSize(&num, &size);
if(size == 0) return -1;
ImageCodecInfo *pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
if(pImageCodecInfo == NULL) return -1;
GetImageEncoders(num, size, pImageCodecInfo);
for(unsigned int j = 0; j < num; ++j){
if(wcscmp(pImageCodecInfo[j].MimeType, format) == 0){
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j;
}   
}
free(pImageCodecInfo);
return -1;
}

int GetScreeny(LPWSTR lpszFilename, ULONG uQuality) // by Napalm
{
    ULONG_PTR gdiplusToken;
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

HDC hdcScreen  = CreateDC("DISPLAY", NULL, NULL, NULL);
HDC hdcCapture = CreateCompatibleDC(hdcScreen);
int nWidth     = GetDeviceCaps(hdcScreen, HORZRES),
    nHeight    = GetDeviceCaps(hdcScreen, VERTRES),
    nBPP       = GetDeviceCaps(hdcScreen, BITSPIXEL);

LPBYTE lpCapture;
BITMAPINFO bmiCapture = { {
sizeof(BITMAPINFOHEADER), nWidth, -nHeight, 1, nBPP, BI_RGB, 0, 0, 0, 0, 0,
} };
HBITMAP hbmCapture = CreateDIBSection(hdcScreen, &bmiCapture,
DIB_PAL_COLORS, (LPVOID *)&lpCapture, NULL, 0);
if(!hbmCapture){
DeleteDC(hdcCapture);
DeleteDC(hdcScreen);
GdiplusShutdown(gdiplusToken);
return 1;
}

int nCapture = SaveDC(hdcCapture);
SelectObject(hdcCapture, hbmCapture);
BitBlt(hdcCapture, 0, 0, nWidth, nHeight, hdcScreen, 0, 0, SRCCOPY);
RestoreDC(hdcCapture, nCapture);
DeleteDC(hdcCapture);
DeleteDC(hdcScreen);

CLSID imageCLSID;
Bitmap *pScreenShot = new Bitmap(hbmCapture, (HPALETTE)NULL);
EncoderParameters encoderParams;
encoderParams.Count = 1;
encoderParams.Parameter[0].NumberOfValues = 1;
encoderParams.Parameter[0].Guid  = EncoderQuality;
encoderParams.Parameter[0].Type  = EncoderParameterValueTypeLong;
encoderParams.Parameter[0].Value = &uQuality;
GetEncoderClsid(L"image/jpeg", &imageCLSID);
int result = (pScreenShot->Save(lpszFilename, &imageCLSID, &encoderParams) == Ok);
delete pScreenShot;
DeleteObject(hbmCapture);
GdiplusShutdown(gdiplusToken);
return result;
}

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
return GetScreeny(L"screeny.jpg", 75);

Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

dedndave

credit ?
do you know who wrote it ?

Magnum

You can see some of the authors in the include files and Napalm wrote the main code.

At 4608 bytes in size it's pretty good for a C++ program.

The jpgs are large at 165K.


Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

qWord

MREAL macros - when you need floating point arithmetic while assembling!

Magnum

I just looked a second time.

No msdn in any of my files unless you are counting a makefile.  :t

Andy
Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

Donkey

#5
Taking a quick look at capturing the screen and saving it as a jpeg, I decided it was best to start from "scratch" and not try to translate someone else's code. Here's what I came up with, the save code is from an old gdip example I wrote a few years back.

Note that I do not use MASM so the code is in GoAsm syntax, I don't translate to MASM so you'll have to get help somewhere else if you can't follow it. You will need my header files to assemble the RadASM 3 project. I have included an executable for you to test to make sure it meets your needs before translating it. Very little error trapping is included though it works reliably on My Win7 Pro box.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Vortex

Hi Donkey,

Your tool works fine on my XP Sp3 :t

Donkey

Thanks Vortex,

I updated the attachment to add comments and get rid of the dialog. There was only the one download as far as I know.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

jj2007

Works fine, Edgar :t
Maybe the release version should wait for the user pressing a hotkey...

Donkey

Quote from: jj2007 on December 26, 2012, 07:20:25 AM
Works fine, Edgar :t
Maybe the release version should wait for the user pressing a hotkey...

Hi Jochen,

Its just an example to show one way it can be done, Magnum can decide if its what he needs and how to trigger it, for me my interest in it is pretty much over as it was a simple program that didn't really pose any challenge.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Donkey

Actually there was something I left out, when dealing with JPEG files you should set the quality of JPEG you want. In this version there is a variable "Quality" in the data section that when set between 0 and 100 will set the resulting JPEG quality. In the example it is set to 50 which results in a ~155K file on my system, 100 will result in a ~1.1MB file.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Magnum

Thanks for the code.

I plan on adding the ability to take a prt scr at an adjustable time interval.

Take care,
                   Andy

Ubuntu-mate-18.04-desktop-amd64

http://www.goodnewsnetwork.org

dedndave

very nice Edgar   :t

there should also be a way to set the "smoothing" on jpeg's
in days of old, i seem to recall a 3rd setting (not color depth), but i don't remember   :P
probably had something to do with the Hoffman tables
it took forever to compress or decompress a jpeg on an XT - lol
i probably still have some of the old 16-bit programs for jpegs if i look hard enough

hfheatherfox07

Hello I came across something today and I remembered this thread....
Have A look at the attachment in the second post here:

http://masm32.com/board/index.php?topic=835.0

:t
Your code and your skills will be assimilated. Your programming language is irrelevant.
We are the ASM Borg and you will become part of us. Compile and be assembled.

Gunther

Well written code, Edgar. :t Thank you for that.

Gunther
You have to know the facts before you can distort them.