⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpgvsbmp.cpp

📁 用于网络传输时的Jpg和Bmp格式相互转换的API封装
💻 CPP
字号:
// JpgVSbmp.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "JpgVSbmp.h"
#include "Jpeg.h"
#include "Dib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

/////////////////////////////////////////////////////////////////////////////
// CJpgVSbmpApp

BEGIN_MESSAGE_MAP(CJpgVSbmpApp, CWinApp)
	//{{AFX_MSG_MAP(CJpgVSbmpApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CJpgVSbmpApp construction

CJpgVSbmpApp::CJpgVSbmpApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CJpgVSbmpApp object

CJpgVSbmpApp theApp;

// 将指定的Bmp文件内容转换为JPG文件格式
// lpBmpFileNameForIn——Bmp文件完整路径
// lpJpgFileNameForOut--输出的JPG文件完整路径
// bColor——是否保留彩色(True——保留,False——转为2色图)
// nQuality——图像质量(1~100,默认75,100为质量最高)
BOOL _stdcall BmpToJpg(LPCSTR lpBmpFileNameForIn, LPCSTR lpJpgFileNameForOut,BOOL bColor=TRUE, int nQuality=75)
{
	BOOL bt;
	CJpeg jj;
	CDib * m_pDib;
	m_pDib=new CDib;
	
	if(nQuality<1 ||nQuality>100)
		nQuality=75;

	// 读取Bmp信息
	CFile file(lpBmpFileNameForIn,CFile::modeRead);
	m_pDib->Read(&file);

	// 保存为Jpg格式
	bt=jj.Save(lpJpgFileNameForOut, m_pDib, bColor, nQuality);
	delete m_pDib;
	
	return bt;
}

// 将指定的Bmp文件内容转换为JPG文件格式
// lpJpgFileNameForIn——Bmp文件完整路径
// lpBmpFileNameForOut——输出的Bmp文件完整路径
// iColorBit——转换后Bmp图的颜色位数(1——2色,4——16色,8——256色,others——24位)
BOOL _stdcall JpgToBmp(LPCSTR lpJpgFileNameForIn, LPCSTR lpBmpFileNameForOut,int iColorBit=24)
{
	BOOL bt;
	CJpeg jj;
	CDib * m_pDib;
	m_pDib=new CDib;

	// 创建CFile对象
	CFile file(lpBmpFileNameForOut,CFile::modeCreate | CFile::modeWrite);
	
	// 加载JPG文件内容
	jj.Load(lpJpgFileNameForIn);
	// 获得JPG文件句柄
	HDIB hDIB = CopyHandle(jj.GetDib()->GetHandle());
	if (hDIB == NULL)
		return FALSE;
	// 将DIB对象与JPG文件句柄关联起来
	m_pDib->Attach(hDIB);
	
	// 判断BMP颜色位数
	if(iColorBit==1 || iColorBit==4 || iColorBit==8 )
		m_pDib->ConvertFormat(iColorBit);	// 2、16、256色Bmp
	else
		m_pDib->ConvertFormat(24);			// 24位Bmp


	// 写BMP文件数据
	bt=m_pDib->Write(&file);

	delete m_pDib;

	return bt;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -