よろしくお願いします。
以前以下のサンプルを組み合わせたスクリーンキャプチャのプログラムを改良しようと取り組んでいるのですが、うまくいきません。
具体的にどのようにしようとしているかと言うと、現在は
スクリーンキャプチャ→exeファイルと同階層にbmp形式で保存→保存したファイルを読み込んでjpgに変換→exeファイルと同階層に保存
としているのですが、この流れからbmp形式で保存する部分を削除したいのです。
アドバイスやご指摘がありましたらよろしくお願いします。
↓参考にしたサンプルコード
http://www.geocities.jp/katayama_hirofumi_mz/win32/savebmp.htm
http://www.geocities.jp/katayama_hirofumi_mz/win32/savejpeg.htm
↓自分のコード
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tchar.h>
#ifdef __cplusplus
#define TRUE 1
#define FALSE 0
extern "C"
{
#endif
#define XMD_H
#include "jpeglib.h"
#include "jerror.h"
#ifdef __cplusplus
}
#endif
#pragma comment(lib, "libjpeg.lib")
#pragma comment(lib, "Vlapi.lib")
/*ローカルタイムの取得*/
char *fmttm(char *buf, struct tm *st)
{
sprintf(buf, "%d%d%d%02d%02d%02d",
st->tm_year +1900,
st->tm_mon +1,
st->tm_mday,
st->tm_hour,
st->tm_min,
st->tm_sec);
return buf;
}
/*ビットマップの取得*/
HBITMAP CreateWindowBitmap(HWND hWnd)
{
HBITMAP hbmNew;
HDC hDC, hMemDC;
HGDIOBJ hbmOld;
RECT rc;
SIZE siz;
DWORD dwError;
BITMAPINFO bi;
VOID *pvBits;
GetWindowRect(hWnd, &rc);
siz.cx = rc.right - rc.left;
siz.cy = rc.bottom - rc.top;
hbmNew = NULL;
hDC = GetWindowDC(hWnd);
if (hDC != NULL)
{
hMemDC = CreateCompatibleDC(hDC);
if (hMemDC != NULL)
{
ZeroMemory(&bi.bmiHeader, sizeof(BITMAPINFOHEADER));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = siz.cx;
bi.bmiHeader.biHeight = siz.cy;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
hbmNew = CreateDIBSection(hDC, &bi, DIB_RGB_COLORS, &pvBits,
NULL, 0);
if (hbmNew != NULL)
{
hbmOld = SelectObject(hMemDC, hbmNew);
BitBlt(hMemDC, 0, 0, siz.cx, siz.cy,
hDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hbmOld);
}
else
dwError = GetLastError();
DeleteDC(hMemDC);
}
else
dwError = GetLastError();
ReleaseDC(hWnd, hDC);
SetLastError(dwError);
}
return hbmNew;
}
typedef struct tagBITMAPINFOEX
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
} BITMAPINFOEX, FAR * LPBITMAPINFOEX;
/*ビットマップをファイルに保存*/
BOOL SaveBitmapToFile(LPCTSTR pszFileName, HBITMAP hbm)
{
BOOL f;
DWORD dwError;
BITMAPFILEHEADER bf;
BITMAPINFOEX bi;
BITMAPINFOHEADER *pbmih;
DWORD cb;
DWORD cColors, cbColors;
HDC hDC;
HANDLE hFile;
LPVOID pBits;
BITMAP bm;
if (!GetObject(hbm, sizeof(BITMAP), &bm))
return FALSE;
pbmih = &bi.bmiHeader;
ZeroMemory(pbmih, sizeof(BITMAPINFOHEADER));
pbmih->biSize = sizeof(BITMAPINFOHEADER);
pbmih->biWidth = bm.bmWidth;
pbmih->biHeight = bm.bmHeight;
pbmih->biPlanes = 1;
pbmih->biBitCount = bm.bmBitsPixel;
pbmih->biCompression = BI_RGB;
pbmih->biSizeImage = bm.bmWidthBytes * bm.bmHeight;
if (bm.bmBitsPixel < 16)
cColors = 1 << bm.bmBitsPixel;
else
cColors = 0;
cbColors = cColors * sizeof(RGBQUAD);
bf.bfType = 0x4d42;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
cb = sizeof(BITMAPFILEHEADER) + pbmih->biSize + cbColors;
bf.bfOffBits = cb;
bf.bfSize = cb + pbmih->biSizeImage;
pBits = HeapAlloc(GetProcessHeap(), 0, pbmih->biSizeImage);
if (pBits == NULL)
return FALSE;
f = FALSE;
hDC = GetDC(NULL);
if (hDC != NULL)
{
if (GetDIBits(hDC, hbm, 0, bm.bmHeight, pBits, (BITMAPINFO*)&bi,
DIB_RGB_COLORS))
{
hFile = CreateFile(pszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_WRITE_THROUGH, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
f = WriteFile(hFile, &bf, sizeof(BITMAPFILEHEADER), &cb, NULL) &&
WriteFile(hFile, &bi, sizeof(BITMAPINFOHEADER), &cb, NULL) &&
WriteFile(hFile, bi.bmiColors, cbColors, &cb, NULL) &&
WriteFile(hFile, pBits, pbmih->biSizeImage, &cb, NULL);
if (!f)
dwError = GetLastError();
CloseHandle(hFile);
if (!f)
DeleteFile(pszFileName);
}
else
dwError = GetLastError();
}
else
dwError = GetLastError();
ReleaseDC(NULL, hDC);
}
else
dwError = GetLastError();
HeapFree(GetProcessHeap(), 0, pBits);
SetLastError(dwError);
return f;
}
/*ビットマップをjpgに変換*/
BOOL SaveBitmapAsJpeg(LPCSTR pszFileName, HBITMAP hbm, INT quality, BOOL progression)
{
FILE *fp;
BITMAP bm;
struct jpeg_compress_struct comp;
struct jpeg_error_mgr jerr;
JSAMPLE * image_buffer;
BITMAPINFO bi;
HDC hDC, hMemDC;
LPBYTE pbBits;
INT nWidthBytes;
DWORD cbBits;
BOOL f;
if (GetObject(hbm, sizeof(BITMAP), &bm) != sizeof(BITMAP)){
return FALSE;
}
fp = fopen(pszFileName, "wb");
if (fp == NULL){
return FALSE;
}
comp.err = jpeg_std_error(&jerr);
jpeg_create_compress(&comp);
jpeg_stdio_dest(&comp, fp);
comp.image_width = bm.bmWidth;
comp.image_height = bm.bmHeight;
comp.input_components = 3;
comp.in_color_space = JCS_RGB;
jpeg_set_defaults(&comp);
jpeg_set_quality(&comp, quality, TRUE);
if (progression)
jpeg_simple_progression(&comp);
jpeg_start_compress(&comp, TRUE);
ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biWidth = bm.bmWidth;
bi.bmiHeader.biHeight = bm.bmHeight;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 24;
f = FALSE;
#define WIDTHBYTES(i) (((i) + 31) / 32 * 4)
nWidthBytes = WIDTHBYTES(bm.bmWidth * 24);
cbBits = nWidthBytes * bm.bmHeight;
pbBits = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, cbBits);
if (pbBits != NULL)
{
image_buffer = (JSAMPLE *)HeapAlloc(GetProcessHeap(), 0, nWidthBytes);
if (image_buffer != NULL)
{
hDC = GetDC(NULL);
if (hDC != NULL)
{
hMemDC = CreateCompatibleDC(hDC);
if (hMemDC != NULL)
{
f = GetDIBits(hMemDC, hbm, 0, bm.bmHeight, pbBits,
(BITMAPINFO*)&bi, DIB_RGB_COLORS);
DeleteDC(hMemDC);
}
ReleaseDC(NULL, hDC);
}
if (f)
{
INT x, y;
LPBYTE src, dest;
for(y = 0; y < bm.bmHeight; y++)
{
dest = image_buffer;
src = &pbBits[(bm.bmHeight - y - 1) * nWidthBytes];
for(x = 0; x < bm.bmWidth; x++)
{
*dest++ = *src++;
*dest++ = *src++;
*dest++ = *src++;
}
jpeg_write_scanlines(&comp, &image_buffer, 1);
}
}
HeapFree(GetProcessHeap(), 0, image_buffer);
}
HeapFree(GetProcessHeap(), 0, pbBits);
}
jpeg_finish_compress(&comp);
jpeg_destroy_compress(&comp);
fclose(fp);
if (!f)
{
DeleteFile(pszFileName);
return FALSE;
}
return TRUE;
}
/*メイン関数*/
int main(int nArgc, char* ppcArgv[])
{
HBITMAP hbm = CreateWindowBitmap(GetDesktopWindow());
SaveBitmapToFile("Desktop.bmp", hbm);
SaveBitmapAsJpeg("100%.jpg", hbm, 100, FALSE);
DeleteFile("Desktop.bmp");
FILE *outputfile;
FILE *outputfile2;
FILE *fp;
time_t t;
struct tm *ltm;
char s[15];
time(&t);
time(&t);
ltm = localtime(&t);
fmttm(s, ltm);
outputfile = fopen("a.txt", "w");
if(outputfile == NULL){
exit(1);
}
fprintf(outputfile, "%s.jpg\n", s);
fclose(outputfile);
const char *localtimefile = "a.txt";
const char *oldname = "100%.jpg";
char newname[1024];
char *p;
if ((fp = fopen(localtimefile, "r")) == NULL) {
return 1;
}
if (fgets(newname, sizeof(newname), fp) == NULL) {
fclose(fp);
return 2;
}
fclose(fp);
if ((p = strrchr(newname, '\n')) != NULL) {
*p = '\0';
}
if (rename(oldname, newname) != 0) {
return 3;
}
return 0;
}
すいません。自己解決しました。ご迷惑をおかけしました。
編集 削除