无废话,直接上代码
show me the code 头文件#include
#include
#include
#include
using namespace std;
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
class DGIWin32Plus
{
Bitmap * _bitmap = NULL;
//IStream *_stream = NULL;
::CLSID _jpgClsid;
int _oldw = 320;
int _oldh = 240;
protected:
int mi_to_memory(Gdiplus::Bitmap *image, void *outbuf, int outbufsize);
public:
int EncodeRGB2Jpeg(char *RGB,int width,int height ,int LineWidth, char *Jpeg, int JpegLen);
DGIWin32Plus(void);
~DGIWin32Plus(void);
};
show me the code cpp文件
#include "DGIWPlus.h"
#include
#include
#define DGIDelete(x) do{if(x!=NULL){delete x;x=NULL;}}while(0)
#define DGIDelete_Array(x) do{if(x!=NULL){ delete[]x;x=NULL;}}while(0)
#define DEFINE_GOBJ Graphics gObj(_Mem)
//#define DEFINE_GOBJHDC Graphics gObjhdc(_Mem)
#define DEFINE_IMG(x) Gdiplus::Bitmap *x;x=NULL
#define GET_WIDTH(x) x->GetWidth()
#define GET_HEIGHT(x) x->GetHeight()
#define UNDEFINE_IMG(x) DGIDelete(x)
static int GetEncoderClsid( const WCHAR *format, CLSID *pClsid )
{
UINT num = 0, size = 0;
Gdiplus::ImageCodecInfo *pImageCodecInfo = NULL;
Gdiplus::GetImageEncodersSize( &num, &size );
if( size == 0 )
{
return -1;
}
pImageCodecInfo = (Gdiplus::ImageCodecInfo*) malloc( size );
Gdiplus::GetImageEncoders( num, size, pImageCodecInfo );
for( UINT i = 0; i Seek( lnOffset, STREAM_SEEK_END, &ulnSize ) != S_OK )
{
return -1;
}
if( stream->Seek( lnOffset, STREAM_SEEK_SET, NULL ) != S_OK )
{
return -1;
}
if (ulnSize.QuadPart > outbufsize)
ulnSize.QuadPart = outbufsize;
/* read it */
//*outbuf = malloc( (size_t)ulnSize.QuadPart );
//*size = (size_t) ulnSize.QuadPart;
ULONG bytesRead;
if( stream->Read( outbuf, (ULONG)ulnSize.QuadPart, &bytesRead ) != S_OK )
{
//free( *outbuf );
return -1;
}
return bytesRead;
}
static Gdiplus::Bitmap *mi_from_memory( const void *buf, size_t size )
{
IStream *stream = NULL;
HGLOBAL global = ::GlobalAlloc( GMEM_MOVEABLE, size );
if( global == NULL )
{
return NULL;
}
/* copy the buf content to the HGLOBAL */
if( !mem_to_global( buf, size, global ) )
{
::GlobalFree( global );
return NULL;
}
/* get the IStream from the global object */
if( ::CreateStreamOnHGlobal( global, TRUE, &stream ) != S_OK )
{
::GlobalFree( global );
return NULL;
}
/* create the image from the stream */
Gdiplus::Bitmap *image = Gdiplus::Bitmap::FromStream( stream );
stream->Release();
/* i suppose when the reference count for stream is 0, it will
GlobalFree automatically. The Image maintain the object also.*/
return image;
}
int DGIWin32Plus::mi_to_memory( Gdiplus::Bitmap *image, void *outbuf, int outbufsize )
{
IStream *stream = NULL;
if (::CreateStreamOnHGlobal(NULL, TRUE, &stream) != S_OK)
{
return -1;
}
/* save the image to stream */
Gdiplus::Status save_s = image->Save( stream, &_jpgClsid );
if( save_s != Gdiplus::Ok )
{
stream->Release();
return -1;
}
/* read the stream to buffer */
int ret = stream_to_mem(stream, outbuf, outbufsize);
stream->Release();
return ret;
}
static GdiplusStartupInput m_gdiplusStartupInput ;
static ULONG_PTR m_gdiplusToken;
void StartDGIWPlus()
{
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
}
void EndDGIPlus()
{
GdiplusShutdown(m_gdiplusToken);
}
DGIWin32Plus::DGIWin32Plus(void)
{
StartDGIWPlus();
//::CLSID jpgClsid;
GetEncoderClsid(L"image/jpeg", &_jpgClsid);
}
DGIWin32Plus::~DGIWin32Plus(void)
{
if (_bitmap != NULL)
delete _bitmap;
/*if(_stream !=NULL)
_stream->Release();*/
EndDGIPlus();
}
int DGIWin32Plus::EncodeRGB2Jpeg(char *RGB,int width,int height ,int lineWidth, char *Jpeg, int JpegLen)
{
if (width != _oldw || height != _oldh)
{
_oldw = width;
_oldh = height;
}
if (_bitmap != NULL)
{
delete _bitmap;
_bitmap = NULL;
}
_bitmap = new Bitmap(width, height, lineWidth, PixelFormat24bppRGB, (BYTE*)RGB);
if (_bitmap == NULL)
return -1;
//size_t size;
int ret = mi_to_memory(_bitmap, (void*)Jpeg, JpegLen);
//delete bitmap;
return ret;
}
各位读者直接调用类就行,数据存磁盘就再写个存文件就好