0

Can center the GIF file in the box. I've tried $objForm.StartPosition = "CenterScreen" and so many others and can't figure it out.

Here is the form:

$img = [System.Drawing.Image]::Fromfile($file);
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.WindowState= "Maximize"
$form.ControlBox = $false
$form.FormBorderStyle = "0"
$form.BackColor = [System.Drawing.Color]::black

$pictureBox1 = New-Object Windows.Forms.PictureBox

$pictureBox1.Width =  $img.Size.Width;
$pictureBox1.Height =  $img.Size.Height;
$pictureBox1.Image = $img;
$form.Controls.Add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
#$form.Show();    
$file = (Get-Item 'C:\ABCD.gif')
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
user10859837
  • 1
  • 1
  • 1
  • 2
    One suggestion: Rephrase the title of your issue as a question so others can better understand your issue. For example, "How can I center an image in WinForms dialog box?" Also, be sure to include the other options you've tried already so others can help you more easily. – Larz Jan 02 '19 at 21:42
  • 1
    I'd also put the very last line in front of the script. –  Jan 02 '19 at 21:45

1 Answers1

0

There are different solutions to show an image centered in a form. For example you can set the Dock property of PicureBox to Fill and set the SizeMode to Center:

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$pic =  New-Object System.Windows.Forms.PictureBox
$pic.BackColor = [System.Drawing.Color]::Transparent
$pic.Dock = [System.Windows.Forms.DockStyle]::Fill
$pic.ImageLocation = "https://i.stack.imgur.com/repwc.gif"
$pic.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::CenterImage
$form.Controls.Add($pic)
$form.ShowDialog() | Out-Null
$form.Dispose()

Then it will show like this:

enter image description here

To set more than one control to show in center, you can use a mix of TableLayoutPanel and FlowLayoutPanel take a look at this post.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398