Thread: Creating and saving images

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32

    Creating and saving images

    I have a a short question. I want to be able to take a screenshot of the desktop, without manually pressing print screen and save it to a file.

    Most of the resources that I have searched to do that either have external libraries or a large amount of unnecessary code I'd need to shift through. I'd rather *attempt* to keep it small, and in a single executable.

    Does anyone know any good resources to create either JPG, bitmap, or PNG images? (although, i'd rather it be JPG because PNG and bitmap images are pretty huge.)

    I'm running on Windows if that changes anything.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a reason they have libraries that do it for them, or huge amounts of code. No, you can't magically come up with some short way that they haven't thought of before to do it. Otherwise, other smart people out there would have already done it. The best you are going to get is to use one of the libraries you've already discovered, or to write your own from scratch (huge amounts of code).

    I'll leave the png vs jpg debate to someone who cares enough to argue it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There are lots of ready to use programs out there that can take screen shots for you...

    Get your Googling hat on and look for ...

    IrfanView
    Neopaint
    WMSnap
    SnagIt
    Screen Grab
    Fast Stone
    7Capture
    ...and dozens more

    Plus if you're on windows 7 it has it's own snipping tool supplied with the OS...

    I get that you may be looking for a learning experience but this one has been done to death and capturing the desktop, all it's icons and open windows is no small task. Plus I think you'll find that software taking screenshots without the user's interraction is going to realy tick a lot of people off...

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    at Quzah, I was just wondering if any shorter way has been made....I know there are a few windows functions which make it easier...but to be honest, I'm still an amateur, I don't know exactly which ones nor how to use them.

    at Tater, I'm just trying to find something to write up, and I found that this would be kind of interesting that's why I didn't use a ready-made program. Also, I wont be "taking screenshots without the user's interaction" because...well...my stuff doesn't get published...so I don't really have to worry about that.

    Thanks anyways for the fast replies.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm thinking that if you post in the Windows Programming forum, you will get better answers than here...
    Basically it involves catching the windows screen buffer with GDI...

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'm confused. Isn't getting a screenshot as easy as GetDC(NULL) followed by BitBlt()? I may be missing something here, but I think the only part of this that requires any effort is JPEG encoding the pixels.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bithub View Post
    I'm confused. Isn't getting a screenshot as easy as GetDC(NULL) followed by BitBlt()? I may be missing something here, but I think the only part of this that requires any effort is JPEG encoding the pixels.
    Yep, that's pretty much it. GDI is the Windows "Graphic Drawing Interface" which includes GetDC() and BitBlt(). He'll need a jpg library to save the file... or he can save BMPs and convert them later...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    Quote Originally Posted by bithub View Post
    I'm confused. Isn't getting a screenshot as easy as GetDC(NULL) followed by BitBlt()? I may be missing something here, but I think the only part of this that requires any effort is JPEG encoding the pixels.
    Thanks for the reply. But after a few hours of googling (because not only did I want to take a screenshot, but save it too), and messing with a bit of code, I managed to put this all together...problem is...it doesn't work, and I can't seem to guess why. I've attempted to debug it but all I get is just a 3MB bitmap file thats floodfilled with black. Heres the code.
    Code:
    	HDC Screenshot = GetDC(NULL);
    
    	int ResX = GetSystemMetrics(SM_CXSCREEN);
    	int ResY = GetSystemMetrics(SM_CYSCREEN);
    
    	HDC MemDesktop = CreateCompatibleDC(Screenshot);
    	HBITMAP tmpbitmap = CreateCompatibleBitmap(MemDesktop, ResX, ResY);
    
    	HBITMAP bitmap = (HBITMAP)SelectObject(MemDesktop, tmpbitmap);
    
    	BitBlt(MemDesktop, 0,0, ResX, ResY, Screenshot, 0,0, SRCCOPY);
    
    	BITMAPINFO BitInfo;
    
    	memset(&BitInfo, 0, sizeof(BITMAPINFO));
    
    	BitInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    	BitInfo.bmiHeader.biWidth = ResX;
    	BitInfo.bmiHeader.biHeight = ResY;
    	BitInfo.bmiHeader.biPlanes = 1;
    	BitInfo.bmiHeader.biBitCount = 16;
    	BitInfo.bmiHeader.biCompression = BI_RGB; //BI_JPEG;
    	BitInfo.bmiHeader.biSizeImage = (BitInfo.bmiHeader.biWidth * BitInfo.bmiHeader.biHeight) * 2;
    
    	void *bits = malloc(BitInfo.bmiHeader.biSizeImage);
    	GetDIBits(MemDesktop, bitmap, 0, BitInfo.bmiHeader.biHeight, bits, &BitInfo, DIB_PAL_COLORS);
    	HANDLE bmfile = CreateFile("image.bmp", GENERIC_READ | GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    
    	BITMAPFILEHEADER bitheader;
    
    	bitheader.bfReserved1 = 0;
    	bitheader.bfReserved2 = 0;
    	bitheader.bfType = 0x4d42;
    	bitheader.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
    	bitheader.bfSize = bitheader.bfOffBits + BitInfo.bmiHeader.biSizeImage;
    
    	DWORD bytes;
    	WriteFile(bmfile, &bitheader, sizeof(BITMAPFILEHEADER), &bytes, NULL);
    	WriteFile(bmfile, &BitInfo.bmiHeader, sizeof(BITMAPINFOHEADER), &bytes, NULL);
    	WriteFile(bmfile, bits, BitInfo.bmiHeader.biSizeImage, &bytes, NULL);
    
    	CloseHandle(bmfile);
    	free(bits);
    	DeleteDC(MemDesktop);
    	DeleteDC(Screenshot);
    	DeleteObject(bitmap);
    	DeleteObject(tmpbitmap);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating images using C# code
    By synyyy in forum C# Programming
    Replies: 1
    Last Post: 01-26-2011, 11:27 PM
  2. Replies: 1
    Last Post: 12-01-2009, 08:24 AM
  3. creating images with c++
    By pastitprogram in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2008, 07:43 AM
  4. Button Images and Saving
    By tyler4588 in forum Windows Programming
    Replies: 2
    Last Post: 12-10-2004, 09:20 PM
  5. Creating a VCD to Show Images
    By Davros in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2003, 04:02 PM