明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5066|回复: 2

使用梦想绘图控件把DWG文件转成JPG 讲解

[复制链接]
发表于 2009-9-23 08:36 | 显示全部楼层 |阅读模式


1.1
说明    控件提供的接口,可以把dwg文件转换成Bmp,Jpg文件,也可以把DWG文件中某个区域的图形绘制到CDC上或保存为Bmp文件。这些接口即能在VC中使用,也能在VBC#Delphi,网页中调用。
1.2
DwgToJpg    不需要使用控件打开dwg文件,直接把dwg文件转成jpg文件。

VC接口:
BOOL MxDraw::DwgToJpg(IN LPCTSTR pszDwgFilePath,

OUT
LPCTSTR pszJpgFilePath,
int iWidth = -1,
int iHeight = -1);
COM接口:

bool
     DwgToJpg(
string
     pszDwgFileName,
string
     pszJpgFileName,
int
     lWidth,
int
     lHeight)

MxDrawXLib.IMxDrawApplication
的成员
参数:
pszDwgFilePath
转入的DWG文件路径
pszJpgFilePath
另存为的Jpg文件路径
iWidth
保存后的Jpg文件的像素宽度
iHeight
保存后的Jpg文件的像素高度

VC调用参考例程:

void CTestDlg::OnBnClickedDwgtojpgButton()
{

// TODO:



CPreviewFileDialog openDlg(TRUE,_T("dwg"),NULL,


OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,


//_T("dwg(*.dwg) | *.dwg||"),


_T("DWG files (*.dwg)|*.dwg|DXF files (*.dxf)|*.dxf|Jpg files (*.jpg)|*.jpg|BMP files (*.bmp)|*.bmp||"),


this);



CString sDwgFileName;


if(openDlg.DoModal() == IDOK)


{


sDwgFileName = openDlg.GetPathName();


}


else


{


return;


}



//


CString sJpgFilePath;


CFileDialog openJpgDlg(FALSE,_T("jpg"),NULL,


OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,


_T("jpg(*.jpg) | *.jpg||"),


this);


if(openJpgDlg.DoModal() == IDOK)


{


sJpgFilePath = openJpgDlg.GetPathName();


}


else


{


return;


}



// 1000
1000是转成jpg的像素宽度和高度。

if(MxDraw:wgToJpg(sDwgFileName,sJpgFilePath,1000,1000) )


{


AfxMessageBox(_T("
转换成功"));

}


else


{


AfxMessageBox(_T("
转换失败"));

}


}


C# 调用参考例程:
private void DwgToJpg_Click(object sender, EventArgs e)

{

// 创建一个应用对象


MxDrawApplication app = new MxDrawApplication();



OpenFileDialog ofd = new OpenFileDialog();


ofd.Filter = "Dwg
文件(*.Dwg)|*.Dwg|Dxf files (*.Dxf)|*.dxf";


if (ofd.ShowDialog() != DialogResult.OK)


{


return;


}



SaveFileDialog sfd = new SaveFileDialog();


sfd.Filter = "jpg
文件(*.jpg)|*.jpg";


if (sfd.ShowDialog() != DialogResult.OK)


{


return;


}


// 10001000是转成jpg的像素宽度和高度。

if(app.DwgToJpg(ofd.FileName, sfd.FileName, 1000, 1000) )


{



MessageBox.Show("
转换成功");

}


else


{


MessageBox.Show("
转换失败");

}



}


}



1.3
WriteJpg    使用控件打开dwg文件或经过了编辑后,调用该函数把它另存为Jpg文件。

VC接口:
BOOL
MxDraw::WriteJpg(MXDRAWOCXHANDLE hOcx,LPCTSTR pszJpgFilePath,
           int iWidth = -1,int iHeight = -1);
COM 接口:

public virtual bool
     SaveJpgFile(
string
     pszJpgFilePath,
int
     lWidth,
int
     lHeight)



AxMxDrawXLib.AxMxDrawX 的成员
参数:
hOcx
控件的标识句柄
pszJpgFilePath
保存的Jpg文件路径
lWidth
保存后的Jpg文件像素宽度,取默认值-1,程序就自动取4000
lHeight
保存后的Jpg文件像素高度,取默认值-1,程序就自动取4000

1.4
WriteBmp   使用控件打开dwg文件或经过了编辑后,调用该函数把它另存为Bmp文件。

VC接口:
BOOL
MxDraw::WriteBmp(MXDRAWOCXHANDLE hOcx,LPCTSTR pszBmpFilePath,
          int iWidth = -1,int iHeight = -1);

COM 接口:

public virtual bool
     SaveBmpFile(
string
     pszBmpFilePath,
int
     lWidth,
int
     lHeight)


AxMxDrawXLib.AxMxDrawX
的成员

参数:
hOcx
控件的标识句柄
pszJpgFilePath
保存的Jpg文件路径
lWidth
保存后的Jpg文件像素宽度,取默认值-1,程序就自动取4000
lHeight
保存后的Jpg文件像素高度,取默认值-1,程序就自动取4000

VC调用参考例程:
void CTestDlg::OnBnClickedSavebmpButton()
{

// TODO:
在此添加控件通知处理程序代码


CFileDialog openDlg(FALSE,_T("bmp"),NULL,


OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,


_T("bmp(*.bmp) | *.bmp||"),this);



CString sFileName;


if(openDlg.DoModal() == IDOK)


{


sFileName = openDlg.GetPathName();


}


else


{


return;


}



if(!MxDraw::WriteBmp(m_hDrawOcx,sFileName) )


{


CString sError;


if(MxDraw::GetLastError().IsEmpty() )


sError = _T("
保存位图文件失败!");


else


sError.Format(_T("
保存位图文件失败!原因为:%s"),MxDraw::GetLastError());


AfxMessageBox(sError);


}


else


{


AfxMessageBox(_T("
保存成功"));


}

}

1.5
DrawToDc   绘制控件当前图形中指定区域到CDC对象中。
VC接口:
BOOL MxDraw::DrawToDc(MXDRAWOCXHANDLE hOcx,
           CDC* pDC,
int iDCx,int iDCy,int iDCWidth,int iDCHeight,
           double dLbx,double dLby,double dRtx,double dRty

);

参数:
hOcx



标识控件的句柄
pDC

把指定区域的图形中的内容绘制到pDC
iDCx, iDCy
绘制pDC的左上角位置
iDCWidth,iDCHeight
绘制到pDC上的宽度和高度
dLbx, dLby
指定图形区域的左下角坐标
dRtx, dRty
指定图形区域的右下角坐标

VC参考例程:

void CTestCommands:rawToBmp()
{

//
选择让用从图上选择个存位图的区域。


acutPrintf(_T("\n
请点取存位图的区域:"));



//
动态拖放输入,让用户确定要保存的区域


CRectSelJig getRect;

   

// pt1,pt2
是矩形框的两点


AcGePoint3d pt1,pt2;


if(!getRect.DoIt(pt1,pt2) )


return;



//
让用户选择保存的位图文件.


CTestDlg* pDlg = (CTestDlg*)AfxGetApp()->GetMainWnd();


CFileDialog openDlg(FALSE,_T("bmp"),NULL,


OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,


_T("bmp(*.bmp) | *.bmp||"),


pDlg);



CString sFileName;


if(openDlg.DoModal() == IDOK)


{


sFileName = openDlg.GetPathName();


}


else


{


return;


}



int iBmpWidth
= 1000;


int iBmpHeight = 500;



//
开始保存文件.


CDC dc;


dc.Attach(::GetDC(NULL));



CBitmap bm;


bm.CreateCompatibleBitmap(&dc,iBmpWidth,iBmpHeight);



CDC tmpDc;


tmpDc.CreateCompatibleDC(&dc);


CBitmap*pOld = tmpDc.SelectObject(&bm);



if(MxDraw:rawToDc(MxDraw::GetCurOcxHandle(),


&tmpDc,0,0,iBmpWidth,iBmpHeight,


pt1.x,pt1.y,pt2.x,pt2.y


)


)


{


tmpDc.SelectObject(pOld);


if(SaveBmp(&bm,&dc,sFileName) )


{


AfxMessageBox(_T("
保存成功"));


}


else


{


AfxMessageBox(_T("
保存失败"));


}



}


else


{


AfxMessageBox(_T("
未知原因,保存失败"));


tmpDc.SelectObject(pOld);


}

}


bool CTestCommands::SaveBmp(CBitmap* pBmp,CDC* pDc,const CString& sBmpFilePath)
{

if(sBmpFilePath.IsEmpty() )


{


AfxMessageBox(_T("
文件路径为空"));


return false;


}


BITMAP btm;


pBmp->GetBitmap(&btm);



DWORD size
= btm.bmWidthBytes * btm.bmHeight;


LPSTR lpData = (LPSTR)GlobalAlloc(GPTR,size);


/////////////////////////////////////////////


BITMAPINFOHEADER bih;


bih.biBitCount=btm.bmBitsPixel;


bih.biClrImportant=0;


bih.biClrUsed=0;


bih.biCompression=0;


bih.biHeight=btm.bmHeight;


bih.biPlanes=1;


bih.biSize=sizeof(BITMAPINFOHEADER);


bih.biSizeImage=size;


bih.biWidth=btm.bmWidth;


bih.biXPelsPerMeter=0;


bih.biYPelsPerMeter=0;


///////////////////////////////////


GetDIBits(pDc->GetSafeHdc(),*pBmp,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);



BITMAPFILEHEADER bfh;


bfh.bfReserved1=bfh.bfReserved2=0;


bfh.bfType=((WORD)('M'<< 8)|'B');


bfh.bfSize=54+size;


bfh.bfOffBits=54;


bool isSuc = false;


CFile bf;


if(bf.Open(sBmpFilePath,CFile::modeCreate|CFile::modeWrite))


{


bf.Write(&bfh,sizeof(BITMAPFILEHEADER));


bf.Write(&bih,sizeof(BITMAPINFOHEADER));


bf.Write(lpData,size);


bf.Close();


isSuc = true;


}


else


{


AfxMessageBox(_T("
创建文件失败"));


}


GlobalFree(lpData);


return isSuc;

}
发表于 2012-6-29 09:57 | 显示全部楼层
mark
好,有空再来
发表于 2016-9-28 14:19 | 显示全部楼层
怎么使用,有方法吗?我还不知道
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-3-29 07:21 , Processed in 0.209880 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表