购买与咨询 NX 二次开发视频教程,请联系微信号:13890821008 QQ号:85585969
1:
自定义一个头文件取名为:get_prt_image.h
然后复制粘贴以下代码到get_prt_image.h头文件中并保存 #pragma once
#include <afxwin.h>
#include <string>
#include <Gdiplus.h>
#pragma comment(lib,"gdiplus.lib")
using namespace Gdiplus;
using namespace std;
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
ULONG_PTR m_gdiplusToken;
Gdiplus::GdiplusStartupInput StartupInput;
GdiplusStartup(&m_gdiplusToken,&StartupInput,NULL);
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);
GdiplusShutdown(m_gdiplusToken);
return -1;
}
void get_thumb(string file_name,string bmppath)
{
CLSID bmpClsid;
GetEncoderClsid(L"image/bmp", &bmpClsid);

●●●请先
登陆 或
注册 后查看●●●
Gdiplus::Image * pImage = Gdiplus::Image::FromFile(WStr);
Gdiplus::Image * smage=pImage->GetThumbnailImage(400,400);
string path2=bmppath;
size_t len2 =path2.size()+1;
size_t converted2 = 0;
wchar_t *WStr2;
WStr2=(wchar_t*)malloc(len2*sizeof(wchar_t));
mbstowcs_s(&converted, WStr2, len2, path2.c_str(), _TRUNCATE);
pImage->~Image();
Status rr= smage->Save(WStr2,&bmpClsid);
}
int get_prt_image(string file_name,string bmppath)
{
IStorage* pStorage = NULL;
HRESULT hResult;
wchar_t path_file[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, file_name.c_str(), -1, path_file, MAX_PATH);
hResult = StgOpenStorage(path_file, NULL, STGM_READ | STGM_SHARE_DENY_WRITE, NULL, 0, &pStorage);
if(hResult != S_OK)return-1;
IStorage* pSubStorage = NULL;
HRESULT hr = pStorage->OpenStorage(L"images", NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &pSubStorage);
IStream *pStream; pStream=NULL;
DWORD dwMode=STGM_READ|STGM_SHARE_EXCLUSIVE;
if(pSubStorage!=NULL) pSubStorage->OpenStream( L"preview", NULL, dwMode, 0, &pStream );
if(pStream!=NULL)
{
CLSID bmpClsid;
GetEncoderClsid(L"image/bmp", &bmpClsid);
Image *image1=Image::FromStream(pStream);
//image1=image1->GetThumbnailImage(300,300);
if (image1!=NULL)
{
wchar_t path_file[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, bmppath.c_str(), -1, path_file, MAX_PATH);
image1->Save(path_file,&bmpClsid,NULL);
}
pStream->Release();
}
pStorage->Release();
pStorage = NULL;
//get_thumb(bmppath,bmppath);
return 0;
}
2:
再新建一个text.cpp文件,并引用加载上面get_prt_image.h的头文件,具体代码如下:
#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
# include <strstream>
# include <iostream>
using std::ostrstream;
using std::endl;
using std::ends;
using std::cerr;
#else
# include <strstream.h>
# include <iostream.h>
#endif
#include <uf.h>
#include <uf_ui.h>
#include <uf_exit.h>
#include"get_prt_image.h" //添加上面保存的头文件
#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
static int report_error( char *file, int line, char *call, int irc)
{
if (irc)
{
char err[133],
msg[133];
sprintf(msg, "*** ERROR code %d at line %d in %s:\n+++ ",
irc, line, file);
UF_get_fail_message(irc, err);
UF_print_syslog(msg, FALSE);
UF_print_syslog(err, FALSE);
UF_print_syslog("\n", FALSE);
UF_print_syslog(call, FALSE);
UF_print_syslog(";\n", FALSE);
if (!UF_UI_open_listing_window())
{
UF_UI_write_listing_window(msg);
UF_UI_write_listing_window(err);
UF_UI_write_listing_window("\n");
UF_UI_write_listing_window(call);
UF_UI_write_listing_window(";\n");
}
}
return(irc);
}
extern DllExport void ufusr( char *parm, int *returnCode, int rlen )
{
if( UF_CALL(UF_initialize()) )
{
return;
}
string prtpath="C:\\112233.prt";
string bmppath="C:\\a.bmp";
string bmppath2="C:\\b.bmp";
get_prt_image(prtpath,bmppath); //调用函数生成小缩略图
get_thumb(bmppath,bmppath2); //调用函数生成大缩略图
UF_CALL(UF_terminate());
}
extern int ufusr_ask_unload( void )
{
return( UF_UNLOAD_IMMEDIATELY );
}