简体   繁体   English

GetPreviewBitmapFile保存具有奇怪颜色的预览位图(SolidWorks)

[英]GetPreviewBitmapFile save preview bitmap with strange colors (SolidWorks)

I'm developing C# VSTA macro for our manufacturing department. 我正在为我们的制造部门开发C#VSTA宏。 I'm using SldWorks.GetPreviewBitMapFile to save a bitmap preview of the selected model and show it in the picturebox as shown below: 我正在使用SldWorks.GetPreviewBitMapFile保存所选模型的位图预览,并将其显示在图片框中,如下所示:

在此处输入图片说明

The code looks fine and execute fine except for the strange colors: 该代码看起来不错,并且执行得很好,除了奇怪的颜色:

bool status = swApp.GetPreviewBitmapFile(filepath, configuration, "D:\\preview.bmp");
            pictureBox1.ImageLocation = "D:\\Preview.bmp";
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Refresh();

Has anyone had a similar problem and what's the remedy? 有没有人有类似的问题,有什么补救办法?

Here is an example of working code: 这是一个工作代码示例:

//to compare output
iSwApp.GetPreviewBitmapFile(@"c:\Path\Part1.SLDPRT", "Default", @"c:\Path\Part1_0.bmp");

object com = iSwApp.GetPreviewBitmap(@"c:\Path\Part1.SLDPRT", "Default");
stdole.StdPicture pic = com as stdole.StdPicture;
Bitmap bmp = Bitmap.FromHbitmap((IntPtr)pic.Handle);
bmp.Save(@"c:\Path\Part1_1.bmp");

There are few notes about GetPreviewBitmap from SW API: 关于SW API的GetPreviewBitmap的几点说明:

  • Currently only in-process applications (that is, macros or add-ins) can use this method; 当前,只有进程内的应用程序(即宏或加载项)可以使用此方法。 out-of-process applications (that is, executables) will get an automation error because the IPictureDisp interface cannot be marshalled across process boundaries. 进程外应用程序(即可执行文件)将获得自动化错误,因为IPictureDisp接口无法跨进程边界编组。 This is a Microsoft behavior by design. 这是设计使然的Microsoft行为。 See the Microsoft Knowledge Base for details. 有关详细信息,请参见Microsoft知识库。
  • This method is not supported in macros or out-of-process applications in SolidWorks x64. SolidWorks x64中的宏或进程外应用程序不支持此方法。

I was able to make it work in SolidWorks x64 only in .NET add-in and in VBA macros. 我能够使其仅在.NET插件和VBA宏中在SolidWorks x64中工作。 Let me know if you need add-in example. 让我知道您是否需要插件示例。

An update for SW2016. SW2016的更新。 The GetPreviewBitmapFile function works for an out-of-process Csharp console program. GetPreviewBitmapFile函数适用于进程外的Csharp控制台程序。 However, the colors of the generated bitmap are very strange. 但是,生成的位图的颜色非常奇怪。 It turns out the only valid solution is to use the SaveBmp function. 事实证明,唯一有效的解决方案是使用SaveBmp函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM