根据已绘好图的DC,用GDI+导出JPG图片

这是刚进公司做的一个小小小project,记录下GDI+一个使用的小方法.

exportguiapi.h

#ifndef _EXPORT_GUI_API_H
#define _EXPORT_GUI_API_H

#define EXPORT_TYPE_JPG 1
#define EXPORT_TYPE_PNG 2
#define EXPORT_TYPE_BMP 3
class ImageSize
{
public:
	int  m_ExportType;
	int m_Quality;
	int m_SrcWidth;
        int m_SrcHeight;
	int m_DesWidth;
	int m_DesHeight;

};

 int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);//helper function

 bool ImageExportFun(CDC *pDC,CString pathname,ImageSize size);

#endif

exportguiapi.cpp

#include "stdafx.h"
#include <tchar.h>
#include "exportguiapi.h"
#include "GdiPlus.h"
using namespace Gdiplus; 


int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	UINT num= 0;
	UINT size= 0;

	ImageCodecInfo* pImageCodecInfo= NULL;

	GetImageEncodersSize(&num, &size);
	if(size== 0)
	{
		return -1;
	}
	pImageCodecInfo= (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo== NULL)
	{
		return -1;
	}

	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;
		}
	}

	free(pImageCodecInfo);
	return -1;
}


bool ImageExportFun(CDC *pDC,CString pathname,ImageSize size)
{
	if (pDC==NULL ||pathname.GetLength()<=0) //insure pDC and pathname is available
	{
		return false;
	}

	CBitmap screenbitmap; 
	screenbitmap.CreateCompatibleBitmap(pDC,size.m_DesWidth,size.m_DesHeight);  
	CDC memDC;  
	memDC.CreateCompatibleDC(pDC);  
	CBitmap *pOldBitmap = memDC.SelectObject(&screenbitmap);   
	memDC.SetStretchBltMode(HALFTONE);
	memDC.StretchBlt(0,0,size.m_DesWidth,size.m_DesHeight,pDC,0,0,size.m_SrcWidth,size.m_SrcHeight,SRCCOPY);
	memDC.SelectObject(pOldBitmap);  
	Bitmap bitmap((HBITMAP)screenbitmap.GetSafeHandle(),NULL);  

	CLSID picClsid;    
	
	CString str= pathname.Right(3);
	if (size.m_ExportType==1)//when we save a JPG image,we should setting the compression level
	{
		EncoderParameters   encoderParameters;  
		// Before we call Image::Save, we must initialize an
		// EncoderParameters object. The EncoderParameters object
		// has an array of EncoderParameter objects. In this
		// case, there is only one EncoderParameter object in the array.
		// The one EncoderParameter object has an array of values.
		// In this case, there is only one value (of type ULONG)
		// in the array. We will let this value vary from 0 to 100.
		encoderParameters.Count   =   1;  
		encoderParameters.Parameter[0].Guid   =   EncoderQuality;  
		encoderParameters.Parameter[0].Type   =   EncoderParameterValueTypeLong;  
		encoderParameters.Parameter[0].NumberOfValues   =   1;  
		encoderParameters.Parameter[0].Value   =  &size.m_Quality; 
		GetEncoderClsid(L"image/jpeg", &picClsid);  
		if (str.CompareNoCase(_T("jpg"))!=0) // if the pathname dosen't has extension filename, add a extension filename
		{
			pathname += _T(".jpg");
		}
		bitmap.Save(pathname, &picClsid,&encoderParameters); 
	}
	else if (size.m_ExportType==2)
	{
		GetEncoderClsid(L"image/png", &picClsid);   
		if (str.CompareNoCase(_T("png"))!=0) 
		{
			pathname += _T(".png");
		}
		bitmap.Save(pathname, &picClsid,NULL); 
	}
	else if (size.m_ExportType==3)
	{
		GetEncoderClsid(L"image/bmp", &picClsid);  
		if (str.CompareNoCase(_T("bmp"))!=0) 
		{
			pathname += _T(".bmp");
		}
		bitmap.Save(pathname, &picClsid,NULL); 
	}
	else
	{
		DeleteObject(screenbitmap);
		memDC.DeleteDC();
		return false;
	}
	DeleteObject(screenbitmap);
	memDC.DeleteDC();
	return true;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值