Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6209617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:01:07+00:00 2026-05-24T06:01:07+00:00

Working on a Windows 7 servicepack 1 environment with cygwin gcc compiler. The following

  • 0

Working on a Windows 7 servicepack 1 environment with cygwin gcc compiler.

The following program is supposed to take a screenshot and save the file as a .bmp to the file specified. It compiles fine and seems to give a .bmp file in the desired directory but when run seems to create only a 1kb file with no screenshot data in it. If the while loop is given a starting value of while(1), it gives the “Unable to Create Bitmap File” error. I am new to this kind of programming and cannot seem to see why this is.

Any ideas?

(has to be compiled with -lgdi32)

code:

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

void TakeScreenShot(char* filename);

int main()
{
   TakeScreenShot("c:\\Screenshot.bmp");
   return 0;
}

//
// Side Effects:N/A
//
//This code is copyrighted and has// limited warranties.Please see http://
//   www.Planet-Source-Code.com/vb/scripts/Sh
//   owCode.asp?txtCodeId=10754&lngWId=3//for details.//**************************************
// 

void TakeScreenShot(char* filename)
{
   keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
   keybd_event(VK_SNAPSHOT, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
   HBITMAP h;        

   OpenClipboard(NULL);
   h = (HBITMAP)GetClipboardData(CF_BITMAP);
   CloseClipboard();
   HDC hdc=NULL;

   FILE*fp=NULL;
   LPVOID pBuf=NULL;
   BITMAPINFO bmpInfo;
   BITMAPFILEHEADER bmpFileHeader;

   do
   {
      hdc=GetDC(NULL);
      ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
      bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
      GetDIBits(hdc,h,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);

      if(bmpInfo.bmiHeader.biSizeImage<=0)
         bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
      if((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)
      {
         MessageBox( NULL, "Unable to Allocate Bitmap Memory", "Error", MB_OK|MB_ICONERROR);
         break;
      }
      bmpInfo.bmiHeader.biCompression=BI_RGB;
      GetDIBits(hdc,h,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);

      if((fp = fopen(filename,"wb"))==NULL)
      {
         MessageBox(NULL, "Unable to Create Bitmap File", "Error", MB_OK|MB_ICONERROR);
         break;
      }

      bmpFileHeader.bfReserved1=0;
      bmpFileHeader.bfReserved2=0;
      bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
      bmpFileHeader.bfType='MB';
      bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);

      fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
      fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
      fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
   }
   while(0);

   if(hdc)
      ReleaseDC(NULL,hdc);

   if(pBuf)
      free(pBuf);

   if(fp)
      fclose(fp);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:01:07+00:00Added an answer on May 24, 2026 at 6:01 am

    The program apparently saves bitmap data it gets from the clipboard. If there is no data on the clipboard, I assume it only saves an empty bitmap. And it writes a file called “Screenshot.bmp”, not a .png file.

    To put bitmap data on the clipboard, I assume you must press the print screen button first. This saves a screenshot to the clipboard. Now the program can be used to save this clipboard data to a file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in Windows MFC application..In my design am displaying the file details
I am working in windows environment and I need to open a new instance
The following is working under windows for reading csv files line by line. f
working in windows environment, System configuration Winxp, Wamp in local disk E, Strawberry PERL
I'm working on Windows. and I want to monitor of system file.(access, open, write,
I have modified a working Windows service that had always been starting beforehand. After
Occasionally working in Windows Vista the O.S. will desaturate the screen, rendering all colors
i'm working on windows mobile app (c#, .net 2.0). i would like to make
I am working on Windows Server 2003 (IIS6), which has two asp.net sites running
I m working on windows form application. I need to show amount in words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.