如何插入jpg?
C#开发|冯美娟|2018-07-26 10:44
-
回答:
private void InsertImage() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "jpg 文件(*.jgp)|*.jpg"; if (ofd.ShowDialog() != DialogResult.OK) { return; } Image tmpImage = null; try { tmpImage = Image.FromFile(ofd.FileName); } catch(Exception exp) { MessageBox.Show(exp.Message); return; } if (tmpImage == null) return; double dW = tmpImage.Width; double dH = tmpImage.Height; tmpImage = null; // 图片文件的插入点。 MxDrawPoint point = (MxDrawPoint)(axMxDrawX1.GetPoint(false,0,0, "\n 指定插入点:") ); if (point == null) return; string sFilePath = ofd.FileName; string sFileName = sFilePath; MxDrawDatabase database = (MxDrawDatabase)(axMxDrawX1.GetDatabase() ); MxDrawDictionary dict = database.GetNamedObjectsDictionary(); if (dict == null) return; MxDrawDictionary imageDict = (MxDrawDictionary)(dict.GetAt("ACAD_IMAGE_DICT", false) ); if(imageDict == null) { // 增加一个字典对象。 imageDict = (MxDrawDictionary)(dict.AddObject("ACAD_IMAGE_DICT", "McDbDictionary") ); } if (imageDict == null) return; MxDrawRasterImageDef imgedef = (MxDrawRasterImageDef)(imageDict.GetAt(sFileName, false) ); if(imgedef == null) { imgedef = (MxDrawRasterImageDef)(imageDict.AddObject(sFileName, "McDbRasterImageDef")); if (imgedef == null) return; } imgedef.SourceFileName = sFileName; MxDrawBlockTableRecord curSpace = database.CurrentSpace(); MxDrawVector3d uCorner = new MxDrawVector3d(); uCorner.x = dW; MxDrawVector3d vOnPlane = new MxDrawVector3d(); vOnPlane.y = dH; curSpace.AddImage(point.x,point.y,uCorner.x,uCorner.y, vOnPlane.x,vOnPlane.y, imgedef.ObjectID); MxDrawApplication app = new MxDrawApplication(); app.ZoomAll(); }