Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need sample dialog based application to load PNG file on static control. please provide the code to load the PNG (bitmap)file in Dialog based application
Posted
Updated 8-Mar-13 4:13am
v4
Comments
Anurag Sinha V 8-Mar-13 5:52am    
Hi, guess you are using WinForms..
drop a picture box control to your winform, browse and add the src of the pic using the properties window..there you go.you are done...

regards
[no name] 8-Mar-13 5:55am    
i need the code to addd it dynamically
[no name] 8-Mar-13 5:54am    
i need the code to addd it dynamically
Anurag Sinha V 8-Mar-13 6:07am    
have a look at the below solution..hav tried something..

reagrds

Use the wizards to create a dialog based application, add a static picture control (style SS_BITMAP) to the dialog template and use a CStatic member variable.

C++
// CMyDialog.h 
class CMyDialog : CDialog
{
public:
    CMyDialog(CWnd* pParent = NULL);
protected:
    virtual void DoDataExchange(CDataExchange* pDX);
    void LoadPicture(LPCTSTR lpszFileName);
    CStatic m_Picture;
}

// CMyDialog.cpp
#include <atlimage.h>

void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_PICT_STATIC, m_Picture);
}

void CMyDialog::LoadPicture(LPCTSTR lpszFileName)
{
    if (lpszFileName && *lpszFileName)
    {
        CImage Image;
        // Load the image as bitmap (supports PNG files)
        if (SUCCEEDED(Image.Load(lpszFileName)))
        {
            // Pass image to static picture control
            HBITMAP hOld = m_Picture.SetBitmap(Image.Detach());
            // Release old bitmap if there was one
            if (hOld)
                VERIFY(::DeleteObject(hOld));
        }
    }
}
 
Share this answer
 
Hey Sowmya,

Add the png file to the solution in a folder, name it as Images. Edit the properties of the png file as: Enable the Copy to Output Directory option. Further enable it for the Images folder too.

then add the below code or similar. It should work for you.

C#
PictureBox picture = new PictureBox
        {
            Name = "pictureBox",
            Size = new Size(50, 50),
            Location = new Point(10,10),
            Visible=true
        };

        pic.ImageLocation = "..\Images\picture.jpg";
        Form1.Controls.Add(pic);


hope it helps.

-anurag
 
Share this answer
 
Comments
Richard MacCutchan 8-Mar-13 6:29am    
What does this have to do with a C++ Dialog?
Anurag Sinha V 8-Mar-13 6:46am    
Was it asked to do with C++?Now i see it was asked under C++ section.
Gosh I missed that, thought it would have been asked for a C# app..
Thanks for pointing it out Richard... :)
 
Share this answer
 

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