I have an Excel spreadsheet which is one sheet with a table and chart in it. What I need to do is add this to a PDF. The PDF API I am using does not support importing Excel documents so I wondered if there is any way from C++ to save/convert an Excel document to a Jpeg (or BMP)?
Just to add, obviously this has to be done in C++ as part of an existing application which creates PDFs every time it is run. I wouldn't be asking the question if it was a one off that could just be done manually.
Well I have, after much effort and hair pulling, managed to answer my own question. Contrary to what was said in other answers, it is possible to save an Excel spreadsheet as Jpeg (although I have stuck to bitmap format as that's good enough for me. It would be easy enough to convert to jpeg though). What I did via the Excel API was select the used range. From there, I copy this to the clipboard as a picture, then get the clipboard data and convert it to a picture object with an IPicture interface. From there, create a stream and copy this to the stream before saving this stream to file. The result is a bmp file with exactly the image I was after. Code sample below.
CRange range = sheet.get_UsedRange();
range.CopyPicture(Excel::xlScreen, Excel::xlBitmap);
if (IsClipboardFormatAvailable(CF_BITMAP)) // just to be sure it copied
{
if (OpenClipboard(NULL) > 0)
{
// save clipboard as bmp
HBITMAP hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
SaveBitmap(ps_filename, hBitmap, NULL);
CloseClipboard();
}
}
}
bool CExcelDocument::SaveBitmap(LPCSTR filename, HBITMAP bmp, HPALETTE pal)
{
bool bReturn = false;
PICTDESC pictdesc;
LPPICTURE pPicture;
::ZeroMemory( &pictdesc, sizeof(pictdesc) );
pictdesc.picType = PICTYPE_BITMAP;
pictdesc.bmp.hbitmap = bmp;
pictdesc.bmp.hpal = pal;
pictdesc.cbSizeofstruct = sizeof(PICTDESC);
HRESULT hr = OleCreatePictureIndirect(&pictdesc, IID_IPicture, false, reinterpret_cast<void**>(&pPicture));
if (!SUCCEEDED(hr))
return false;
LPSTREAM pStream;
hr = CreateStreamOnHGlobal(0, true, &pStream);
if (!SUCCEEDED(hr))
{
pPicture->Release();
return false;
}
LONG lBytesRead = 0;
DWORD lBytesWritten = 0;
hr = pPicture->SaveAsFile(pStream, true, &lBytesRead);
if (!SUCCEEDED(hr))
{
pStream->Release();
pPicture->Release();
return false;
}
HANDLE hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (!hFile)
{
return false;
}
HGLOBAL hMemory = 0;
GetHGlobalFromStream(pStream, &hMemory);
LPVOID pData = GlobalLock(hMemory);
bReturn = !!WriteFile(hFile, pData, lBytesRead, &lBytesWritten, 0);
bReturn &= (lBytesWritten == static_cast<DWORD>(lBytesRead));
GlobalUnlock(hMemory);
CloseHandle(hFile);
pStream->Release();
pPicture->Release();
return bReturn;
}
What you can do is, load the spreadsheet into C++ using the Execl API (Office Interop). Now you have access to the values within the rows and columns. You should also be able to extract how large they are (width and height). This is your starting point.
Now the simplest approach is to use GDI (or whatever) to create a new picture and walk through every cell within your excel sheet and draw the content of it into your picture. After this you can draw lines resembling the cells itself.
Anyway this results in a lot of work and won't be easy, especially if you have to consider things like text alignment within the cell and border sizes and so on.
Related
A while ago, I have created a CredentialProvider for Windows 7 that showed a custom tile image for the user. When I tested my CredentialProvider on Windows 10, I noticed that the tile image is not round as the usual Windows user image is, so I assumed, I have to provide my own round image with transparency. I tried to load a transparent PNG with the round shape and pass the corresponding HBITMAP in the ICredentialProviderCredentiall::GetBitmapValue() function. Unfortunately, instead of being transparent, the background of the PNG is white. Is there any way to show the actual transparency? Since the HBITMAP is drawn by LogonUI itself, I cannot modify the drawing behavior.
To create the HBITMAP out of my PNG resource, I use the following function:
HBITMAP LoadPNG(HINSTANCE hInst, int resourceId)
{
HGLOBAL hGlobal;
LPSTREAM pStream;
HBITMAP tBmp = NULL;
ULONG_PTR token = 0;
Gdiplus::GdiplusStartupInput input = NULL;
Gdiplus::GdiplusStartup(&token, &input, NULL);
if (token != 0)
{
HRSRC hRsrc = FindResource(hInst, MAKEINTRESOURCE(resourceId), TEXT("PNG"));
HGLOBAL hGlob1 = LoadResource(hInst, hRsrc);
int size = SizeofResource(hInst, hRsrc);
hGlobal = GlobalAlloc(GMEM_FIXED, size);
LPVOID resPtr = LockResource(hGlob1);
memcpy(hGlobal, resPtr, size);
FreeResource(hGlob1);
CreateStreamOnHGlobal(hGlobal, true, &pStream);
Gdiplus::Bitmap* bmp = new Gdiplus::Bitmap(pStream, false);
bmp->GetHBITMAP(Gdiplus::Color::Transparent, &tBmp);
Gdiplus::GdiplusShutdown(token);
}
return tBmp;
}
I tried also other values for the GetHBITMAP() background color parameter such as AlphaMask and AlphaShift, but these did not work either (instead, the white background turned black)
Is there any way to accomplish my goal?
The newest Credential Provider Technical Reference (which is not that easy to find on MSDN) actually tells what the issue with the tile image is:
In Windows 10, the selected User/V1/PLAP credential provider has an
image size of 192x192. The ones on the bottom left list is of 48x48.
Note LogonUI uses circular image for user and square image for V1/PLAP
according to the new design direction. The image size of V2 Credential
Provider tile under a selected user is 48x48.
If I read the documentation correctly then the challenge must be somehow to convince the LogonUI that its a User and not a V1/PLAP, I think this could be archieved returning a dummy user in the GetSerialization call.
I am working on one MFC/SDI project Where I am using CListView as a default View.
But the thing is, I have applied ImageList to the CListCtrl
but these Images are not clearly displayed.
As shown in the picture below.
Below is the code for creating new image List.
Creating ImageList where m_ImgList is a member of CImageList.
if(m_ImgList->m_hImageList == NULL || m_ImgList->DeleteImageList() == TRUE)
{
m_ImgList->Create(16,16, ILC_COLOR,0, 1);
}
Then I am creating images by using BLOB data from database.
Here, pByte is BYTE* to hold BLOB Data.
HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE, nbytes+5);
LPVOID pV = GlobalLock(hGlobal);
memcpy(pV, pByte, nbytes);
GlobalUnlock(hGlobal);
LPSTREAM pstm = NULL;
HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
CImage image;
image.Load(pstm);
if (image == NULL)
{
image.Load(NILIMAGE);
}
CPaintDC dc(this);
CDC pMDC;
pMDC.CreateCompatibleDC(&dc);
CBitmap pb;
pb.CreateCompatibleBitmap(&dc, iNewWidth, iNewHeight);
CBitmap *pob = (CBitmap*)pMDC.SelectObject(&pb);
image.StretchBlt(pMDC.m_hDC,0, 0, iNewWidth, iNewHeight,
0, 0, image.GetWidth(), image.GetHeight(), SRCCOPY);
pMDC.SelectObject(pob);
Adding images to the image list
if((m_ImgList->Add(&pb, RGB(0,0,0)))== -1)
{
_T("undone");
}
if(hGlobal != NULL)
::GlobalFree(hGlobal);
Can anyone tell me how can I do that?
Thank you in Advance.
and Sorry for bad English.
I have a Direct2D app that I am making, and I am writing a Direct2D library that makes using Direct2D easier for me as well. I'll post the exact problematic code if I need to, but my main issue is that I have an ID2D1HwndRenderTarget in the definition of one class, I extend that class with another class, in the child class I have a method that calls a method of the parent class that initializes the Render Target, and then in turn calls the load method of the child class. However, as soon as the program reaches the load content method of the child class, the __vfptr variable (I don't have a clue what that is) in the IUnknown portion of the ID2D1HwndRenderTarget is now null. The only reason I figured this out is in some other code I was getting an access violation error when using the render target to create a ID2D1Bitmap from an IWicBitmapSource. I don't understand how this happens because after initializing the Render Target, that _vfptr variable becomes null as soon as the method with the initialization code returns. Can anyone explain why this may be happening? My relevant code is below.
This code is called once to create the hwnd render target and the offscreen render target. This is in a dll project.
GameBase.cpp
HRESULT GameBase::Initialize(HINSTANCE hInst, HWND winHandle, struct DX2DInitOptions options)
{
this->mainRenderTarget = NULL;
this->offscreenRendTarget = NULL;
this->factory = NULL;
HRESULT result;
D2D1_FACTORY_OPTIONS factOptions;
D2D1_FACTORY_TYPE factType;
if(options.enableDebugging)
factOptions.debugLevel = D2D1_DEBUG_LEVEL::D2D1_DEBUG_LEVEL_ERROR;
else
factOptions.debugLevel = D2D1_DEBUG_LEVEL::D2D1_DEBUG_LEVEL_NONE;
if(options.singleThreadedApp)
factType = D2D1_FACTORY_TYPE_SINGLE_THREADED;
else
factType = D2D1_FACTORY_TYPE_MULTI_THREADED;
result = D2D1CreateFactory(factType, factOptions, &this->factory);
if(FAILED(result))
{
OutputDebugString(L"Failed to create a Direct 2D Factory!");
return result;
}
this->instance = hInst;
this->hwnd = winHandle;
D2D1_SIZE_U size = D2D1::SizeU(options.winWidth, options.winHeight);
this->width = options.winWidth;
this->height = options.winHeight;
result = factory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(winHandle, size), &this->mainRenderTarget);
if(FAILED(result))
{
OutputDebugString(L"Failed to create a render target to draw to the window with!");
return result;
}
result = this->mainRenderTarget->CreateCompatibleRenderTarget(&this->offscreenRendTarget);
if(FAILED(result))
{
OutputDebugString(L"Failed to create an offscreen render target from the main render target.");
return result;
}
return LoadContent();
}
After the call to LoadContent, at no point in time do I change the value of the mainRenderTarget.
DX2DImage.cpp
HRESULT DX2DImageLoader::LoadFromResource(LPCWSTR resourceName, LPCWSTR resourceType, HMODULE progModule, DX2DImage* image)
{
if(!this->isInit)
{
OutputDebugStringA("You must call InitializeImageLoader before using this image loader!");
return E_FAIL;
}
IWICBitmapDecoder *decoder = NULL;
IWICBitmapFrameDecode *source = NULL;
IWICStream *stream = NULL;
IWICFormatConverter *converter = NULL;
HRSRC imageResHandle = NULL;
HGLOBAL imageResDataHandle = NULL;
void *imageFile = NULL;
DWORD imageFileSize = 0;
HRESULT result;
//Find the image.
imageResHandle = FindResource(progModule, resourceName, resourceType);
if(!imageResHandle)
{
OutputDebugStringA("Failed to get a handle to the resource!");
return E_FAIL;
}
//Load the data handle of the image.
imageResDataHandle = LoadResource(progModule, imageResHandle);
if(!imageResDataHandle)
{
OutputDebugStringA("Failed to load the image from the module!");
return E_FAIL;
}
//Lock and retrieve the image.
imageFile = LockResource(imageResDataHandle);
if(!imageFile)
{
OutputDebugStringA("Failed to lock the image in the module!");
return E_FAIL;
}
//Get the size of the image.
imageFileSize = SizeofResource(progModule, imageResHandle);
if(!imageFileSize)
{
OutputDebugStringA("Failed to retrieve the size of the image in the module!");
return E_FAIL;
}
//Create a stream that will read the image data.
result = this->factory->CreateStream(&stream);
if(FAILED(result))
{
OutputDebugStringA("Failed to create an IWICStream!");
return result;
}
//Open a stream to the image.
result = stream->InitializeFromMemory(reinterpret_cast<BYTE*>(imageFile), imageFileSize);
if(FAILED(result))
{
OutputDebugStringA("Failed to initialize the stream!");
return result;
}
//Create a decoder from the stream
result = this->factory->CreateDecoderFromStream(stream, NULL, WICDecodeMetadataCacheOnDemand, &decoder);
if(FAILED(result))
{
OutputDebugStringA("Failed to create a decoder from the stream!");
return result;
}
//Get the first frame from the image.
result = decoder->GetFrame(0, &source);
if(FAILED(result))
{
OutputDebugStringA("Failed to get the first frame from the decoder!");
return result;
}
//Create a format converter to convert image to 32bppPBGRA
result = this->factory->CreateFormatConverter(&converter);
if(FAILED(result))
{
OutputDebugStringA("Failed to create a format converter!");
return result;
}
//Convert the image to the new format.
result = converter->Initialize(source, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeMedianCut);
if(FAILED(result))
{
OutputDebugStringA("Failed to convert the image to the correct format!");
return result;
}
//Create the Direct2D Bitmap from the Wic Bitmap.
result = this->renderTarget->CreateBitmapFromWicBitmap(converter, NULL, &image->bitmap);
if(FAILED(result))
{
OutputDebugStringA("Failed to create a Direct 2D Bitmap from a WIC Bitmap!");
return result;
}
image->width = static_cast<UINT>(image->bitmap->GetSize().width);
image->height = static_cast<UINT>(image->bitmap->GetSize().height);
SafeRelease(&source);
SafeRelease(&converter);
SafeRelease(&decoder);
SafeRelease(&stream);
return S_OK;
}
The Access Violation exception occurs on the line
result = this->renderTarget->CreateBitmapFromWicBitmap(converter, NULL, &image->bitmap);
where image->bitmap is a currently NULL (Like it's supposed to be) ID2D1Bitmap.
Here, the renderTarget variable is the same mainRenderTarget variable from GameBase.cpp above. When I debug the line, all the parents of the RenderTarget are not null, however once I get to the IUnknown interface under it all, the _vfptr thing is null. This is not the case with the converter variable, this variable, or image variable.
I don't have enough code to debug your code, but from what I see I suspect the call to converter->Initialize(...) be invalid since MSDN say:
If you do not have a predefined palette, you must first create one. Use
InitializeFromBitmap to create the palette object, then pass it in along
with your other parameters.
dither, pIPalette, alphaThresholdPercent, and paletteTranslate are used to
mitigate color loss when converting to a reduced bit-depth format. For
conversions that do not need these settings, the following parameters values
should be used: dither set to WICBitmapDitherTypeNone, pIPalette set to NULL,
alphaThresholdPercent set to 0.0f, and paletteTranslate set to
WICBitmapPaletteTypeCustom.
And in your code you do not provide a valid pallete(you used NULL) and your paletteTranslate is not WICBitmapPaletteTypeCustom
I tried the following code on OnInitDialog() but nothing was shown.
m_staticLogo.SetBitmap(::LoadBitmap(NULL, MAKEINTRESOURCE(IDB_LOGO)));
where m_staticLogo is the static picture control and IDB_LOGO is the resource ID of the png file.
As you’ve discovered, ::LoadBitmap (and the newer ::LoadImage) only deal with .bmps. By far the easiest solution is to convert your image to a .bmp.
If the image has transparency, it can be converted into a 32-bit ARGB bitmap (here is a tool called AlphaConv that can convert it). Then load the image using the CImage class LoadFromResource method. Pass the CImage to m_staticLogo.SetBitmap().
But if you really need it to be a .png, it can be done.
Method 1 (the easier way): Load the .png from a file using CImage::Load. Pass the CImage to m_staticLogo.SetBitmap().
Method 2 (the harder way): Load the .png from a resource by loading the resource into a COM IStream and using CImage::Load. (NOTE: CImage::LoadFromResource looks tempting but will not work with a .png graphic). To get the resource into a COM IStream, see this Codeproject article. Note the article works with Gdiplus::Bitmap but the key part is how to create the IStream, which you should be able to adapt for CImage. Finally, pass the CImage to m_staticLogo.SetBitmap().
Edit: Updated to use CImage, which is easier than Gdiplus::Bitmap.
For those, who need quick solution, here is a way to load png file from resources using GDI+ (original answer for standard GDI from here - http://www.codeproject.com/Questions/377803/How-to-load-PNG-images-in-mfc):
bool GdiPlusUtils::LoadBitmapFromPNG(UINT uResourceID,
Bitmap** ppBitmapOut, HINSTANCE hInstance /*= NULL*/)
{
bool bRet = false;
if (!hInstance)
hInstance = AfxGetInstanceHandle();
HRSRC hResourceHandle = ::FindResource(
hInstance, MAKEINTRESOURCE(uResourceID), L"PNG");
if (0 == hResourceHandle)
{
return bRet;
}
DWORD nImageSize = ::SizeofResource(hInstance, hResourceHandle);
if (0 == nImageSize)
{
return bRet;
}
HGLOBAL hResourceInstance = ::LoadResource(hInstance, hResourceHandle);
if (0 == hResourceInstance)
{
return bRet;
}
const void* pResourceData = ::LockResource(hResourceInstance);
if (0 == pResourceData)
{
FreeResource(hResourceInstance);
return bRet;
}
HGLOBAL hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, nImageSize);
if (0 == hBuffer)
{
FreeResource(hResourceInstance);
return bRet;
}
void* pBuffer = ::GlobalLock(hBuffer);
if (0 != pBuffer)
{
CopyMemory(pBuffer, pResourceData, nImageSize);
IStream* pStream = 0;
if (S_OK == ::CreateStreamOnHGlobal(hBuffer, FALSE, &pStream))
{
*ppBitmapOut = new Bitmap(pStream);
pStream->Release();
bRet = true;
}
::GlobalUnlock(hBuffer);
}
::GlobalFree(hBuffer);
UnlockResource(hResourceInstance);
FreeResource(hResourceInstance);
return bRet;
}
You can add png file as resource using Add Resource command and in the panel choose Import.
Bitmap and icon it supports. Not sure about png.
Alternately, May be you can try the following.
open png in MS Paint or some other viewer.
Then copy the image portion from that.
Create a resource in MFC resource.
Paste the copied image to newly created resource.
Use new resource id in LoadBitmap.
If you are converting .png image file to .bmp format, you can end up with image clarity. So, catch WM_PAINT message in dialog box class and use
Graphics::DrawImage method
To obtain this method link your project with gdiplus.lib library.
Actually, it only fails the second time it's called. I'm using a windowless control to play video content, where the video being played could change while the control is still on screen. Once the graph is built the first time, we switch media by stopping playback, replacing the SOURCE filter, and running the graph again. This works fine under Vista, but when running on XP, the second call to Run() returns E_UNEXPECTED.
The initialization goes something like this:
// Get the interface for DirectShow's GraphBuilder
mGB.CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER);
// Create the Video Mixing Renderer and add it to the graph
ATL::CComPtr<IBaseFilter> pVmr;
pVmr.CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC);
mGB->AddFilter(pVmr, L"Video Mixing Renderer 9");
// Set the rendering mode and number of streams
ATL::CComPtr<IVMRFilterConfig9> pConfig;
pVmr->QueryInterface(IID_IVMRFilterConfig9, (void**)&pConfig);
pConfig->SetRenderingMode(VMR9Mode_Windowless);
pVmr->QueryInterface(IID_IVMRWindowlessControl9, (void**)&mWC);
And here's what we do when we decide to play a movie. RenderFileToVideoRenderer is borrowed from dshowutil.h in the DirectShow samples area.
// Release the source filter, if it exists, so we can replace it.
IBaseFilter *pSource = NULL;
if (SUCCEEDED(mpGB->FindFilterByName(L"SOURCE", &pSource)) && pSource)
{
mpGB->RemoveFilter(pSource);
pSource->Release();
pSource = NULL;
}
// Render the file.
hr = RenderFileToVideoRenderer(mpGB, mPlayPath.c_str(), FALSE);
// QueryInterface for DirectShow interfaces
hr = mpGB->QueryInterface(&mMC);
hr = mpGB->QueryInterface(&mME);
hr = mpGB->QueryInterface(&mMS);
// Read the default video size
hr = mpWC->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL);
if (hr != E_NOINTERFACE)
{
if (FAILED(hr))
{
return hr;
}
// Play video at native resolution, anchored at top-left corner.
RECT r;
r.left = 0;
r.top = 0;
r.right = lWidth;
r.bottom = lHeight;
hr = mpWC->SetVideoPosition(NULL, &r);
}
// Run the graph to play the media file
if (mMC)
{
hr = mMC->Run();
if (FAILED(hr))
{
// We get here the second time this code is executed.
return hr;
}
mState = Running;
}
if (mME)
{
mME->SetNotifyWindow((OAHWND)m_hWnd, WM_GRAPHNOTIFY, 0);
}
Anybody know what's going on here?
Try calling IMediaControl::StopWhenReady before removing the source filter.
When are you calling QueryInterface directly? you can use CComQIPtr<> to warp the QI for you. This way you won't have to call Release as it will be called automatically.
The syntax look like this: CComPtr<IMediaControl> mediaControl = pGraph;
In FindFilterByName() instead of passing live pointer pass a CComPtr, again so you won't have to call release explicitly.
Never got a resolution on this. The production solution was to just call IGraphBuilder::Release and rebuild the entire graph from scratch. There's a CPU spike and a slight redraw delay when switching videos, but it's less pronounced than we'd feared.