how to display photo

i want to show a photo in my graphical interface, while the program is still , but the photo is not displayed. (i use windows.h library). please tell me how to fix it. Thanks
How can anyone tell you how to fix it, when you won't show us your code?
This can be done in multiple ways, the simple one is to create image resource in Visual Studio, this is the first step to embed and image into executable.

Once done you need to load the image from executable into a DirectX Bitmap pointer, below is a tested and working function to load resource image from executable:

Once loaded you can use DirectX API's to draw the image onto window. very simple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// error checking function, implementation not given but
// you will surely need good error checking code. let me know if so.
void ShowError(std::string file, int line, HRESULT hr = S_OK);

#include <Windows.h>
#include <wincodec.h>	// WIC
#include <d2d1.h>		// ID2D1Bitmap
//
// Loads resource Image from executable
// into ID2D1Bitmap* pointer
// HRESULT checked
//
template<typename RenderType>
HRESULT LoadResourceImage(
	IWICImagingFactory* pFactory,
	PCTSTR szFilename,
	PCTSTR szFileType,
	RenderType* pRenderTarget,
	ID2D1Bitmap** ppBitmap)
{
	HRESULT hr = S_OK;
	DWORD dwImageSize = 0;
	HMODULE hModule = GetModuleHandle(nullptr);

	HRSRC hResource = nullptr;
	HGLOBAL hResourceData = nullptr;
	void* pImageFile = nullptr;
	IWICStream* pStream = nullptr;
	IWICFormatConverter* pConverter = nullptr;
	IWICBitmapFrameDecode* pFrameDecode = nullptr;
	IWICBitmapDecoder* pDecoder = nullptr;

	if (!hModule)
	{
		ShowError(__FILENAME__, __LINE__);
		goto done;
	}

	hResource = FindResource(hModule, szFilename, szFileType);
	if (!hResource)
	{
		ShowError(__FILENAME__, __LINE__);
		goto done;
	}

	if (FAILED(hr = hResource ? S_OK : E_FAIL))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	dwImageSize = SizeofResource(hModule, hResource);
	if (!dwImageSize)
	{
		ShowError(__FILENAME__, __LINE__);
		goto done;
	}

	if (FAILED(hr = dwImageSize ? S_OK : E_FAIL))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	hResourceData = LoadResource(hModule, hResource);
	if (!hResourceData)
	{
		ShowError(__FILENAME__, __LINE__);
		goto done;
	}

	if (FAILED(hr = hResourceData ? S_OK : E_FAIL))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	pImageFile = LockResource(hResourceData);
	if (!pImageFile)
	{
		ShowError(__FILENAME__, __LINE__);
		goto done;
	}

	if (FAILED(hr = pImageFile ? S_OK : E_FAIL))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	if (FAILED(hr = pFactory->CreateStream(&pStream)))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	hr = pStream->InitializeFromMemory(
		reinterpret_cast<BYTE*>(pImageFile), dwImageSize);

	if (FAILED(hr))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	hr = pFactory->CreateDecoderFromStream(
		pStream,
		nullptr,
		WICDecodeMetadataCacheOnDemand,
		&pDecoder);

	if (FAILED(hr))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	if (FAILED(hr = pDecoder->GetFrame(0, &pFrameDecode)))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	if (FAILED(hr = pFactory->CreateFormatConverter(&pConverter)))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	hr = pConverter->Initialize(
		pFrameDecode,
		GUID_WICPixelFormat32bppPRGBA,
		WICBitmapDitherTypeNone,
		nullptr,
		0.f,
		WICBitmapPaletteTypeCustom);

	if (FAILED(hr))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

	hr = pRenderTarget->CreateBitmapFromWicBitmap(
		pConverter,
		0,
		ppBitmap);

	if (FAILED(hr))
	{
		ShowError(__FILENAME__, __LINE__, hr);
		goto done;
	}

done:
	SafeRelease(&pFrameDecode);
	SafeRelease(&pDecoder);
	SafeRelease(&pConverter);
	SafeRelease(&pStream);

	return hr;
}


Here is pseudo unchecked code to load the image into a pointer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MyClass
{
      public:
           ID2D1Bitmap* mpBitmap;
           ID2D1HwndRenderTarget* mpRenderWindow;
           CComPtr<IWICImagingFactory2> mpWICFactory;
};

MyClass::MyClass()
{
        if (!mpBitmap)
        {
	       LoadResourceImage(
		       mpWICFactory,
                      // this must be declared in resource.h
		       MAKEINTRESOURCE(ID_OF_THE_IMAGE),
		      TEXT("PNG"),     // assuming image type is PNG
		      mpRenderWindow,
		     &mpBitmap);
        }
}


Note that you will need to initialize WIC factory and DirectX factory to create necessary interfaces.

Here is example tested and working code how to do that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	HRESULT hr = CoInitializeEx(nullptr,
		COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

	if(FAILED(hr))
	{
		// handle error here
	}

	if(SUCCEEDED(hr))
	{
		// Factories are created once for the duration of application
		CComPtr<ID2D1Factory> pDirectFactory = nullptr;
		CComPtr<IWICImagingFactory2> pWICFactory = nullptr;

		// create direct x factory
		hr = D2D1CreateFactory(
			D2D1_FACTORY_TYPE_SINGLE_THREADED,
			&pDirectFactory);

		if (FAILED(hr))
		{
			// handle error here
		}

		// create WIC factory
		hr = CoCreateInstance(
			CLSID_WICImagingFactory,
			nullptr,
			CLSCTX_INPROC_SERVER,
			IID_PPV_ARGS(&pWICFactory));

		if (FAILED(hr))
		{
			// handle error here
		}
        }


You will also need to include at minimum following headers:
1
2
3
4
#include <Windows.h>	// win32 API
#include <d2d1.h>		// direct2d
#include <wincodec.h>	// WIC
#include <atlbase.h>	// com smart pointer 


Here is example pseudo unchecked code how to create interfaces:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// create device dependent resources
//
HRESULT MyClass::CreateGraphicsResources()
{
	HRESULT hr = S_OK;

	if (!mpRenderWindow)
	{
		RECT rc;
		GetClientRect(mHwnd, &rc);
		D2D1_SIZE_U size = D2D1::SizeU(
			static_cast<UINT32>(rc.right),
			static_cast<UINT32>(rc.bottom));

		hr = mpDirectFactory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(),
			D2D1::HwndRenderTargetProperties(mHwnd, size),
			&mpRenderWindow);

		if (!mpBrush && SUCCEEDED(hr))
		{
			hr = mpRenderWindow->CreateSolidColorBrush(
				D2D1::ColorF(D2D1::ColorF::Black),
				&mpBrush
			);

			if (FAILED(hr))
			{
				// handle error
				return hr;
			}
		}

		if (FAILED(hr))
		{
			// handle error
		}
	}
}


This is enough to get you started, Let me know how it goes and if you need additional help.

EDIT:
I posted sample error checking code here, it will help to to debug errors which you will surely encounter:
http://www.cplusplus.com/forum/windows/254826/

EDIT2:
here is missing SafeRelease function:
1
2
3
4
5
6
7
8
9
10
11
12
13
//
// used to check COM pointer first
// and only then release it
//
template <typename T>
inline void SafeRelease(T** ppT)
{
	if (*ppT)
	{
		(*ppT)->Release();
		(*ppT) = nullptr;
	}
}

Last edited on
i want to show a photo

In case it is for nothing else but to dispaly it you may use a ready to use viewer like IrfanView -- https://www.irfanview.com
If your photo should serve as faceplate for your program, you may find a working example here: https://hp.giesselink.com/v41.htm Find in file HP41Keyboard.cpp some details.
Topic archived. No new replies allowed.