Unable to display a PNG file in a Window with the GDI.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I am programming a simple PNG viewer. I use Win32, GDI, C++. The viewer waits until the user select the File menu item. Then it displays a PNG image in the client area of the root window. Currently the filename is hardcoded. Currently the viewer
can display the image but there 2 minor problems:
1- the image is displayed bottom up;
2- The colors arent what the should be;
I joined the source code so you can give me advices or ask questions. You should inside the
<span style="font-size:x-small
<span style="font-size:x-small
case WM_COMMAND:
<font size="2" style="font-size:x-small

</font>in the window procedure.
I am working under windows 7 but I think it does not matter.
I thank you very much for any help.
TremblayE
==================== source code===============================================
<span style="font-size:x-small
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
<font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small static<span style="font-size:x-small
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small bool<span style="font-size:x-small display =
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small false<font size="2 ;</font><span style="font-size:x-small <font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small static<font size="2 BITMAPINFOHEADER bmpInfoHdr;</font><span style="font-size:x-small
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hBitmap = NULL;
FILE* fp = NULL;
<font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small int<font size="2 pngImage = 0;</font><span style="font-size:x-small <font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small int<font size="2 bitDepth = 0;</font><span style="font-size:x-small <font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small int<font size="2 colorType;</font><span style="font-size:x-small
ulg height, width;
<font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small int<font size="2 channels;</font><span style="font-size:x-small
uch* image_data = NULL;
uch red, green, blue;
ulg rowbytes = 0;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_bytepp row_pointers = NULL;
errno_t err = 0;
<font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small switch<font size="2 (Msg)</font><span style="font-size:x-small
{
<font size="2" style="font-size:x-small

</font><font size="2" color="#008000 <font size="2" color="#008000 // Process command received when menu item File is selected.</font></font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small <span style="font-size:x-small <font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small case<font size="2 WM_COMMAND:</font><span style="font-size:x-small <font size="2" style="font-size:x-small

</font><font size="2" color="#008000 <font size="2" color="#008000 // display the png image in the client area.</font></font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small <span style="font-size:x-small <font size="2" style="font-size:x-small
display =
</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small true<font size="2 ;</font><span style="font-size:x-small <font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small if<span style="font-size:x-small ((fp = fopen(<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "Maps-for-free_Sierra_Nevada.png"<span style="font-size:x-small ,
<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "rb"<font size="2 )) == NULL)</font><span style="font-size:x-small
{
_get_errno( &err );
<font size="2" style="font-size:x-small
printf(
</font><span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "errno = %dn"<font size="2 , err );</font><span style="font-size:x-small <font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small return<font size="2 0;</font><span style="font-size:x-small
}
readpng_init(fp, &width, &height, &bitDepth, &colorType);
readpng_get_bgcolor(&red, &green, &blue);
image_data = (uch *)readpng_get_image(2.2, &channels, &rowbytes);
<font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small if<font size="2 (fclose(fp) != 0)</font><span style="font-size:x-small
{
<font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small return<font size="2 0;</font><span style="font-size:x-small
}
hdcWindow = GetDC(hWnd);
<font size="2" style="font-size:x-small

</font><font size="2" color="#008000 <font size="2" color="#008000 // Create a compatible DC which is used in a BitBlt from the window DC</font></font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small <span style="font-size:x-small
hdcMemDC = CreateCompatibleDC(hdcWindow);
<font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small if<font size="2 (!hdcMemDC)</font><span style="font-size:x-small
{
<font size="2" style="font-size:x-small
MessageBox(hWnd, L
</font><span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "CreateCompatibleDC has failed"<span style="font-size:x-small ,L<span style="color:#a31515; font-size:x-small <span style="color:#a31515; font-size:x-small "Failed"<font size="2 ,
MB_OK);</font><span style="font-size:x-small
}
<font size="2" style="font-size:x-small

</font><font size="2" color="#008000 <font size="2" color="#008000 // Get the client area for size calculation</font></font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small <span style="font-size:x-small
RECT rcClient;
GetClientRect(hWnd, &rcClient);
<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small <font size="2" color="#0000ff" style="color:#0000ff; font-size:x-small
#if
</font>
<span style="font-size:x-small 1 <span style="color:#008000; font-size:x-small
<span style="color:#008000; font-size:x-small // new try with the DIB functions.


<span style="font-size:x-small <font size="2" style="font-size:x-small
bmpInfoHdr.biSize =
</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small sizeof<span style="font-size:x-small (BITMAPINFOHEADER);<font size="2" color="#008000 <font size="2" color="#008000 // not sure</font></font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small <span style="font-size:x-small
bmpInfoHdr.biWidth = width;
bmpInfoHdr.biHeight = height;
bmpInfoHdr.biPlanes = 1;
bmpInfoHdr.biBitCount = 3*bitDepth;
<font size="2" style="font-size:x-small
bmpInfoHdr.biCompression = BI_RGB;
</font><font size="2" color="#008000 <font size="2" color="#008000 // not sure //BI_PNG == not valid;</font></font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small <span style="font-size:x-small
bmpInfoHdr.biSizeImage = width * height;
bmpInfoHdr.biXPelsPerMeter = 3959;
bmpInfoHdr.biYPelsPerMeter = 3959;
<font size="2" style="font-size:x-small
bmpInfoHdr.biClrUsed = 2^(3*bitDepth);
</font><font size="2" color="#008000 <font size="2" color="#008000 // not sure</font></font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small <span style="font-size:x-small
bmpInfoHdr.biClrImportant = 0;
BITMAPINFO bmpInfos;
bmpInfos.bmiHeader = bmpInfoHdr;
<font size="2" style="font-size:x-small

</font><font size="2" color="#008000 <font size="2" color="#008000 //bmpInfos.bmiColors = (RGBQUAD)NULL;/* not sure*/</font></font><span style="color:#008000; font-size:x-small <span style="font-size:x-small

<font size="2" style="font-size:x-small
SetDIBitsToDevice(hdcWindow, 0, 0, (rcClient.right-rcClient.left), (rcClient.bottom-rcClient.top), 0
</font><span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small /*not sure*/<span style="font-size:x-small , 0<span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small /*not
sure*/<span style="font-size:x-small , 0, (height-1), (<span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small void<span style="font-size:x-small *)image_data, &bmpInfos, DIB_RGB_COLORS<span style="color:#008000; font-size:x-small <span style="color:#008000; font-size:x-small /*not
sure*/<font size="2 );</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small
#endif
<span style="font-size:x-small <font size="2" style="font-size:x-small

</font><span style="color:#0000ff; font-size:x-small <span style="color:#0000ff; font-size:x-small break<font size="2 ;</font>








View the full article
 
Back
Top