Eto - Convert System.Drawing.Bitmap to Eto.Drawing Bitmap?

Hello,

I’m getting a list of block instance preview “thumbnails” as System.Drawing.Bitmaps that I want to use in an Eto.ImageView control like so:

image

It appears I cannot directly pass the system bitmap to the Eto bitmap.

Is there a method I can use that handles this conversion? Perhaps casting, or something else I am overlooking?

Here’s the excerpt from my code:

        # Calculate grid cell count
        cell_size = 150  # Set your desired cell size here
        column_count, row_count = self.calculate_grid(cell_size)

        top_padding = 30

        # capture bitmap
        thumbnail = forms.ImageView()
        t_size = System.Drawing.Size(cell_size, cell_size)
        thumbnail.Image = None

        # # get active view
        # view = sc.doc.Views.ActiveView
        # thumbnail_bitmap = view.CaptureToBitmap()
        # Rhino.RhinoApp.WriteLine(str(thumbnail_bitmap))

        block_table = sc.doc.ActiveDoc.InstanceDefinitions  # Get All Block Instance Definitions In Document
        block_names = []
        block_thumbnails = []
        for block in block_table:
            block_names.append(block.Name)  # Get Individual Block Name
            block_thumbnails.append(Rhino.DocObjects.InstanceDefinition.CreatePreviewBitmap(block, Rhino.Display.DefinedViewportProjection.Perspective, t_size))  # Create Bitmap Of Block Preview Image
        Rhino.RhinoApp.WriteLine(str(block_names))
        Rhino.RhinoApp.WriteLine(str(block_thumbnails))

        # Add text labels and thumbnails to the grid layout
        for row in range(row_count):
            for col in range(column_count):
                # Create a panel to hold label and thumbnail
                panel = forms.Panel()
                panel.Size = drawing.Size(cell_size, cell_size)
                panel_layout = forms.TableLayout(panel.Size)
                panel_layout.Padding = drawing.Padding(5)
                panel_layout.Spacing = drawing.Size(5, 5)

                # Add label
                label = forms.Label()
                label.Text = f"({row}, {col}) - "
                if row * column_count + col < len(block_names):
                    label.Text += block_names[row * column_count + col]
                else:
                    label.Text += "No block defined"
                label.TextColor = b_color_light if dark_mode else b_color_dark
                panel_layout.Add(label, 0, 0)

                # Add thumbnail
                thumbnail = forms.ImageView()

                thumbnail.Size = drawing.Size(cell_size - 10, cell_size - 40)
                # thumbnail.SizeMode = forms.PictureBoxSizeMode.StretchImage
                if row * column_count + col < len(block_thumbnails):
                    thumbnail.Image = block_thumbnails[row * column_count + col]
                else:
                    thumbnail.Image = None
                panel_layout.Add(thumbnail, 0, 1)

                panel.Content = panel_layout

                # Add panel to the layout
                self.layout.Add(panel, (col * cell_size) + lib_padding, (row * cell_size) + lib_padding + top_padding)

        self.Content = self.layout

and the type error I am getting:

TypeError: System.Drawing.Bitmap value cannot be converted to Eto.Drawing.Image

Thank you all for your help!

Hi @michaelvollrath, you might try Rhino.UI.EtoExtensions.ToEto(bitmap)

_
c.

3 Likes

That appears to have done it, thank you again @clement , you’re the best!

If you do: using Rhion.UI.EtoExtensions you should be able to do var etoBitmap = bitmap.ToEto();

Thanks but this doesn’t seem to work in Python3? Am I missing something @nathanletwory ?

import Rhino.UI.EtoExtensions
ModuleNotFoundError: No module named 'Rhino.UI.EtoExtensions'

Ow right, sorry. That’s a C# thing (:

1 Like

No worries! Wanted to make sure I wasn’t missing something