tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
1
ZUGRIFFE
1104
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    rainer82 rainer82 ist offline Mitglied
    Registriert seit
    Aug 2009
    Beiträge
    13
    ich lade eine Binaere Datei folgendermassen:

    Code :
    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
    
    bool CImageHandler::BuildImageMatrix(std::vector<CString> fList,CString srcpath)
    {
     std::vector<CString>::iterator fIterator;
     CString fName;
     CStringA anssrcpath(srcpath);
     FILE* fPointer=NULL;
     size_t result;
     int count=0,i=372;
     unsigned __int8 buffer[BUFFERLENGTH];
     
     for(fIterator=fList.begin(); fIterator!=fList.end(); fIterator++)
      {
        fName=*fIterator;
        if(fName.Right(3)=="ptd")
      {
     CStringA ansfName(fName);
     errno_t err;
     //load file in binary mode
     err=fopen_s(&fPointer,anssrcpath+ansfName,"rb");
     if(fPointer==NULL)
      {
        MessageBox(/*NULL,*/_T("File: ")+srcpath+fName+_T(" is invalid!"),_T("Error"),MB_ICONERROR|MB_OK);
        return false;
      }             
     // copy the file into the buffer:
     fgetc(fPointer);
     result=fread(buffer,sizeof(unsigned __int8),BUFFERLENGTH,fPointer);
     // terminate
     fclose(fPointer);
     if(!CreateBitmapFromArray(buffer))
      {
        free(buffer);
        return false;
      }
    }
            }
     return true;
    }

    Dann mach ich Folgendes:
    Code :
    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
    
    bool CImageHandler::CreateBitmapFromArray(unsigned __int8 buf[])
    {
     CPaintDC dc(AfxGetMainWnd()); // device context for painting
        
     DWORD dwBits= (DWORD)buf;
     
     BITMAPINFO bmi;
     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
     bmi.bmiHeader.biWidth = 4;
     bmi.bmiHeader.biHeight = 4;
     bmi.bmiHeader.biPlanes = 1;
     bmi.bmiHeader.biBitCount = 32;
     bmi.bmiHeader.biCompression = BI_RGB;
     bmi.bmiHeader.biSizeImage = 0;
     bmi.bmiHeader.biXPelsPerMeter = 0;
     bmi.bmiHeader.biYPelsPerMeter = 0;
     bmi.bmiHeader.biClrUsed = 0;
     bmi.bmiHeader.biClrImportant = 0;
     
     CBitmap bitmap;
     bitmap.CreateCompatibleBitmap(&dc,4,4);
     ::SetDIBits(dc.m_hDC,bitmap,0,4,&dwBits,&bmi,DIB_RGB_COLORS);
     CDC dcMemory;
     dcMemory.CreateCompatibleDC(&dc);
     CBitmap * pOldBitmap = dcMemory.SelectObject(&bitmap);
     dc.BitBlt(0, 0, 4, 4, &dcMemory, 0, 0, SRCCOPY);
     dcMemory.SelectObject(pOldBitmap);
     
    return true;
    }

    Die Klasse Imagehandler ist abgeleitet von CDialog.

    Wie stelle ich nun die Bitmapdatei auf meinem Dialog dar?
    Gibt es da einfachere, fuer Anfaenger wie mich, verstaendlichere Wege?
    Fuer allgemeine, konstruktive Kritik was den Code angeht waehre ich auch dankbar .

    Gruss Christian
    Geändert von rainer82 (17.08.09 um 01:39 Uhr)
     

  2. #2
    RuFFnEcK RuFFnEcK ist offline Mitglied Gold
    Registriert seit
    Jan 2004
    Beiträge
    203
    Hi ich hab grad nicht viel zeit und hab mir deinen Code nicht angeschaut, aber will dennoch helfen
    Deine Frage ist in einem MFC Dialog ein Bitmap zu zeichnen?

    Dann nimm die folgende Klasse und erzeuge dir eine PictureBox und nutze diese Klasse:

    PictureBox.h
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    #pragma once
     
    class PictureBox : public CStatic
    {   
        DECLARE_DYNAMIC(PictureBox)
     
    public:
        PictureBox(void);
        virtual ~PictureBox(void);
        void SetBitmap(CString strBitmap);      
    protected:    
        DECLARE_MESSAGE_MAP()    
        void ShowBitmap(CPaintDC *pDC);       
        CString m_sBitmap;    
        CBitmap m_bmpBitmap;    
        BITMAP bm;   
     
    public:    
        afx_msg void OnPaint();
    };

    PictureBox.cpp
    Code :
    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
    
    #include "StdAfx.h"
    #include "PictureBox.h"
     
    IMPLEMENT_DYNAMIC(PictureBox, CStatic)   
     
    PictureBox::PictureBox()   
    {   
        m_sBitmap = "";
    }      
     
    PictureBox::~PictureBox()   
    {   
    }         
     
    BEGIN_MESSAGE_MAP(PictureBox, CStatic)    
        ON_WM_PAINT()   
    END_MESSAGE_MAP()            
     
    // PictureBox message handlers      
    void PictureBox::ShowBitmap(CPaintDC *pdc)    
    {    
        //Create a device context to load the bitmap into    
        CDC dcMem;    
        dcMem.CreateCompatibleDC(pdc);       
        //Get the Display area available    
        CRect lRect;    
        GetClientRect(lRect);    
        lRect.NormalizeRect();       
        //select the bitmap into compatible device context    
        CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(&m_bmpBitmap);    
        //m_bmpBitmap.SetBitmapDimension(lRect.Width(),lRect.Height());       
        
        //copy & resize the window to the dialog window    
        pdc->StretchBlt(0,0,lRect.Width(),lRect.Height(),&dcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);   
    } 
     
    void PictureBox::OnPaint()   
    {    
        CPaintDC dc(this); 
        // device context for painting       
        RECT rect;    
        GetClientRect(&rect);    
        dc.FillSolidRect(&rect, RGB(255,255,255));       
        if(m_sBitmap!="")     
            ShowBitmap(&dc);   
    }   
     
    void PictureBox::SetBitmap(CString strBitmap)   
    {    
        m_sBitmap = strBitmap;       
        HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),    
            m_sBitmap, IMAGE_BITMAP, 0, 0,     
            LR_LOADFROMFILE | LR_CREATEDIBSECTION);       
        // Do we have a valid handle for the loaded image?    
        if (hBitmap)    
        {     
            // Delete the current bitmap     
            if (m_bmpBitmap.DeleteObject())        
                m_bmpBitmap.Detach(); 
            // If there was a bitmap, detach it     
            // Attach the currently loaded bitmap to the bitmap object     
            m_bmpBitmap.Attach(hBitmap);    
        }    m_bmpBitmap.GetBitmap(&bm);  
        //Get Bitmap Structure    
        Invalidate();
    }

    Viel Spass damit
    Wenn du fragen hast, dann frag einfach.

    Liebe grüße
    RuFF
     

Ähnliche Themen

  1. svg und bitmaps
    Von ernii im Forum Vektor-Programme
    Antworten: 1
    Letzter Beitrag: 05.08.07, 15:12
  2. Probleme mit SFT_Tree 4.0 beim einfügen eines Bitmaps
    Von mki_germo im Forum VisualStudio & MFC
    Antworten: 0
    Letzter Beitrag: 26.10.06, 13:05
  3. Bitmaps
    Von Heizemusik im Forum Photoshop
    Antworten: 11
    Letzter Beitrag: 24.10.05, 15:25
  4. c++6 und bitmaps
    Von JFKder1 im Forum Borland CBuilder und VCL
    Antworten: 0
    Letzter Beitrag: 03.11.04, 21:58
  5. Freehand MX - Konturen eines Bitmaps
    Von BenFish im Forum Vektor-Programme
    Antworten: 4
    Letzter Beitrag: 09.07.04, 02:34