Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    136

    [C++] Visualizzare Bitmap

    ciao a tutti,
    ho creato un porgramma che apre un form (ID_FORM).

    su questo form vorrei visualizzare un immagine bmp (img1.bmp)

    Qualcuno sa dirmi come devo fare?

    Grazie a tutti!

  2. #2
    Utente di HTML.it
    Registrato dal
    Nov 2006
    Messaggi
    258
    che compilatore usi? perchè molti hanno come strumento una imagebox p simile che ti permette di inserire direttamente un'immagine...
    se no gliela fai disegnare pixel per pixel...

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    136
    utilizzo visual c++ 6.

    io ho trovato un modo in rete, solo che non riesco a utilizzarlo nel mio lavoro.

    il metodo che ho trovato è questo :

    codice:
    // *************************************************************
    //                       BMP LOAD EXAMPLE
    //         Written by Juan Soulie <jsoulie@cplusplus.com>
    // *************************************************************
    
    #include <windows.h>
    #include <fstream.h>
    
    char szAppName [] = "BMPLoad";
    
    LRESULT CALLBACK WindowProc (HWND, UINT, WPARAM, LPARAM);
    
    // **********
    // class CRaster
    //   - Generic class for BMP raster images.
    class CRaster {
    	public:
    		int Width,Height;		// Dimensions
    		int BPP;				// Bits Per Pixel.
    		char * Raster;			// Bits of the Image.
    		RGBQUAD * Palette;		// RGB Palette for the image.
    		int BytesPerRow;		// Row Width (in bytes).
    		BITMAPINFO * pbmi;		// BITMAPINFO structure
    
    		// Member functions (defined later):
    		int LoadBMP (char * szFile);
    		int GDIPaint (HDC hdc,int x,int y);
    };
    
    // **********
    // Windows Main Function. 
    //   - Here starts our demo program
    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    					LPSTR lpCmdLine, int nCmdShow )
    {
    	HWND hwnd;
    	MSG msg;
    
    	WNDCLASS wc;
    	wc.style = CS_HREDRAW|CS_VREDRAW;
    	wc.lpfnWndProc = WindowProc;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hInstance = hInstance;
    	wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
    	wc.lpszMenuName = NULL;
    	wc.lpszClassName = szAppName;
    
    	RegisterClass (&wc);
    
    	hwnd = CreateWindow (szAppName,"BMP Load",WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
    		0,0,hInstance,0);
    
    	ShowWindow (hwnd,nCmdShow);
    	UpdateWindow (hwnd);
    
    	while (GetMessage(&msg,0,0,0))
    	{
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	}
    
    	return msg.wParam;
    }
    
    // **********
    // Main Window Procedure.
    //   - Processes Window Messages
    LRESULT CALLBACK WindowProc
    	(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static CRaster bmp;
    	HDC hdc;
    	PAINTSTRUCT ps;
    
    	switch (message)
    	{
    		case WM_CREATE:
    			bmp.LoadBMP ("example.bmp");
    			return 0;
    		case WM_PAINT:
    			hdc=BeginPaint (hwnd,&ps);
    			bmp.GDIPaint (hdc,10,10);
    			EndPaint (hwnd,&ps);
    			return 0;
    		case WM_DESTROY:
    			PostQuitMessage (0);
    			return 0;
    	}
    	return DefWindowProc (hwnd,message,wParam,lParam);
    }
    
    // **********
    // CRaster::LoadBMPFile (FileName);
    //   - loads a BMP file into a CRaster object
    //   * supports non-RLE-compressed files of 1, 2, 4, 8 & 24 bits-per-pixel
    int CRaster::LoadBMP (char * szFile)
    {
    	BITMAPFILEHEADER bmfh;
    	BITMAPINFOHEADER bmih;
    
    	// Open file.
    	ifstream bmpfile (szFile , ios::in | ios::binary);
    	if (! bmpfile.is_open()) return 1;		// Error opening file
    
    	// Load bitmap fileheader & infoheader
    	bmpfile.read ((char*)&bmfh,sizeof (BITMAPFILEHEADER));
    	bmpfile.read ((char*)&bmih,sizeof (BITMAPINFOHEADER));
    
    	// Check filetype signature
    	if (bmfh.bfType!='MB') return 2;		// File is not BMP
    
    	// Assign some short variables:
    	BPP=bmih.biBitCount;
    	Width=bmih.biWidth;
    	Height= (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight; // absoulte value
    	BytesPerRow = Width * BPP / 8;
    	BytesPerRow += (4-BytesPerRow%4) % 4;	// int alignment
    
    	// If BPP aren't 24, load Palette:
    	if (BPP==24) pbmi=(BITMAPINFO*)new char [sizeof(BITMAPINFO)];
    	else
    	{
    		pbmi=(BITMAPINFO*) new char[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];
    		Palette=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));
    		bmpfile.read ((char*)Palette,sizeof (RGBQUAD) * (1<<BPP));
    	}
    	pbmi->bmiHeader=bmih;
    
    	// Load Raster
    	bmpfile.seekg (bmfh.bfOffBits,ios::beg);
    
    	Raster= new char[BytesPerRow*Height];
    
    	// (if height is positive the bmp is bottom-up, read it reversed)
    	if (bmih.biHeight>0)
    		for (int n=Height-1;n>=0;n--)
    			bmpfile.read (Raster+BytesPerRow*n,BytesPerRow);
    	else
    		bmpfile.read (Raster,BytesPerRow*Height);
    
    	// so, we always have a up-bottom raster (that is negative height for windows):
    	pbmi->bmiHeader.biHeight=-Height;
    
    	bmpfile.close();
    
    	return 0;
    }
    
    // **********
    // CRaster::GDIPaint (hdc,x,y);
    // * Paints Raster to a Windows DC.
    int CRaster::GDIPaint (HDC hdc,int x=0,int y=0)
    {
    	// Paint the image to the device.
    	return SetDIBitsToDevice (hdc,x,y,Width,Height,0,0,
    		0,Height,(LPVOID)Raster,pbmi,0);
    }
    cosi è perfetto, apre una finestra e mostra l'immagine.
    il mio problema è che io ho creato un prog con view multiple. ho una classe per ogni view, nella quale carico un form.
    io non riesco a recuperare l'handle di questp form, per poipoter chiamare correttamente :
    hdc=BeginPaint (hwnd,&ps);
    e quindi non riesco a disegnare l'immagine..

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2006
    Messaggi
    258
    non conosco la programmazione ad oggetti.. mi dispiace...

    qualcun altro sa qualcosa per dare una mano?

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,462
    Originariamente inviato da emi81
    il mio problema è che io ho creato un prog con view multiple. ho una classe per ogni view, nella quale carico un form.
    Come le hai create? Quale classe hai usato? Quale libreria?

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2006
    Messaggi
    136
    la classe di una vie è questa :

    FormPres.cpp
    codice:
    #include "stdafx.h"
    #include "WiDemo.h"
    #include "FormPres.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    extern CWiDemoApp theApp;
    
    /////////////////////////////////////////////////////////////////////////////
    IMPLEMENT_DYNCREATE(CFormPres, CColorFormView)
    
    CFormPres::CFormPres()
    	: CColorFormView(CFormPres::IDD, RGB(255, 255, 255)) //white
    {
    	//{{AFX_DATA_INIT(CFormPres)
    		// NOTE: the ClassWizard will add member initialization here
    	//}}AFX_DATA_INIT
    }
    
    CFormPres::~CFormPres()
    {
    	TRACE("destructing CFormPres\n");
    }
    
    void CFormPres:: DoDataExchange(CDataExchange* pDX)
    {
    	CFormView:: DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CFormPres)
    		// NOTE: the ClassWizard will add DDX and DDV calls here
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CFormPres, CColorFormView)
    	//{{AFX_MSG_MAP(CFormPres)
    
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CFormPres diagnostics
    #ifdef _DEBUG
    void CFormPres::AssertValid() const
    {
    	CFormView::AssertValid();
    }
    
    void CFormPres:: Dump(CDumpContext& dc) const
    {
    	CFormView:: Dump(dc);
    }
    
    #endif //_DEBUG
    /////////////////////////////////////////////////////////////////////////////

    FormPres.h
    codice:
    #if !defined(AFX_CFormPres_H__CB97F4D2_1752_11D2_B134_00C04FB9CA2B__INCLUDED_)
    #define AFX_CFormPres_H__CB97F4D2_1752_11D2_B134_00C04FB9CA2B__INCLUDED_
    
    #if _MSC_VER >= 1000
    #pragma once
    #endif // _MSC_VER >= 1000
    // FormPres.h : header file
    //
    #include "ColorFormView.h"
    
    /////////////////////////////////////////////////////////////////////////////
    // CFormPres form view
    
    #ifndef __AFXEXT_H__
    #include <afxext.h>
    #endif
    
    class CFormPres : public CColorFormView
    {
    protected:
    	CFormPres();           // protected constructor used by dynamic creation
    	DECLARE_DYNCREATE(CFormPres)
    
    // Form Data
    public:
    	//{{AFX_DATA(CFormPres)
    	enum { IDD = IDD_FORMPRES };
    		// NOTE: the ClassWizard will add data members here
    	//}}AFX_DATA
    
    // Attributes
    public:
    
    // Operations
    public:
    
    // Overrides
    	// ClassWizard generated virtual function overrides
    	//{{AFX_VIRTUAL(CFormPres)
    	protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    	//}}AFX_VIRTUAL
    
    // Implementation
    protected:
    	virtual ~CFormPres();
    #ifdef _DEBUG
    	virtual void AssertValid() const;
    	virtual void Dump(CDumpContext& dc) const;
    #endif
    
    	// Generated message map functions
    	//{{AFX_MSG(CFormPres)
    	
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    };
    
    /////////////////////////////////////////////////////////////////////////////
    
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_CFormPres_H__CB97F4D2_1752_11D2_B134_00C04FB9CA2B__INCLUDED_)
    Mentre la funzione con la quale le chiamo è questa :
    codice:
    /////////////////////////////////////////////////////////////////////////////
    //This function displays a view that is chosen from the menu.
    void CMainFrame::OnChooseView(UINT nCmdID) 
    {
    	if(nCmdID == m_nCurrentView)
    		return; //view is already selected, so don't need to destroy it and then create it again
    
    	CWnd* pWnd;
    	CWnd* pWndToDelete;
    
    	//The use of EnumChildWindows below is to find a child window that is derived from CView.
    	//GetActiveView() will return NULL if no view has focus, so we can't use it here.
    	//GetActiveDocument() also can't be used because it calls GetActiveView().
    	EnumChildWindows(m_hWnd, MyWndEnumProc, (LPARAM)&(pWnd));
    
    	if(pWnd == NULL)
    	{
    		AfxMessageBox("Problem: CMainFrame::OnChooseView can not find any views!");
    		return;
    	}
    
    	//Now that we've found a view, search up the parent tree until we come to the CMainFrame 
    	//window. Then set pWndToDelete to the CWnd that is the immediate child of CMainFrame. 
    	//This is done to find the most senior view (which may or may not have children views). 
    	//CMainFrame also has children, such as the toolbar, that we don't want to delete.
    	while( lstrcmp(pWnd->GetRuntimeClass()->m_lpszClassName, "CMainFrame") )
    	{
    		pWndToDelete = pWnd;
    		pWnd = pWnd->GetParent();
    	}
    
    	CRuntimeClass* pNewViewRTClass;
    	switch(nCmdID)
    	{
    		case ID_WORKS_SEQUENTIAL:
    			pNewViewRTClass = RUNTIME_CLASS(CFormSeq);
    			break;
    		case ID_WORKS_SINGLEDOC:
    			pNewViewRTClass = RUNTIME_CLASS(CFormSing);
    			break;
    		case IDD_FORMPRES:
    			pNewViewRTClass = RUNTIME_CLASS(CFormPres);
    			break;
    		default:
    			ASSERT(0);
    			return;
    	}
    	
    	//Create the new view:
    	CCreateContext context;
    	context.m_pNewViewClass = pNewViewRTClass;
    
    	m_pNewView = STATIC_DOWNCAST(CView, CreateView(&context));
    	if(m_pNewView != NULL)
    	{
    		m_nCurrentView = nCmdID; 
    
    		//destroy the old view:
    		pWndToDelete->DestroyWindow(); 
    
    		//OnInitialUpdate() is not called as a result of calling CreateView() above. 
    		//It is not always called by the framework, so it is called here:
    		m_pNewView->OnInitialUpdate(); 
    
    		//Show and activate view:
    		m_pNewView->ShowWindow(SW_SHOW); 
    		SetActiveView(m_pNewView);
    		RecalcLayout();
    	}
    	else
    	{
    		AfxMessageBox("problem creating a view in CMainFrame::OnChooseView()");
    	}
    }

  7. #7
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,296

    Moderazione

    Originariamente inviato da emi81
    Qualcuno sa dirmi come devo fare?
    In futuro, evitare di aprire discussioni duplicate, dato che avevi già aperto una discussione analoga qui.
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.