How to save IDirect3DSurface9 or LPD3DXBUFFER to memory as JPG - c++

I'm trying to capture game screenshot, i have that code:
LPDIRECT3DDEVICE9 Device;
D3DSURFACE_DESC screenDescription;
...
void Capture(){
IDirect3DSurface9* pRenderTarget;
IDirect3DSurface9* pDestTarget;
Device->GetRenderTarget(0, &pRenderTarget);
pRenderTarget->GetDesc(&screenDescription);
Device->CreateOffscreenPlainSurface(screenDescription.Width, screenDescription.Height, screenDescription.Format, D3DPOOL_SYSTEMMEM, &pDestTarget, NULL);
Device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_FORCE_DWORD, &pDestTarget);
char tName[200];
sprintf(tName, "%s_%d.jpg", "C:\\test", GetTickCount());
D3DXSaveSurfaceToFileA(tName, D3DXIFF_JPG, pDestTarget, NULL, NULL);
//LPD3DXBUFFER buffer;
//D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_JPG, pDestTarget, NULL, NULL);
pRenderTarget->Release();
pDestTarget->Release();
isCapturing = false;
}
Saving to file with D3DXSaveSurfaceToFileA works perfectly, but i want to save captured image and write them to end of another file on disk, not creating a new once.
Is there a way how convert IDirect3DSurface9 or LPD3DXBUFFER to JPG bytes?

Ok, i'm found this code and he works:
LPD3DXBUFFER buffer;
D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_JPG, pDestTarget, NULL, NULL);
DWORD imSize = buffer->GetBufferSize();
void* imgBuffer = buffer->GetBufferPointer();
std::fstream out;
out.open(tName, std::ios_base::binary | std::ios_base::out);
out.write((char*)imgBuffer, imSize);
out.clear();
out.close();

Related

C++ raw HBITMAP to png struct data [duplicate]

I'm trying to send a screenshot of a window over tcp to a server.
Getting the screenshot is no problem (using GDIplus). The networking is also easy for me. The problem is trying to convert the gdi+ Bitmap to a png (in memory) to get the data out of it and send it to the server.
Can anyone help me please?
Gdiplus can save to file, or save to memory using IStream. See Gdiplus::Image::Save method
//get gdi+ bitmap
Gdiplus::Bitmap bitmap(hbitmap, nullptr);
//write to IStream
IStream* istream = nullptr;
HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &istream);
CLSID clsid_png;
CLSIDFromString(L"{557cf406-1a04-11d3-9a73-0000f81ef32e}", &clsid_png);
bitmap.Save(istream, &clsid_png);
The memory size is small enough that you can copy from IStream to a single buffer (see "Minimum example" for more detail)
//get memory handle associated with istream
HGLOBAL hg = NULL;
GetHGlobalFromStream(istream, &hg);
//copy IStream to buffer
int bufsize = GlobalSize(hg);
char *buffer = new char[bufsize];
//lock & unlock memory
LPVOID ptr = GlobalLock(hg);
memcpy(buffer, ptr, bufsize);
GlobalUnlock(hg);
//release will automatically free the memory allocated in CreateStreamOnHGlobal
istream->Release();
PNG is now available in buffer, its size is bufsize. You can work directly with the binary data, or convert to Base64 to send over the network.
Minimum example:
#include <iostream>
#include <fstream>
#include <vector>
#include <Windows.h>
#include <gdiplus.h>
bool save_png_memory(HBITMAP hbitmap, std::vector<BYTE>& data)
{
Gdiplus::Bitmap bmp(hbitmap, nullptr);
//write to IStream
IStream* istream = nullptr;
if (CreateStreamOnHGlobal(NULL, TRUE, &istream) != 0)
return false;
CLSID clsid_png;
if (CLSIDFromString(L"{557cf406-1a04-11d3-9a73-0000f81ef32e}", &clsid_png)!=0)
return false;
Gdiplus::Status status = bmp.Save(istream, &clsid_png);
if (status != Gdiplus::Status::Ok)
return false;
//get memory handle associated with istream
HGLOBAL hg = NULL;
if (GetHGlobalFromStream(istream, &hg) != S_OK)
return 0;
//copy IStream to buffer
int bufsize = GlobalSize(hg);
data.resize(bufsize);
//lock & unlock memory
LPVOID pimage = GlobalLock(hg);
if (!pimage)
return false;
memcpy(&data[0], pimage, bufsize);
GlobalUnlock(hg);
istream->Release();
return true;
}
int main()
{
CoInitialize(NULL);
ULONG_PTR token;
Gdiplus::GdiplusStartupInput tmp;
Gdiplus::GdiplusStartup(&token, &tmp, NULL);
//take screenshot
RECT rc;
GetClientRect(GetDesktopWindow(), &rc);
auto hdc = GetDC(0);
auto memdc = CreateCompatibleDC(hdc);
auto hbitmap = CreateCompatibleBitmap(hdc, rc.right, rc.bottom);
auto oldbmp = SelectObject(memdc, hbitmap);
BitBlt(memdc, 0, 0, rc.right, rc.bottom, hdc, 0, 0, SRCCOPY);
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
ReleaseDC(0, hdc);
//save as png
std::vector<BYTE> data;
if(save_png_memory(hbitmap, data))
{
//write from memory to file for testing:
std::ofstream fout("test.png", std::ios::binary);
fout.write((char*)data.data(), data.size());
}
DeleteObject(hbitmap);
Gdiplus::GdiplusShutdown(token);
CoUninitialize();
return 0;
}

C++ gdi::Bitmap to PNG Image in memory

I'm trying to send a screenshot of a window over tcp to a server.
Getting the screenshot is no problem (using GDIplus). The networking is also easy for me. The problem is trying to convert the gdi+ Bitmap to a png (in memory) to get the data out of it and send it to the server.
Can anyone help me please?
Gdiplus can save to file, or save to memory using IStream. See Gdiplus::Image::Save method
//get gdi+ bitmap
Gdiplus::Bitmap bitmap(hbitmap, nullptr);
//write to IStream
IStream* istream = nullptr;
HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &istream);
CLSID clsid_png;
CLSIDFromString(L"{557cf406-1a04-11d3-9a73-0000f81ef32e}", &clsid_png);
bitmap.Save(istream, &clsid_png);
The memory size is small enough that you can copy from IStream to a single buffer (see "Minimum example" for more detail)
//get memory handle associated with istream
HGLOBAL hg = NULL;
GetHGlobalFromStream(istream, &hg);
//copy IStream to buffer
int bufsize = GlobalSize(hg);
char *buffer = new char[bufsize];
//lock & unlock memory
LPVOID ptr = GlobalLock(hg);
memcpy(buffer, ptr, bufsize);
GlobalUnlock(hg);
//release will automatically free the memory allocated in CreateStreamOnHGlobal
istream->Release();
PNG is now available in buffer, its size is bufsize. You can work directly with the binary data, or convert to Base64 to send over the network.
Minimum example:
#include <iostream>
#include <fstream>
#include <vector>
#include <Windows.h>
#include <gdiplus.h>
bool save_png_memory(HBITMAP hbitmap, std::vector<BYTE>& data)
{
Gdiplus::Bitmap bmp(hbitmap, nullptr);
//write to IStream
IStream* istream = nullptr;
if (CreateStreamOnHGlobal(NULL, TRUE, &istream) != 0)
return false;
CLSID clsid_png;
if (CLSIDFromString(L"{557cf406-1a04-11d3-9a73-0000f81ef32e}", &clsid_png)!=0)
return false;
Gdiplus::Status status = bmp.Save(istream, &clsid_png);
if (status != Gdiplus::Status::Ok)
return false;
//get memory handle associated with istream
HGLOBAL hg = NULL;
if (GetHGlobalFromStream(istream, &hg) != S_OK)
return 0;
//copy IStream to buffer
int bufsize = GlobalSize(hg);
data.resize(bufsize);
//lock & unlock memory
LPVOID pimage = GlobalLock(hg);
if (!pimage)
return false;
memcpy(&data[0], pimage, bufsize);
GlobalUnlock(hg);
istream->Release();
return true;
}
int main()
{
CoInitialize(NULL);
ULONG_PTR token;
Gdiplus::GdiplusStartupInput tmp;
Gdiplus::GdiplusStartup(&token, &tmp, NULL);
//take screenshot
RECT rc;
GetClientRect(GetDesktopWindow(), &rc);
auto hdc = GetDC(0);
auto memdc = CreateCompatibleDC(hdc);
auto hbitmap = CreateCompatibleBitmap(hdc, rc.right, rc.bottom);
auto oldbmp = SelectObject(memdc, hbitmap);
BitBlt(memdc, 0, 0, rc.right, rc.bottom, hdc, 0, 0, SRCCOPY);
SelectObject(memdc, oldbmp);
DeleteDC(memdc);
ReleaseDC(0, hdc);
//save as png
std::vector<BYTE> data;
if(save_png_memory(hbitmap, data))
{
//write from memory to file for testing:
std::ofstream fout("test.png", std::ios::binary);
fout.write((char*)data.data(), data.size());
}
DeleteObject(hbitmap);
Gdiplus::GdiplusShutdown(token);
CoUninitialize();
return 0;
}

How to use GDI+ library to decode a jpeg in memory?

GDI+ provides a Image class, and you can use this class to read a image file with one format and then save this file to another format. But if I want to just decode a jpeg file (already loaded into memory), how can I do it?
You can use SHCreateMemStream and Gdiplus::Image::FromStream
#include <Window.h>
#include <Gdiplus.h>
#include <Shlwapi.h>
#include <atlbase.h>
...
CComPtr<IStream> stream;
stream.Attach(SHCreateMemStream(buf, bufsize));
Gdiplus::Image *image = Gdiplus::Image::FromStream(stream);
Where buf contains jpeg data (or any other compatible image format) and bufsize is the length of that data.
SHCreateMemStream needs "Shlwapi.lib" library.
Example:
void foo(HDC hdc)
{
//Read jpeg from input file in to buf:
HANDLE hfile = CreateFile(L"test.jpg",
GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (!hfile) return;
DWORD bufsize = GetFileSize(hfile, NULL);
BYTE *buf = new BYTE[bufsize];
DWORD temp;
ReadFile(hfile, buf, bufsize, &temp, 0);
//convert buf to IStream
CComPtr<IStream> stream;
stream.Attach(SHCreateMemStream(buf, bufsize));
//Read from IStream
Gdiplus::Bitmap *image = Gdiplus::Bitmap::FromStream(stream);
if (image)
{
Gdiplus::Graphics g(hdc);
g.DrawImage(image, 0, 0);
delete image;
}
delete[]buf;
CloseHandle(hfile);
}
Edit: easier method as mentioned in comments:
IStream* stream = SHCreateMemStream(buf, bufsize);
Gdiplus::Image *image = Gdiplus::Image::FromStream(stream);
...
stream->Release();

following "What's a Creel?"s tutorial: Can't create a IWICBitmapDecoder, in visual studio 2015

I've been following "what's a Creel?"'s tutorial for direct 2d. I got to tutorial 8: 'Loading an image'. I didn't have the spritesheet object save the pointer to the Graphics object as this caused problems with this version of visual studio, so it's passed every time something needing it is called. main point: when I try creating a IWICBitmapDecoder with the wicfactory->CreateDecoderFromFile() method, I get the following error:
Exception thrown at 0x008C70A7 in Project8.exe: 0xC0000005: Access violation reading location 0x00000000.
and in the Autos I get:
hr | E_NOINTERFACE No such interface supported.
this | 0x00c1a5c8 {bmp=0x00000000 <NULL> } spritesheet *
wicfactory | 0x00000000<NULL>
wicdecoder | 0xcccccccc{...}
the code being this:
#pragma once
#include <wincodec.h> //include windowscodecs.lib in the linker input
#include "Graphics.h"
#include <d2d1.h>
#include<string>
class spritesheet {
public:
ID2D1Bitmap* bmp;
spritesheet() {}
spritesheet(LPCWSTR file, graphics* gfx) {
//this->gfx = gfx;
//bmp = NULL;
HRESULT hr;
//create an image factory
IWICImagingFactory *wicFactory;
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
CLSID_WICImagingFactory,
(LPVOID*)&wicFactory
);
//create a decoder
IWICBitmapDecoder *wicdecoder;
hr = wicFactory->CreateDecoderFromFilename(
file,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&wicdecoder
);
IWICBitmapFrameDecode* wicframe = NULL;
hr = wicdecoder->GetFrame(0, &wicframe);
IWICFormatConverter *wicconverter = NULL;
hr = wicFactory->CreateFormatConverter(&wicconverter);
hr = wicconverter->Initialize(
wicframe,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.0,
WICBitmapPaletteTypeCustom
);
gfx->gettarget()->CreateBitmapFromWicBitmap(
wicconverter,
NULL,
&bmp
);
if (wicdecoder) wicdecoder->Release();
if (wicFactory) wicFactory->Release();
if (wicconverter) wicconverter->Release();
if (wicframe) wicframe->Release();
}
void init(wchar_t * file, graphics * gfx) {
//this->gfx = gfx;
//bmp = NULL;
HRESULT hr;
//create an image factory
IWICImagingFactory* wicFactory;
hr = CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
CLSID_WICImagingFactory,
(LPVOID*)&wicFactory
);
//create a decoder
IWICBitmapDecoder* wicdecoder;
hr = wicFactory->CreateDecoderFromFilename(
file,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&wicdecoder
);
IWICBitmapFrameDecode* wicframe = NULL;
hr = wicdecoder->GetFrame(0, &wicframe);
IWICFormatConverter *wicconverter = NULL;
hr = wicFactory->CreateFormatConverter(&wicconverter);
hr = wicconverter->Initialize(
wicframe,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.0,
WICBitmapPaletteTypeCustom
);
gfx->rendertarget->CreateBitmapFromWicBitmap(
wicconverter,
NULL,
&bmp
);
if (wicdecoder) wicdecoder->Release();
if (wicFactory) wicFactory->Release();
if (wicconverter) wicconverter->Release();
if (wicframe) wicframe->Release();
gfx->rendertarget->DrawBitmap(
bmp,
D2D1::RectF(0.0f, 0.0f, 10, 10), //dest rect
1.0f,
D2D1_BITMAP_INTERPOLATION_MODE::D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, //effect for scaling
D2D1::RectF(0, 0, 10, 10)); //scource rect
}
void draw(graphics *gfx) {
gfx->rendertarget->DrawBitmap(
bmp,
D2D1::RectF(0.0f, 0.0f, 10, 10), //dest rect
1.0f,
D2D1_BITMAP_INTERPOLATION_MODE::D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, //effect for scaling
D2D1::RectF(0, 0, 10, 10)); //scource rect
}
};
now, just to test things, I did put a ID2D1Bitmap* bmp; at the start of each method just to see where things got, but the wicdecoder error message just changed to a random place in memory.
Found the issue. the implementation of CoCreateInstance() was fine, but there were a few problems with his code, relating to it being outdated.
NOW, before i begin, i must say, this was PARTLY taken from: https://msdn.microsoft.com/en-us/library/windows/desktop/dd756686(v=vs.85).aspx
the implementation here still gives no usable implementation of CoCreateInstance(), (and also requires you to have the boilerplate code of this tutorial. Also, the tutorial's code is from 2008 and the architecture expands the amount of memory the app uses every time you resize the window... but i digress. main point: DON'T use that tutorial's program example as a model for your own programs, but it does give a good example on how to declare/initialize the proper WIC and D2D objects) the Microsoft documentation for DX12 is also out of date, but this is what I will clarify here.
the correct implementation of CoCreateInstance(), and the correct way to load an image is:
void spritesheet::load(PCWSTR uri, ID2D1HwndRenderTarget * gfx) {
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;
IWICImagingFactory *wicFactory = NULL;
CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&wicFactory);
HRESULT hr = wicFactory->CreateDecoderFromFilename(
uri,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
if (SUCCEEDED(hr)) {
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
if (SUCCEEDED(hr)) {
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = wicFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr)) {
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
if (SUCCEEDED(hr))
{
// Create a Direct2D bitmap from the WIC bitmap.
hr = gfx->CreateBitmapFromWicBitmap(
pConverter,
NULL,
&bmp
);
}
if (pDecoder) {
pDecoder->Release();
}
if (pSource) {
pSource->Release();
}
if (pConverter) {
pConverter->Release();
}
}
Note:
uri simply being the file name, in a PCWSTR format, declared as L"name.bmp" when passing to the function.
the correct way to draw an image is:
void spritesheet::draw(ID2D1HwndRenderTarget * gfx, float bx, float by, float rx, float ry, float dx, float dy) {
D2D1_SIZE_F size = bmp->GetSize();
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(dx, dy);
gfx->DrawBitmap(
bmp,
D2D1::RectF(
upperLeftCorner.x,
upperLeftCorner.y,
upperLeftCorner.x + rx,
upperLeftCorner.y + ry),
1,
D2D1_BITMAP_INTERPOLATION_MODE::D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
D2D1::RectF(
bx,
by,
rx,
ry)
);
}
Now, the rest of the D2D documentation is not up to date, but still gives you a good example of what's out there to use. Some of the objects don't exist anymore, like D2D1::Rect being canned and now you only have: D2D1::RectF, nad some of the documentation and tutorials are rather disjointed and untested, but if you dig long enough, you can create a game. just remember to have the x86/x64 redistributeable packages run as part of your installer written in c# (because c++ won't run natively in windows anymore, and c# is half as fast), and your c++ program will run where you need it to.
Im tring to do the solution you gave here but no luck still wicFactory is null.
D2DGauge* d2dg = reinterpret_cast<D2DGauge*>(args);
HRESULT hr;
bmp = nullptr;
IWICBitmapDecoder *pDecoder = NULL;
IWICBitmapFrameDecode *pSource = NULL;
IWICFormatConverter *pConverter = NULL;
//Creating a factory
IWICImagingFactory *wicFactory = NULL;
CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&wicFactory);
MessageBox(NULL, (LPCWSTR)wicFactory, NULL, MB_OK);
//Creating a decoder
hr = wicFactory->CreateDecoderFromFilename(
filename,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder);
//Read Frame from image
if (SUCCEEDED(hr)) {
// Create the initial frame.
hr = pDecoder->GetFrame(0, &pSource);
}
//Creating a Converter
if (SUCCEEDED(hr)) {
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = wicFactory->CreateFormatConverter(&pConverter);
}
//Setup the converter
if (SUCCEEDED(hr)) {
hr = pConverter->Initialize(
pSource,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
//Use the converter to create an D2D1Bitmap
//ID2D1Bitmap* bmp;
if (SUCCEEDED(hr))
{
hr = d2dg->pRT->CreateBitmapFromWicBitmap(
pConverter,
NULL,
&bmp
);
}
if (wicFactory)wicFactory->Release();
if (pDecoder)pDecoder->Release();
if (pConverter)pConverter->Release();
if (pSource)pSource->Release();
How could I make it not a nullptr?

Enhanced Video Renderer snapshot image is empty

I am trying to use the enhanced video renderer to take a snapshot, image of the current rendered stream. I am able to display a video feed either from a file or capture card. When I run
if (pDisplay)
{
BITMAPINFOHEADER lpHeader = { 0 };
BYTE* lpCurrImage = NULL;
DWORD bitmapSize = 0;
LONGLONG timestamp = 0;
lpHeader.biSize = sizeof(BITMAPINFOHEADER);
if (SUCCEEDED(hr = pDisplay->GetCurrentImage(&lpHeader, &lpCurrImage, &bitmapSize, &timestamp)))
{
the output image data is 0, see image below:
For some reason, when I use VMR7 or 9, the image works fine and has data (rendering the same video) however EVR does not get an image. The bitmap that outputs is 14kb (which is the header information).
For more info, here is the code that I use to render the EVR:
HRESULT InitializeWindowlessEVR(IGraphBuilder* pGraph, IBaseFilter** ppEVR)
{
IBaseFilter* pEvr = NULL;
CComPtr<IMFGetService> pServices;
CComPtr<IMFQualityAdvise2> pAdvise;
if (!ppEVR)
return E_POINTER;
*ppEVR = NULL;
// Create the VMR and add it to the filter graph.
HRESULT hr = CoCreateInstance(CLSID_EnhancedVideoRenderer, NULL,
CLSCTX_INPROC, IID_IBaseFilter, (void**)&pEvr);
if (SUCCEEDED(hr))
{
hr = pGraph->AddFilter(pEvr, L"Enhanced Video Renderer");
if (SUCCEEDED(hr))
{
// Update the window settings
CHECK_HR(pEvr->QueryInterface(IID_IMFGetService, (void**)&pServices));
CHECK_HR(pServices->GetService(MR_VIDEO_RENDER_SERVICE, IID_IMFVideoDisplayControl, (void**)&pVideoDisplay));
CHECK_HR(pVideoDisplay->SetVideoWindow(ghApp));
CHECK_HR(pVideoDisplay->SetAspectRatioMode(MFVideoARMode_PreservePicture));
}
pEvr->AddRef();
*ppEVR = pEvr;
pEvr->Release();
pEvr = NULL;
}
done:
return hr;
}
Please help me figure out why I am unable to get an image for EVR, I do not want to go back and use VMR9. I can post more code if it helps. Thanks in advance!
Answer:
BOOL CaptureImage(LPCTSTR szFile)
{
if (pVideoDisplay)
{
BYTE* lpCurrImage = NULL;
BITMAPINFOHEADER lpHeader = { 0 };
DWORD bitmapSize = 0;
LONGLONG timestamp = 0;
lpHeader.biSize = sizeof(BITMAPINFOHEADER);
if (SUCCEEDED(pVideoDisplay->GetCurrentImage(&lpHeader, &lpCurrImage, &bitmapSize, &timestamp)))
{
// Create a new file to store the bitmap data
HANDLE hFile = CreateFile(szFile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
return FALSE;
DWORD dwWritten;
BITMAPFILEHEADER fileHeader;
fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fileHeader.bfReserved1 = 0;
fileHeader.bfReserved2 = 0;
fileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + lpHeader.biSizeImage;
fileHeader.bfType = 0x4d42;
// Write the bitmap header and bitmap bits to the file
WriteFile(hFile, (LPCVOID)&fileHeader, sizeof(BITMAPFILEHEADER), &dwWritten, 0);
WriteFile(hFile, (LPCVOID)&lpHeader, sizeof(BITMAPINFOHEADER), &dwWritten, 0);
WriteFile(hFile, (LPCVOID)lpCurrImage, bitmapSize, &dwWritten, 0);
CloseHandle(hFile);
CoTaskMemFree(lpCurrImage);
return TRUE;
}
else
{
return FALSE;
}
}
return FALSE;
}