资源简介
VC GDI+ 窗口截图内存BMP转JPG,压缩、JPG再转IStream流,IStream流再转 BYTE
上体见 http://blog.csdn.net/q610098308/article/details/78181933
代码片段和文件信息
/**
@file mem_image.cpp
@author Kevin Lynx
@brief to load/save images from/to memory based on GDI+
*/
#include “mem_image.h“
#include
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 < num; ++ i )
{
if( wcscmp( pImageCodecInfo[i].MimeType format ) == 0 )
{
*pClsid = pImageCodecInfo[i].Clsid;
free( pImageCodecInfo );
return i;
}
}
free( pImageCodecInfo );
return -1;
}
static bool mem_to_global( const void *buf size_t size HGLOBAL global )
{
void *dest = ::GlobalLock( global );
if( dest == NULL )
{
return false;
}
memcpy( dest buf size );
::GlobalUnlock( global );
return true;
}
static bool stream_to_mem( IStream *stream void **outbuf size_t *size )
{
ULARGE_INTEGER ulnSize;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
/* get the stream size */
if( stream->Seek( lnOffset STREAM_SEEK_END &ulnSize ) != S_OK )
{
return false;
}
if( stream->Seek( lnOffset STREAM_SEEK_SET NULL ) != S_OK )
{
return false;
}
/* 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 false;
}
return true;
}
Gdiplus::Image *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::Image *image = Gdiplus::Image::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;
}
void *mi_to_memory( Gdiplus::Image *image void **outbuf size_t *size )
{
IStream *stream = NULL;
if( ::CreateStreamOnHGlobal( NULL TRUE &stream ) != S_OK )
{
return NULL;
}
/* get the jpg encoder */
::CLSID jpgClsid;
GetEncoderClsid( L“image/jpeg“ &jpgClsid );
/* save the image to stream */
Gdiplus::Status save_s = image->Save( stream &jpgClsid );
if( save_s != Gdiplus::Ok )
{
stream->Release();
return NULL;
}
/* read the stream to buffer */
if( !stream_to_mem( stream outbuf size ) )
{
stream->Release();
return NULL;
}
return
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-10-09 14:12 Screen_Shot\
目录 0 2017-10-09 14:12 Screen_Shot\Debug\
目录 0 2017-10-09 14:12 Screen_Shot\ScreenShot\
文件 897 2017-09-26 22:08 Screen_Shot\ScreenShot.sln
文件 18432 2017-10-09 14:12 Screen_Shot\ScreenShot.suo
文件 3014 2009-05-28 19:47 Screen_Shot\ScreenShot\mem_image.cpp
文件 645 2009-05-28 19:47 Screen_Shot\ScreenShot\mem_image.h
文件 2366 2017-09-26 22:08 Screen_Shot\ScreenShot\ReadMe.txt
文件 1980 2017-09-26 22:13 Screen_Shot\ScreenShot\resource.h
文件 52660 2017-10-09 13:52 Screen_Shot\ScreenShot\ScreenShot.aps
文件 7186 2017-10-09 14:12 Screen_Shot\ScreenShot\ScreenShot.cpp
文件 39 2017-09-26 22:08 Screen_Shot\ScreenShot\ScreenShot.h
文件 23558 2009-08-31 02:31 Screen_Shot\ScreenShot\ScreenShot.ico
文件 7496 2017-10-09 13:52 Screen_Shot\ScreenShot\ScreenShot.rc
文件 4607 2017-10-09 11:56 Screen_Shot\ScreenShot\ScreenShot.vcxproj
文件 1804 2017-10-09 11:56 Screen_Shot\ScreenShot\ScreenShot.vcxproj.filters
文件 143 2017-09-26 22:08 Screen_Shot\ScreenShot\ScreenShot.vcxproj.user
文件 23558 2009-08-31 02:31 Screen_Shot\ScreenShot\small.ico
文件 215 2017-09-26 22:08 Screen_Shot\ScreenShot\stdafx.cpp
文件 420 2017-09-26 22:08 Screen_Shot\ScreenShot\stdafx.h
文件 236 2017-09-26 22:08 Screen_Shot\ScreenShot\targetver.h
- 上一篇:基于RS422总线的单片机多机通讯接口的设计与实现
- 下一篇:logo识别项目
评论
共有 条评论