自己用GDI+封装的BMP转PNG格式图片的dll(二)

本文介绍了一位作者使用GDI+库封装的BMP转换为PNG格式的DLL实现,提供了源代码及转换方法。只需简单修改,也能实现BMP到GIF的转换。对于想要自定义图片转换DLL的读者,这是一个实用的例子。

最近看了点GDI+,对里边提供转图片格式的方法大感惊讶,太简洁了!尽管很方便,自己还是将它做成了一个dll,因为自己其实一直希望有这么个dll。

以下便是该dll的源代码,在VC6.0环境下编译生成dll。

改成GIF也很方便,只要将所有的 png替换成gif即可,^O^

当然,核心的改动一是文件名改成gif

outFileName=fileTitle+string(".gif");
二是 编码码改成gif

GetEncoderClsid(L"image/gif", &encoderClsid);

改成JPG,需要增加一个参数,可以设置压缩系数,这里不再赘述,感兴趣可以参考MSDN。

头文件:BMP2PNG.h

#ifndef BMP2PNG_H
#define BMP2PNG_H

extern "C" int __declspec(dllexport)BMP2PNG(char *file);
extern "C" unsigned long __declspec(dllexport)GetInfoLog();

#endif

头文件:StdAfx.h 这是系统默认生成的

#if !defined(AFX_STDAFX_H__8042CF65_A139_4F35_A943_43735F5587B9__INCLUDED_)
#define AFX_STDAFX_H__8042CF65_A139_4F35_A943_43735F5587B9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


// Insert your headers here
#define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers

#include <windows.h>


// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__8042CF65_A139_4F35_A943_43735F5587B9__INCLUDED_)
实现文件:BMP2PNG.cpp
// BMP2PNG.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "BMP2PNG.h"

#include<wtypes.h>
#include<objidl.h>

#define ULONG_PTR ULONG
#include <gdiplus.h>
#include <string>

using namespace std;
using namespace Gdiplus;

#pragma comment( lib, "gdiplus.lib" ) 
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);  // helper function

unsigned long infoLog;

unsigned long GetInfoLog()
{	
	return infoLog;	
}

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}


int BMP2PNG(char *file)
{  
	string inFilePath,inFileName,outFileName,fileExtName,fileTitle; 
	string filePathName;

	inFilePath=string(file);
	size_t lastPoint=inFilePath.rfind('.');
	size_t lastDivd=inFilePath.rfind('\\');
	
	fileExtName=inFilePath.substr(lastPoint+1,3);
	fileTitle=inFilePath.substr(lastDivd+1,lastPoint-lastDivd-1);
	filePathName=inFilePath.substr(0,lastDivd+1);
	
	if(fileExtName.compare("bmp")&&fileExtName.compare("BMP"))
	{
		infoLog=ERROR_INVALID_PARAMETER;
		return 1;
	}
	
	if(!filePathName.empty() && !SetCurrentDirectory(filePathName.c_str()))
	{
		infoLog=GetLastError(); // 设置当前目录失败
		return	1;
	}

	inFileName=fileTitle+string(".bmp");
	outFileName=fileTitle+string(".png");

	// Convert to WCHAR
	// inFileName
	size_t bufSize;

	bufSize=MultiByteToWideChar(CP_ACP,NULL,inFileName.c_str(),inFileName.length(),NULL,0); // 获得转换缓冲区大小
		
	if(!bufSize) 
	{
		infoLog=GetLastError();
		return 1;
	}

	wchar_t * pwInFileName = new wchar_t[bufSize+1];
	
	if(!MultiByteToWideChar(CP_ACP, NULL, inFileName.c_str(),inFileName.length(), pwInFileName, bufSize))
	{
		infoLog=GetLastError();
		return 1;
	}
	pwInFileName[bufSize]=L'\0';

	// outFileName

	bufSize=MultiByteToWideChar(CP_ACP,NULL,outFileName.c_str(),outFileName.length(),NULL,0); // 获得转换缓冲区大小
		
	if(!bufSize) 
	{
		infoLog=GetLastError();
		return 1;
	}

	wchar_t * pwOutFileName = new wchar_t[bufSize+1];
	
	if(!MultiByteToWideChar(CP_ACP, NULL, outFileName.c_str(),outFileName.length(), pwOutFileName, bufSize))
	{
		infoLog=GetLastError();
		return 1;
	}
	pwOutFileName[bufSize]=L'\0';
		

	// Initialize GDI+.
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	Status  stat;

	stat=GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	if(stat!=Ok)
	{
		infoLog=stat;
		return 2;
	}

	CLSID   encoderClsid;

	Image*   image = new Image(pwInFileName);//

	// Get the CLSID of the PNG encoder.
	GetEncoderClsid(L"image/png", &encoderClsid);

	stat = image->Save(pwOutFileName, &encoderClsid, NULL);

	delete image;	
	
	if(pwInFileName)
		delete [] pwInFileName;
	if(pwOutFileName)
		delete [] pwOutFileName;

	GdiplusShutdown(gdiplusToken);
	
	if(stat == Ok)
	{
		infoLog=1;
		return 0;
	}
	else
	{
		infoLog=stat;
		return 2;
	}

}

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;

	GetImageEncodersSize(&num, &size);
	if(size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL)
		return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);

	for(UINT j = 0; j < num; ++j)
	{
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}    
	}

	free(pImageCodecInfo);
	return -1;  // Failure
}

生成的dll可由下面链接获得:

http://download.csdn.net/source/3486711

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值