Click here to Skip to main content
15,895,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to open a picture in a format like Jpg or bmp or gif
i added it to the resource so its binded in the exe and its not taken from the harddrive of the client who is running the exe
now what i want to do is to open this pic and display it in photoviewer of the window os
my c++ code is made on an EMPTY Project with multibyte character set.
Below is something i got from net but it has few problems like first of all its in UNICODE
then its using hardcoding for paths like its using
C++
WCHAR wsParameters[MAX_PATH] = L"\"C:\\Program Files\\Windows Photo Viewer\\PhotoViewer.dll\" ImageView_Fullscreen ";
its not must that every os will be in the drive C
then on this line
C++
WCHAR wsFile[MAX_PATH] = L"E:\\Temp\\test.jpg";

its storing the file in clients harddrive which i don't want to do
so any suggesstions??

What I have tried:

C++
void SaveIStreamToFile(IStream *pIStream, LPWSTR pwsFile)
{
	HANDLE hFile;
	LPVOID pv = NULL;
	LARGE_INTEGER li;
	DWORD  nNbBytesWritten;
	ULONG  nNbBytes;
	HRESULT hr;
	int nBufferSize = 4096;

	hFile = CreateFile(pwsFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile != INVALID_HANDLE_VALUE)
	{
		pv = (LPSTR)LocalAlloc(LPTR, nBufferSize);
		if (pv)
		{
			LISet32(li, 0);
			pIStream->Seek(li, STREAM_SEEK_SET, NULL);
			do
			{
				hr = pIStream->Read(pv, nBufferSize, &nNbBytes);
				if (SUCCEEDED(hr))
				{
					if (!WriteFile(hFile, pv, nNbBytes, &nNbBytesWritten, NULL))
						hr = E_FAIL;
				}
			} while ((SUCCEEDED(hr)) && (nNbBytes == nBufferSize));
			LocalFree(pv);
		}
		CloseHandle(hFile);
	}
	return;
}
int main()
{
	HRESULT(STDAPICALLTYPE * pSHCreateStreamOnDllResourceW)(PCWSTR pwszDll, PCWSTR pwszName, PCWSTR pwszType, IStream** ppstm);
	HMODULE hInst = LoadLibrary(L"SHLWAPI.DLL");
	if (hInst)
	{
		(FARPROC&)pSHCreateStreamOnDllResourceW = GetProcAddress(hInst, (LPCSTR)MAKEINTRESOURCE(627));
		IStream *pstm;
		if (pSHCreateStreamOnDllResourceW)
		{
			WCHAR wszPath[MAX_PATH];
			GetModuleFileNameW(NULL, wszPath, MAX_PATH);
			HRESULT hr = pSHCreateStreamOnDllResourceW(wszPath, (LPWSTR)MAKEINTRESOURCE(IDR_JPEG1), L"JPEG", &pstm);
			WCHAR wsFile[MAX_PATH] = L"E:\\Temp\\test.jpg";
			SaveIStreamToFile(pstm, wsFile);

			WCHAR wsParameters[MAX_PATH] = L"\"C:\\Program Files\\Windows Photo Viewer\\PhotoViewer.dll\" ImageView_Fullscreen ";
			lstrcat(wsParameters, wsFile);
			WCHAR wsProgram[MAX_PATH] = L"rundll32.exe";
			SHELLEXECUTEINFO sei;
			ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
			sei.cbSize = sizeof(SHELLEXECUTEINFO);
			sei.fMask = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS;
			sei.hwnd = NULL;
			sei.lpVerb = L"open";
			sei.lpFile = wsProgram;
			sei.lpParameters = wsParameters;
			sei.nShow = SW_SHOWNORMAL;
			BOOL bReturn = ShellExecuteEx(&sei);
		}
	}
}
Posted
Updated 11-Mar-20 5:10am
v2
Comments
Richard MacCutchan 11-Mar-20 9:37am    
You should never hard code file paths in your applications. If you want to access a file then use one of the FileDialog classes which allows the user to select it.

As far as I can tell, you have no choice but to write the file you want to open to the drive and open it in PhotoViewer from there. PhotoViewer will not open an image from resources.

The only other alternative is to write your own photoviewer in your app where you have complete control over where it gets image data from.
 
Share this answer
 
Comments
Member 12899279 11-Mar-20 9:02am    
what if i only want to open the picture doesn't matter if its open in photoviewer or not .but i want to load pic only from resources and not from client pc
Dave Kreskowiak 11-Mar-20 11:25am    
An external application will not open an image from resources like it will from a file. There is no filepath specification that works like that.
Apparently this solution was useless so it has been removed.
 
Share this answer
 
v2
Comments
Member 12899279 11-Mar-20 12:45pm    
thanks for replying can you please elaborate a little more?i made these changes in the code but still its taking hard coded path value to open photo viewer from C drive.there's a possibility that client's os might not be on drive C then this code will break.
BOOL CreateTempName(TCHAR* szFileName)
{
TCHAR szTempPath[MAX_PATH];
BOOL bRet = FALSE;
DWORD dwLen = GetTempPath(MAX_PATH, szTempPath);
if (dwLen > 0 && dwLen < MAX_PATH)
{
if (GetTempFileName(szTempPath, TEXT("RES"), 0, szFileName))
bRet = TRUE;
}
return bRet;
}
void SaveIStreamToFile(IStream *pIStream, LPTSTR pwsFile)
{
HANDLE hFile;
LPVOID pv = NULL;
LARGE_INTEGER li;
DWORD nNbBytesWritten;
ULONG nNbBytes;
HRESULT hr;
int nBufferSize = 200;

hFile = CreateFile(pwsFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
pv = (LPSTR)LocalAlloc(LPTR, nBufferSize);
if (pv)
{
LISet32(li, 0);
pIStream->Seek(li, STREAM_SEEK_SET, NULL);
do
{
hr = pIStream->Read(pv, nBufferSize, &nNbBytes);
if (SUCCEEDED(hr))
{
if (!WriteFile(hFile, pv, nNbBytes, &nNbBytesWritten, NULL))
hr = E_FAIL;
}
} while ((SUCCEEDED(hr)) && (nNbBytes == nBufferSize));
LocalFree(pv);
}
CloseHandle(hFile);
}
return;
}
void showPic()
{
//ShellExecute(NULL, "open", "testpic.jpg", NULL, NULL, SW_SHOWNORMAL); //'bitmap1.bmp'Is the name of the resource bitmap
HRESULT(STDAPICALLTYPE * pSHCreateStreamOnDllResourceW)(LPWSTR pwszDll, LPWSTR pwszName, LPWSTR pwszType, IStream** ppstm);
HMODULE hInst = LoadLibrary("SHLWAPI.DLL");
if (hInst)
{
(FARPROC&)pSHCreateStreamOnDllResourceW = GetProcAddress(hInst, (LPCSTR)MAKEINTRESOURCE(627));
IStream *pstm;
if (pSHCreateStreamOnDllResourceW)
{
char szPath[MAX_PATH];
GetModuleFileNameA(NULL, szPath, MAX_PATH);
WCHAR wszWidePath[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszWidePath, sizeof(wszWidePath));
HRESULT hr = pSHCreateStreamOnDllResourceW(wszWidePath, (LPWSTR)MAKEINTRESOURCE(IDB_PNG1), L"PNG", &pstm);
TCHAR szTempFile[MAX_PATH];
CreateTempName(szTempFile);
//TCHAR wsFile[MAX_PATH] = TEXT("E:\\Temp\\test.jpg");
// C:\Users\CHRIST~1\AppData\Local\Temp\RESD12.tmp
SaveIStreamToFile(pstm, szTempFile);

TCHAR wsParameters[MAX_PATH] = TEXT("\"C:\\Program Files\\Windows Photo Viewer\\PhotoViewer.dll\" ImageView_Fullscreen ");
lstrcat(wsParameters, szTempFile);
TCHAR wsProgram[MAX_PATH] = TEXT("rundll32.exe");
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS;
sei.hwnd = NULL;
sei.lpVerb = TEXT("open");
sei.lpFile = wsProgram;
sei.lpParameters = wsParameters;
sei.nShow = SW_SHOWNORMAL;
BOOL bReturn = ShellExecuteEx(&sei);
}
}

}
int main()
{

thread th2(showPic);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900