Windows Forms PictureBox y Group

Hello I have been trying to add an image and a group in forms but unfortunately I have not been able to reach a solution, can someone help me with that

import clr
clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')

from System.Reflection import Assembly
from System.Windows.Forms import Form
from System.Drawing import Icon

iconFilename = r'D:\ICONOS\5.ico'
icon = Icon(iconFilename)

form = Form()
form.Text = "Hello World"
form.Icon = icon
form.Show()

OUT = form

image

Hi,
you need to use this method
you can also store your image as base64 directly in your script

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class Form9(Form):
	def __init__(self, base64Str):
		self._base64Str = base64Str
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._pictureBox1 = System.Windows.Forms.PictureBox()
		self._pictureBox1.BeginInit()
		self.SuspendLayout()
		# 
		# pictureBox1
		# 
		picTop = System.Convert.FromBase64String(self._base64Str)
		self._imageTop = System.Drawing.Image.FromStream(System.IO.MemoryStream(picTop))
		self._imageTopResize = Bitmap(self._imageTop, Size(100, 100))
		self._pictureBox1.Location = System.Drawing.Point(45, 67)
		self._pictureBox1.Name = "pictureBox1"
		self._pictureBox1.Size = System.Drawing.Size(100, 100)
		self._pictureBox1.TabIndex = 0
		self._pictureBox1.TabStop = False
		self._pictureBox1.Image = self._imageTopResize
		# 
		# Form9
		# 
		self.ClientSize = System.Drawing.Size(284, 261)
		self.Controls.Add(self._pictureBox1)
		self.Name = "Form9"
		self.Text = "Form9"
		self._pictureBox1.EndInit()
		self.ResumeLayout(False)
		
objForm = Form9(IN[0])
objForm.ShowDialog()

demoBase64_Winform.dyn (16.3 KB)

3 Likes

really surprising, I thank you very much for the solution

@c.poupin I tried to add a couple of texts and a button but it generates an error, could you tell me that I am doing wrong

# Charger les bibliothèques DesignScript et Standard Python
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class Form9(Form):
	def __init__(self, imgpath, valueA, valueB):
		self._imgpath = imgpath
		self.InitializeComponent()
		self._valueInitA = str(valueA)
		self._valueInitB = str(valueB)
		
		# texBox
		
		self.textbox = TextBox()
		self.textbox.Text = self._valueInitA
		self.textbox.Location = Point(25, 25)
		self.textbox.Width = 150
		
		# texBox1
		self.textbox1 = TextBox()
		self.textbox1.Text = self._valueInitB
		self.textbox1.Location = Point(25, 55)
		self.textbox1.Width = 150
		
		# button1
		self.button1 = Button()
		self.button1.Text = 'Press Me'
		self.button1.Location = Point(25, 125)
		
		self.AcceptButton = self.button1

		self.Controls.Add(self.textbox)
		self.Controls.Add(self.textbox1)
		self.Controls.Add(self.button1)

		self.values = []
		self.button1.Click += self.update
		
	def InitializeComponent(self):
		self._pictureBox1 = System.Windows.Forms.PictureBox()
		self._pictureBox1.BeginInit()
		self.SuspendLayout()
		# 
		# pictureBox1
		# 
		self._imageTop = Image.FromFile(self._imgpath)
		self._imageTopResize = Bitmap(self._imageTop, Size(100, 100))
		self._pictureBox1.Location = System.Drawing.Point(45, 67)
		self._pictureBox1.Name = "pictureBox1"
		self._pictureBox1.Size = System.Drawing.Size(100, 100)
		self._pictureBox1.TabIndex = 0
		self._pictureBox1.TabStop = False
		self._pictureBox1.Image = self._imageTopResize
		# 
		# Form9
		# 
		self.ClientSize = System.Drawing.Size(284, 261)
		self.Controls.Add(self._pictureBox1)
		self.Name = "Form9"
		self.Text = "Form9"
		self._pictureBox1.EndInit()
		self.ResumeLayout(False)
		
	def update(self, sender, event):
		for control in self.Controls:
			if isinstance(control, TextBox):
				self.values.append(control.Text)
		self.Close()


objForm = Form9(IN[0])
objForm = SimpleTextBoxForm(IN[1], IN[2])
objForm.ShowDialog()

the code that I am trying to incorporate is the following

import System
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Drawing import Point
from System.Windows.Forms import Application, Button, Form, Label, TextBox

class SimpleTextBoxForm(Form):
    def __init__(self, valueA, valueB):
        self._valueInitA = str(valueA)
        self._valueInitB = str(valueB) 

        self.Width = 220
        self.Height = 200

        self.textbox = TextBox()
        self.textbox.Text = self._valueInitA
        self.textbox.Location = Point(25, 25)
        self.textbox.Width = 150

        self.textbox1 = TextBox()
        self.textbox1.Text = self._valueInitB
        self.textbox1.Location = Point(25, 55)
        self.textbox1.Width = 150

        self.button1 = Button()
        self.button1.Text = 'Press Me'
        self.button1.Location = Point(25, 125)
        
        self.AcceptButton = self.button1

        self.Controls.Add(self.textbox)
        self.Controls.Add(self.textbox1)
        self.Controls.Add(self.button1)
        
        self.values = []
        self.button1.Click += self.update

    def update(self, sender, event):
        for control in self.Controls:
            if isinstance(control, TextBox):
              self.values.append(control.Text)
        self.Close()

form = SimpleTextBoxForm(IN[0], IN[1])
form.ShowDialog()

OUT = form.values

Hello @Christhian
if you are a beginner on python or on Winform, I do not recommend copying and pasting a piece of code,
(I made this mistake a long time ago when I started in Lisp …)

Here are some interesting resources with IronPython and Winform

I will publish the solution later :wink:

@c.poupin You are absolutely right, I am just starting with python and winform, thank you very much for these guides they will be very helpful, an additional query you know where I can find a guide on the Civil 3d API with python.

I will be waiting for the solution, thank you very much

@c.poupin please do not forget to place the solution

if I did not answer immediately, it is so that you try for yourself with the links given above :slightly_smiling_face:

here an example

import sys
import clr
import System
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Drawing import Point, Image, Bitmap, Size
from System.Windows.Forms import Application, Button, Form, Label, TextBox

class SimpleTextBoxForm(Form):
    def __init__(self, valueA, valueB, imgpath):
        self._imgpath = imgpath
        self._valueInitA = str(valueA)
        self._valueInitB = str(valueB) 
        self._pictureBox1 = System.Windows.Forms.PictureBox()
        self._pictureBox1.BeginInit()

        self.Width = 220
        self.Height = 300

        self.textbox = TextBox()
        self.textbox.Text = self._valueInitA
        self.textbox.Location = Point(25, 25)
        self.textbox.Width = 150

        self.textbox1 = TextBox()
        self.textbox1.Text = self._valueInitB
        self.textbox1.Location = Point(25, 55)
        self.textbox1.Width = 150

        self._imageTop = Image.FromFile(self._imgpath)
        self._imageTopResize = Bitmap(self._imageTop, Size(100, 100))
        self._pictureBox1.Location = System.Drawing.Point(45, 90)
        self._pictureBox1.Name = "pictureBox1"
        self._pictureBox1.Size = System.Drawing.Size(100, 100)
        self._pictureBox1.TabIndex = 0
        self._pictureBox1.TabStop = False
        self._pictureBox1.Image = self._imageTopResize

        self.button1 = Button()
        self.button1.Text = 'Press Me'
        self.button1.Location = Point(25, 230)
        
        self.AcceptButton = self.button1

        self.Controls.Add(self.textbox)
        self.Controls.Add(self.textbox1)
        self.Controls.Add(self.button1)
        self.Controls.Add(self._pictureBox1)

        self.values = []
        self.button1.Click += self.update
        self._pictureBox1.EndInit()

    def update(self, sender, event):
        for control in self.Controls:
            if isinstance(control, TextBox):
              self.values.append(control.Text)
        self.Close()

form = SimpleTextBoxForm(IN[0], IN[1], IN[2])
form.ShowDialog()

OUT = form.values
2 Likes