bmp größe in pixel



  • hallo,

    kennt jmd eine einfache möglichkeit die größe einer bitmap, welche als *.bmp file vorliegt herauszubekommen.

    am besten wäre eine funktion wie zb:
    GetBitmapDimension(CString Filename,int *pWidth, int *pHeight);

    ich möchte aber auf keinen fall die ganze bitmap reinladen müssen, bloß um die größe feststellen zu können

    danke schonmal

    mfg, TFTS

    PS: das ganze soll in Visual C++ 6.0 geschrieben werden, es ist mir also alles recht c,c++,winapi,mfc ... was das problem lösen könnte



  • Idee: Die Daten aus der Datei auslesen. 😎

    Bye, TGGC (Demo or Die)



  • Du brauchst nicht die gesamte Datei einzulesen, sondern nur den Header, siehe z.B. hier - die Bildgröße steht in den Bytes 18..21 (Breite) und 22..25 (Höhe).





  • BitmapDIBUncompressed32Bit.h:

    //------------------------------------------------------------------------------
    // Last Change: 8-10-2005
    // Author: Julian Amann (vertexwahn@gmx.de)
    //
    // Brief description: simple class to handle 32 bit uncompressed bitmaps for 
    // windows
    //------------------------------------------------------------------------------
    
    #ifndef BitmapDIBUncompressed32Bit_h
    #define BitmapDIBUncompressed32Bit_h
    
    #include <windows.h>
    #include <stdio.h>
    
    #ifdef _MSC_VER
    	#pragma pack(push,packing)
    	#pragma pack(1)
    	#define PACK_STRUCT
    #else
    	#error you must byte-align these structures with appropriate compiler directives
    #endif
    
    //------------------------------------------------------------------------------------
    // a class for an uncompressed bitmap
    class BitmapDIBUncompressed32Bit
    {
    public:
    
    	BitmapDIBUncompressed32Bit(char * filename);
    	BitmapDIBUncompressed32Bit(HDC hdc, int width, int height);
    	int getWidth() const;
    	int getHeight() const;
    
    	void Save(char * filename);
    
    	virtual ~BitmapDIBUncompressed32Bit();
    
    	void Release();
    
    	HDC getHDC();
    
    	UINT * getBytes() const;
    	void setBytes(UINT * data)
    	{
    		BitmapBytes = data;
    	}
    
    private:
    	BitmapDIBUncompressed32Bit(const BitmapDIBUncompressed32Bit &src) {};
    
    	UINT					*BitmapBytes;
    	BITMAPINFO				BitmapInfo;
    	BITMAPINFOHEADER		BitmapInfoHeader;
    	HDC						BitmapHDC;
    	HBITMAP					BitmapHandle;
    };
    
    void CaptureScreen(BitmapDIBUncompressed32Bit &bitmap);
    
    // force default byte alignment
    #ifdef _MSC_VER
    	#pragma pack(pop, packing)
    #endif
    
    #undef PACK_STRUCT
    
    #endif
    

    BitmapDIBUncompressed32Bit.cpp:

    //------------------------------------------------------------------------------
    // Last Change: 8-10-2005
    // Author: Julian Amann (vertexwahn@gmx.de)
    //
    // Brief description: simple class to handle 32 bit uncompressed bitmaps for 
    // windows
    //------------------------------------------------------------------------------
    
    #include <windows.h>
    #include <stdio.h>
    #include "BitmapDIBUncompressed32Bit.h"
    
    #ifdef _MSC_VER
    	#pragma pack(push,packing)
    	#pragma pack(1)
    	#define PACK_STRUCT
    #else
    	#error you must byte-align these structures with appropriate compiler directives
    #endif
    
    BitmapDIBUncompressed32Bit::BitmapDIBUncompressed32Bit(char * filename)
    {
    	HBITMAP hBitmap =
    	(HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0,
        LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    
    	if(hBitmap==0)
    		MessageBox(NULL,"","",MB_OK);
    
    	BITMAP Bitmap;
    	GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
    	HDC tmpBitmapHDC = CreateCompatibleDC(0);
    	SelectObject(tmpBitmapHDC, hBitmap);
    
    	BitmapInfoHeader.biSize = sizeof(BitmapInfo);
    	BitmapInfoHeader.biWidth = Bitmap.bmWidth;
    	BitmapInfoHeader.biHeight = Bitmap.bmHeight;
    	BitmapInfoHeader.biCompression = BI_RGB;
    	BitmapInfoHeader.biBitCount = 32;
    	BitmapInfoHeader.biPlanes = 1;
    	BitmapInfoHeader.biSizeImage = 0;
    	BitmapInfoHeader.biClrImportant = 0;
    	BitmapInfoHeader.biClrUsed = 0;
    	BitmapInfoHeader.biSizeImage = 0;
    	BitmapInfoHeader.biXPelsPerMeter = 0;
    	BitmapInfoHeader.biYPelsPerMeter = 0;
    
    	BitmapInfo.bmiHeader = BitmapInfoHeader;
    
    	BitmapHDC	 = CreateCompatibleDC(0); 
    
    	BitmapHandle = CreateDIBSection(0, &BitmapInfo, 
    									0, (void**)&BitmapBytes, NULL, 0);
    
    	SelectObject(BitmapHDC, BitmapHandle);
    
    	BitBlt(BitmapHDC, 0, 0, Bitmap.bmWidth, Bitmap.bmHeight, tmpBitmapHDC, 0, 0, SRCCOPY);
    
    	DeleteObject(hBitmap);
    }
    
    BitmapDIBUncompressed32Bit::BitmapDIBUncompressed32Bit(HDC hdc, int width, int height)
    {
    	BitmapInfoHeader.biSize = sizeof(BitmapInfo);
    	BitmapInfoHeader.biWidth = width;
    	BitmapInfoHeader.biHeight = height;
    	BitmapInfoHeader.biCompression = BI_RGB;
    	BitmapInfoHeader.biBitCount = 32;
    	BitmapInfoHeader.biPlanes = 1;
    	BitmapInfoHeader.biSizeImage = 0;
    	BitmapInfoHeader.biClrImportant = 0;
    	BitmapInfoHeader.biClrUsed = 0;
    	BitmapInfoHeader.biSizeImage = 0;
    	BitmapInfoHeader.biXPelsPerMeter = 0;
    	BitmapInfoHeader.biYPelsPerMeter = 0;
    
    	BitmapInfo.bmiHeader = BitmapInfoHeader;
    
    	BitmapHDC	 = CreateCompatibleDC(hdc); 
    
    	BitmapHandle = CreateDIBSection(hdc, &BitmapInfo, 
    									0, (void**)&BitmapBytes, NULL, 0);
    
    	SelectObject(BitmapHDC, BitmapHandle);
    }
    
    int BitmapDIBUncompressed32Bit::getWidth() const
    {
    	return BitmapInfoHeader.biWidth;
    }
    
    int BitmapDIBUncompressed32Bit::getHeight() const
    {
    	return BitmapInfoHeader.biHeight;
    }
    
    void BitmapDIBUncompressed32Bit::Save(char * filename)
    {
    	int imagesize = getWidth() * getHeight() * 4;	
    
    	BITMAPFILEHEADER BitmapFileHeader;						    // File Header
    	BitmapFileHeader.bfType		 = 0x4D42;						// Signatur 0x4D42 (="BM")
    	BitmapFileHeader.bfSize		 = sizeof(BITMAPFILEHEADER) +   // Gesamtgröße der Datei in Bytes
    		                           sizeof(BITMAPINFOHEADER) + 
    							       imagesize; 
    	BitmapFileHeader.bfReserved1 = 0;							// immer 0
    	BitmapFileHeader.bfReserved2 = 0;							// immer 0
    	BitmapFileHeader.bfOffBits   = 54;							// Offset der Pixeldaten innerhalb der Datei (bei uncompressed 32 bit immer 54 Bytes)
    
    	BITMAPINFOHEADER BitmapInfoHeader;							// Bitmap Info Header
    	BitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);			// Größe der Struktur in Bytes (40)
    	BitmapInfoHeader.biWidth = getWidth();						// Breite des Bildes in Pixel
    	BitmapInfoHeader.biHeight = getHeight();					// Höhe des Bildes in Pixel
    	BitmapInfoHeader.biPlanes = 1;								// = 1
    	BitmapInfoHeader.biBitCount = 32;							// 32 Bits Pro Pixel
    	BitmapInfoHeader.biCompression = BI_RGB;					// Komprimierung (0 = keine)
    	BitmapInfoHeader.biSizeImage = imagesize;					// Umfang der Pixeldaten in Bytes
    	BitmapInfoHeader.biXPelsPerMeter = 0;						// horizontale Auflösung (Pixel pro Meter)
    	BitmapInfoHeader.biYPelsPerMeter = 0;						// vertikale Aufläsung (Pixel pro Meter)
    	BitmapInfoHeader.biClrUsed       = 0;						// Zahl der verwendeten Farben
    	BitmapInfoHeader.biClrImportant  = 0;						// Zahl der wichtigen Farben
    
    	FILE *ptrFile = NULL;	
    	ptrFile = fopen(filename, "wb");
    	fwrite(&BitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, ptrFile);		
    	fwrite(&BitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, ptrFile);
    
    	fwrite(BitmapBytes, getWidth() * getHeight() * 4, 1, ptrFile);
    
    	fclose(ptrFile);
    }
    
    BitmapDIBUncompressed32Bit::~BitmapDIBUncompressed32Bit()
    {
    	Release();
    }
    
    void BitmapDIBUncompressed32Bit::Release()
    {
    	DeleteObject(BitmapHandle);
    }
    
    HDC BitmapDIBUncompressed32Bit::getHDC()
    {
    	return BitmapHDC;
    }
    
    UINT * BitmapDIBUncompressed32Bit::getBytes() const
    {
    	return BitmapBytes;
    }
    
    void CaptureScreen(BitmapDIBUncompressed32Bit &bitmap)
    {
    	HDC hdcDesktop = GetWindowDC(GetDesktopWindow());
    	BitBlt(bitmap.getHDC(), 0, 0, bitmap.getWidth(), bitmap.getHeight(),
    		   hdcDesktop, 0, 0, SRCCOPY);
    }
    
    // force default byte alignment
    #ifdef _MSC_VER
    	#pragma pack(pop, packing)
    #endif
    
    #undef PACK_STRUCT
    

Anmelden zum Antworten