dukeyongwang 发表于 2009-11-25 10:26:00

CAD——生成图片

在『Kean』的BlockView.NET中有生成图片的功能,拷贝一个View,然后调用RenderToImage()方法,可是为什么总是提示【对象的当前状态使该操作无效】,请大家帮帮忙?
private void OutputImageMenuItem_Click(object sender, EventArgs e)
    {
      // pick the place where o output the image type
      Autodesk..Windows.SaveFileDialog dialog = new Autodesk.AutoCAD.Windows.SaveFileDialog("RenderToImage", null, "jpg;png;tif;bmp", "BlockViewSnapshotBrowseDialog", Autodesk.AutoCAD.Windows.SaveFileDialog.SaveFileDialogFlags.AllowAnyExtension);
      // if all is ok?
      if (DialogResult.OK == dialog.ShowDialog())
      {
      // create an offscreen device
      using (Device device = new Device())
      {
          // get the size of the GS view
          Size size = mPreviewCtrl.ClientRectangle.Size;
          // resize the device to this
          device.OnSize(size);
          device.BackgroundColor = Color.Black;
          device.SetLogicalPalette(GSUtil.MyAcadColorMs);
          device.OnRealizeBackgroundPalette();
          device.OnRealizeForegroundPalette();
         
          // make a copy of the gs view
          using (Autodesk.AutoCAD.GraphicsSystem.View view = mPreviewCtrl.mpView.Clone(true, true))
          {
            try
            {
            // add it to the device
            device.Add(view);
            // now render the image to a bitmap
            using (System.Drawing.Bitmap bitmap = view.RenderToImage())
            {
                // now save it out!!
                bitmap.Save(dialog.Filename);
            }
            }
            finally
            {
            // do a clear up
            device.EraseAll();
            }
          }
      }
      }
    }

雪山飞狐_lzh 发表于 2009-11-25 15:07:00

public Bitmap GetImage()
      {
            return m_Device.GetSnapshot(m_View.Viewport);
      }

dukeyongwang 发表于 2009-11-25 16:26:00

真的可以实现生成图片。
我在这里提供第二种方法:
调用WINAPI中的BitBlt()截屏方法,也可以实现。实现的效果一样。不过还是弧哥的方法简单,明了。
页: [1]
查看完整版本: CAD——生成图片