Grafiken importieren unter Visual C++ ?



  • Hallo zusammen,
    könnt Ihr mir ein paar Tipps geben, wie zum Geier ich Grafiken (am liebsten wären mir PNGs, aber BMP oder TGA wären auch super) mit Visual C++ verarbeiten kann!? Also eine PNG-Datei einladen, im Fenster darstellen und wohlmöglich auch die Pixel in einem Array ablegen?

    Hilfe - ich bin ECHT verzweifelt! 😕

    Am besten Ihr redet mit mir, wie mit einer Idiotin 😃

    Idealerweise müsste das eine autonome Lösung sein, die also nicht DirectX oder ActiveX oder irgendeine abgefahrene Dritthersteller-Library verwendet oder hat Microsoft so etwas schon standardmäßig vorgesehen und ich Deppin finde das einfach nicht?

    Ich wäre unendlich dankbar, wenn mir jemand eine Lösung präsentieren könnte, bei der - wenn man auf einen Knopf (in seiner Dialogbox) drückt - ein neues Fenster aufgeht, mit dargestelltem Bild.

    Liebe Grüße Angelika

    PS: Ich verwende Microsoft Visual C++ 6.0



  • such mal nach libpng



  • Danke, hilft aber nicht weiter - höchstens meine Verzweiflung zu steigern...



  • Hoffe du fängst etwas an damit.

    Für Dich egentlich nur die Funktion
    void LoadPictureFile(LPCTSTR szFile);
    Interessant.

    // LoadPic.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "resource.h"
    #include <shlobj.h>
    
    #define MAX_LOADSTRING  100
    //  2540
    #define HIMETRIC_INCH    2540
    #define MAP_LOGHIM_TO_PIX(x,ppli)   ( ((ppli)*(x) + HIMETRIC_INCH/2) / HIMETRIC_INCH )
    #define WM_USER_SETSCROLLS    (WM_USER + 1)
    #define MAXFILES 86
    // Global Variables:
    HINSTANCE hInst;                                // current instance
    TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];            // The title bar text
    LPPICTURE gpPicture;
    HWND ghWnd;
    CStringArray m_filenamearray;
    
    void LoadPictureFile(LPCTSTR szFile);
    int ladefiles(CString nextdir);
    
    int i = 0;
    int intervall = 2000;
    CString m_direktory;
    
    // Foward declarations of functions included in this code module:
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    BOOL                CALLBACK passwortDlgProc(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
        // TODO: Place code here.
        MSG msg;
    
        // Initialize global strings
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
        LoadString(hInstance, IDC_LOADPIC, szWindowClass, MAX_LOADSTRING);
        MyRegisterClass(hInstance);
    
        // Perform application initialization:
        if (!InitInstance (hInstance, nCmdShow)) 
        {
            return FALSE;
        }
    
        // Main message loop:
        while (GetMessage(&msg, NULL, 0, 0)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        if (gpPicture)
            gpPicture->Release();
    
        return msg.wParam;
    }
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage is only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
        WNDCLASSEX wcex;
    
        wcex.cbSize = sizeof(WNDCLASSEX); 
    
        wcex.style          = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
    
        wcex.lpfnWndProc    = (WNDPROC)WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, (LPCTSTR)IDI_LOADPIC);
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+2);
    //  wcex.lpszMenuName   = (LPCSTR)IDC_LOADPIC;
        wcex.lpszClassName  = szWindowClass;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
    
        return RegisterClassEx(&wcex);
    }
    
    //
    //   FUNCTION: InitInstance(HANDLE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
       gpPicture = NULL;
    
    //   ghWnd = hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW ,
    //    CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
      ghWnd = hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP  ,
         0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) , NULL, NULL, hInstance, NULL);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
    //  ShowWindow(hWnd, nCmdShow);
    //  UpdateWindow(hWnd);
    
    i = 1;
    
        ShowWindow(hWnd,SW_MAXIMIZE);
        UpdateWindow(hWnd);
    DialogBox(hInst,TEXT ("DIALOG1"),hWnd,passwortDlgProc);
    
    ladefiles(m_direktory);
    
        SetTimer(hWnd,1,intervall,NULL);
    
        return TRUE;
    }
    
    // This function loads a file into an IStream.
    void LoadPictureFile(LPCTSTR szFile)
    {
        // open file
    
        HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
        _ASSERTE(INVALID_HANDLE_VALUE != hFile);
    
        // get file size
        DWORD dwFileSize = GetFileSize(hFile, NULL);
        _ASSERTE(-1 != dwFileSize);
    
        LPVOID pvData = NULL;
        // alloc memory based on file size
        HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
        _ASSERTE(NULL != hGlobal);
    
        pvData = GlobalLock(hGlobal);
        _ASSERTE(NULL != pvData);
    
        DWORD dwBytesRead = 0;
        // read file and store in global memory
        BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
        _ASSERTE(FALSE != bRead);
        GlobalUnlock(hGlobal);
        CloseHandle(hFile);
    
        LPSTREAM pstm = NULL;
        // create IStream* from global memory
        HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
        _ASSERTE(SUCCEEDED(hr) && pstm);
    
        // Create IPicture from image file
        if (gpPicture)
            gpPicture->Release();
        hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
        _ASSERTE(SUCCEEDED(hr) && gpPicture);   
        pstm->Release();
    
        InvalidateRect(ghWnd, NULL, TRUE);
    }
    
    //
    //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND  - process the application menu
    //  WM_PAINT    - Paint the main window
    //  WM_DESTROY  - post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
        static HMENU hMenu;
        POINT point;
        CString file;
        CString zaehler;
          static int          cxClient, cyClient, iVscroll, iHscroll ;
    
        switch (message) 
        {
            case WM_CREATE:
            hMenu = LoadMenu(hInst,(LPCTSTR)IDC_LOADPIC);
            hMenu = GetSubMenu(hMenu,0);
            return 0;
    
            case WM_COMMAND:
            wmId    = LOWORD(wParam); 
            wmEvent = HIWORD(wParam); 
                // Parse the menu selections:
    
            switch (wmId)
            {
                case IDM_DIR:
                DialogBox(hInst,TEXT ("DIALOG1"),hWnd,passwortDlgProc);
                break;
    
                case IDM_KLEIN:
                SetTimer(hWnd,2,15000,NULL);
                ShowWindow(hWnd,SW_HIDE);
                UpdateWindow(hWnd);
                break;
    
                case IDM_OPEN:
                {
                    KillTimer(hWnd,1);
                    // get file name to open
    
                    TCHAR szFile[MAX_PATH];
                    ZeroMemory(szFile, MAX_PATH);
                    OPENFILENAME ofn;
                    ZeroMemory(&ofn, sizeof(OPENFILENAME));
                    ofn.lStructSize = sizeof(OPENFILENAME);
                    ofn.Flags       = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
                    ofn.hwndOwner   = hWnd;
                    ofn.lpstrFilter = _T("Supported Files Types(*.bmp;*.gif;*.jpg;*.ico;*.emf;*.wmf)\0*.bmp;*.gif;*.jpg;*.ico;*.emf;*.wmf\0Bitmaps (*.bmp)\0*.bmp\0GIF Files (*.gif)\0*.gif\0JPEG Files (*.jpg)\0*.jpg\0Icons (*.ico)\0*.ico\0Enhanced Metafiles (*.emf)\0*.emf\0Windows Metafiles (*.wmf)\0*.wmf\0\0");
                    ofn.lpstrTitle  = _T("Open Picture File");
                    ofn.lpstrFile   = szFile;
                    ofn.nMaxFile    = MAX_PATH;
                    if (IDOK == GetOpenFileName(&ofn))
                    // load the file
                    LoadPictureFile(szFile);
                }
            break;
    
            case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
    
            default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    
        case WM_LBUTTONDBLCLK:
        KillTimer(hWnd,1);
        break;
        case WM_MBUTTONUP:
        SetTimer(hWnd,1,intervall,NULL);
        break;
        case WM_RBUTTONUP:
        point.x = LOWORD(lParam);
        point.y = HIWORD(lParam);
        ClientToScreen(hWnd,&point);
        TrackPopupMenu (hMenu,TPM_LEFTALIGN |TPM_RIGHTBUTTON,point.x,point.y,0,hWnd,NULL);
        return 0;
    
        case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
    
        if (gpPicture)
            {
                // get width and height of picture
                long hmWidth;
                long hmHeight;
                gpPicture->get_Width(&hmWidth);
                gpPicture->get_Height(&hmHeight);
                // convert himetric to pixels
                RECT rc;
                GetClientRect(hWnd, &rc);
    
                int nWidth  = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
                int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
    
                if (nWidth > nHeight) 
                {
                    int percent = (rc.bottom - nWidth) * 100 / (rc.bottom);
                    nWidth = nWidth + ((nWidth * percent) / 100);
                    nHeight = nHeight + ((nHeight * percent) / 100);
                }
                else
                {
                    int percent = (rc.right - nHeight) * 100 / (rc.right);
                    nWidth = nWidth + ((nWidth * percent) / 100);
                    nHeight = nHeight + (nHeight * percent / (100));
    
                }
    
                // display picture using IPicture::Render
                gpPicture->Render(hdc, (rc.right-nWidth)/2, (rc.bottom-nHeight)/2, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);
    
        }
        EndPaint(hWnd, &ps);
        break;
    
        case WM_DESTROY:
        KillTimer(hWnd,1);
        KillTimer(hWnd,2);
        PostQuitMessage(0);
        break;
    
        case WM_TIMER:
    
        switch (wParam)
        {
            case 1:
            zaehler.Format("%i",i);
            file = m_filenamearray.GetAt(i);
            KillTimer(hWnd,1);
            LoadPictureFile(file);
            SetTimer(hWnd,1,intervall,NULL);
            i++;
            if (i >= m_filenamearray.GetSize()) i = 0;
            break;
    
            case 2:
            KillTimer(hWnd,2);
            ShowWindow(hWnd,SW_SHOWMAXIMIZED);
            UpdateWindow(hWnd);
            break;
        }
        break;
    
        default:
        return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }
    
    BOOL CALLBACK passwortDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
        HWND  htext;
    
        int ilength = 0;
    
        switch (message)
        {
        case WM_INITDIALOG:
            return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case IDOK:
                {
                    TCHAR intervallvar[20];
                    TCHAR passwort[10];
                    CString passwortergebniss;
                    htext = GetDlgItem(hDlg,IDC_PASSWORT);
                    ilength = GetWindowTextLength(htext);
                    GetWindowText(htext,passwort,ilength+1);
                    passwortergebniss = passwort;
                        if (passwortergebniss == "test")
                        {
                            htext = GetDlgItem(hDlg,IDC_EDIT2);
                            ilength = GetWindowTextLength(htext);
                            GetWindowText(htext,intervallvar,ilength+1);
                            intervall = atoi(intervallvar);
                            intervall *=1000;
                            EndDialog(hDlg,0);
                            return TRUE;
                        } else
                        {
                                PostQuitMessage(0);
    
                        }
    
                }
    
            case IDC_BUTTON1:
                {
                    KillTimer(ghWnd,1);
                    TCHAR passwort[10];
                    CString passwortergebniss;
                    htext = GetDlgItem(hDlg,IDC_PASSWORT);
                    ilength = GetWindowTextLength(htext);
                    GetWindowText(htext,passwort,ilength+1);
                    passwortergebniss = passwort;
                        if (passwortergebniss == "test")
                        {
                            TCHAR szFile[MAX_PATH];
                            ZeroMemory(szFile, MAX_PATH);
                            OPENFILENAME ofn;
                            ZeroMemory(&ofn, sizeof(OPENFILENAME));
                            ofn.lStructSize = sizeof(OPENFILENAME);
                            ofn.Flags       = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
                            ofn.hwndOwner   = hDlg;
                            ofn.lpstrFilter = _T("Supported Files Types(*.bmp;*.gif;*.jpg;*.ico;*.emf;*.wmf)\0*.bmp;*.gif;*.jpg;*.ico;*.emf;*.wmf\0Bitmaps (*.bmp)\0*.bmp\0GIF Files (*.gif)\0*.gif\0JPEG Files (*.jpg)\0*.jpg\0Icons (*.ico)\0*.ico\0Enhanced Metafiles (*.emf)\0*.emf\0Windows Metafiles (*.wmf)\0*.wmf\0\0");
                            ofn.lpstrTitle  = _T("Open Picture File");
                            ofn.lpstrFile   = szFile;
                            ofn.nMaxFile    = MAX_PATH;
                            if (IDOK == GetOpenFileName(&ofn))
                            // load the file
                            LoadPictureFile(szFile);
                        }
    
                }
            case IDC_BUTTON2:
                TCHAR bildzahl[5];
    
                htext = GetDlgItem(hDlg,IDC_BILD);
                ilength = GetWindowTextLength(htext);
                GetWindowText(htext,bildzahl,ilength+1);
                i = atoi(bildzahl);
    
            case IDC_DIR:
                {
    
                    TCHAR passwort[10];
                    CString passwortergebniss;
                    htext = GetDlgItem(hDlg,IDC_PASSWORT);
                    ilength = GetWindowTextLength(htext);
                    GetWindowText(htext,passwort,ilength+1);
                    passwortergebniss = passwort;
                        if (passwortergebniss == "test")
                        {
                            char chName[MAX_PATH];
                            BROWSEINFO bi = {0};    // Path-Dialog
                            bi.hwndOwner = ghWnd;
                            LPITEMIDLIST lpIDList = ::SHBrowseForFolder(&bi);
                            if (lpIDList) 
                            {
                                ::SHGetPathFromIDList(lpIDList, chName);
    
                                m_direktory = chName;
                                if (m_direktory == "") m_direktory = "c:\\";
    
                            }
    
                        }
                        else
                        {
                            PostQuitMessage(0);
                        }
    
                }
    
            case IDCANCEL:
                EndDialog(hDlg,0);
                return TRUE;
            }
            break;
        }
    
    return FALSE;
    }
    
    int ladefiles(CString nextdir)
    {
    
        CString curdir = nextdir;
        CString appname;
        CString ftpfilename;
        CString winfilename;
        CString filename;
    
        CFile debugtext;
        CString neuezeile;
    //  int x = 0;
    
        CFileFind ftpFind;
    
            BOOL bContinue = ftpFind.FindFile(curdir + "\\*.*");
    
    //  debugtext.Open("Debug.txt", CFile::modeCreate | CFile::modeNoTruncate);
    //  debugtext.Close();
    
    //  if(!debugtext.Open("Debug.txt", CFile::modeWrite))
    //  {
    //      meldung.Format("%s kann nicht geöffnet werden", speichern);
    //      m_pOwner->MessageBox(meldung, "Logdaten", MB_ICONEXCLAMATION);
    //  }
    
    while(bContinue > 0)
    {
        bContinue = ftpFind.FindNextFile();
    
        appname = ftpFind.GetFileName();
    
        if (appname == "." || appname == "..")
        {
        }
        else
        {
    
            if (ftpFind.IsDirectory() != 0)
            {
                nextdir = curdir + "\\" + appname;
                ladefiles(nextdir);
            }
            else
            {
            if (ftpFind.GetFileName().Right(3) == "bmp" ||
                ftpFind.GetFileName().Right(3) == "gif" ||
                ftpFind.GetFileName().Right(3) == "jpg" ||
                ftpFind.GetFileName().Right(3) == "ico" ||
                ftpFind.GetFileName().Right(3) == "emf" ||
                ftpFind.GetFileName().Right(3) == "wmf")
            {
                m_filenamearray.Add(ftpFind.GetFilePath());
            }
    
            }
        }
    
    }
    
    //debugtext.Close();
    
    ftpFind.Close();
    
    return 1;
    
    }
    


  • Meinst du jetzt Grafiken in ein Projekt mit einbinden oder aus einer Datei laden?


Anmelden zum Antworten