Пример #1
0
        protected Bitmap GetThumbnailImage(int width) // Implemented abstract function in the base class
        {
            bool everythingisOk = true;



            MemoryStream memStream = new MemoryStream();



            Thread thread = new Thread(() =>
            {
                //string tracefile = @"C:\temp\stlviewer.stl_log";
                string tracefile = TargetFile.Replace(".stl", ".stl_log");


                Logg("_______________________________________________________________________________");
                FileInfo fileInfo = new FileInfo(TargetFile);
                Logg($"Name: {fileInfo.Name}");
                Logg($"Dir.: {fileInfo.Directory}");

                Logg($"Size: {fileInfo.Length}");
                Logg("1. Tread started");


                object convertFromString = null;
                Color color              = new Color();
                ModelVisual3D root       = new ModelVisual3D();
                Model3DGroup model       = new Model3DGroup();
                PerspectiveCamera camera = new PerspectiveCamera();
                BitmapExporter exporter  = new BitmapExporter();


                if (everythingisOk)
                {
                    try
                    {
                        string colorString = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Marlin3DprinterTool", "Color", "Blue");

                        convertFromString = ColorConverter.ConvertFromString(colorString);
                        Logg($"2. Color: {convertFromString}");
                        if (convertFromString != null)
                        {
                            color = (Color)convertFromString;
                        }
                    }
                    catch (Exception e)
                    {
                        Logg($"Something goes wrong in Color {e.Message}");
                        everythingisOk = false;
                    }
                }

                if (everythingisOk)
                {
                    if (convertFromString != null)
                    {
                        Viewport3D viewport = new Viewport3D();

                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"3. STLreader");
                                StLReader stlReader       = new StLReader();
                                stlReader.DefaultMaterial = new DiffuseMaterial(new SolidColorBrush(color));

                                Logg($"4. Read the Targetfile {TargetFile}");
                                model = stlReader.Read(TargetFile);
                                Logg($"5. Model read. New Viewport");
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in STLreader {e.Message}");
                                everythingisOk = false;
                            }
                        }



                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"6. Arrange with / height {width}/{width}");
                                viewport.Measure(new Size(width, width));
                                viewport.Arrange(new Rect(0, 0, width, width));

                                Logg($"7. Create root");

                                viewport.Children.Add(root);


                                Logg($"8. Create camera");

                                camera.Position          = new Point3D(2, 16, 20);
                                camera.LookDirection     = new Vector3D(-2, -16, -20);
                                camera.UpDirection       = new Vector3D(0, 0, 1);
                                camera.FieldOfView       = 45;
                                camera.NearPlaneDistance = 0.1;
                                camera.FarPlaneDistance  = double.PositiveInfinity;
                                viewport.Camera          = camera;
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in STLarrange {e.Message}");
                                everythingisOk = false;
                            }
                        }

                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"8.1. Create light");
                                root.Children.Add(new DefaultLights());
                                Logg($"8.2 Load model");
                                root.Content = model;


                                camera.ZoomExtents(viewport);
                                Logg($"9. Create Background");
                                Brush background = new SolidColorBrush(Colors.Transparent);

                                Logg($"10. Create Exporter");

                                exporter.Background             = background;
                                exporter.OversamplingMultiplier = 1;
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in Background/Light {e.Message}");
                                everythingisOk = false;
                            }
                        }

                        if (everythingisOk)
                        {
                            try
                            {
                                Logg($"11. Export to memorystream");
                                exporter.Export(viewport, memStream);
                                //Logg($"11. Export to {tempBitmap}");
                                //FileStream fs = new FileStream(tempBitmap, FileMode.Create, FileAccess.ReadWrite);
                                //exporter.Export(viewport, fs);
                                //fs.Flush();
                                //fs.Close();
                            }
                            catch (Exception e)
                            {
                                Logg($"Something goes wrong in Export to memory Stream {e.Message}");
                                everythingisOk = false;
                            }
                        }
                    }
                }
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();



            if (everythingisOk)
            {
                try
                {
                    Logg($"12. Create Bitmap from memorystream");
                    Bitmap thumbnailBitmap = (Bitmap)Image.FromStream(memStream);

                    return(thumbnailBitmap);
                }
                catch (Exception e)
                {
                    Logg($"Something goes wrong in Bitmap from messageStream {e.Message}");
                    Logg("_______________________________________________________________________________");

                    return(null);
                }
            }
            Logg("_______________________________________________________________________________");

            return(null);
        }