samedi 6 janvier 2018

How can I reliably set the backroundimage of a picturebox to be a random color?

I have a fairly simple color randomization snippet that I am using so I can accurately see the bounds of an image matrix I am making on screen. Problem is that I can not get it to work 100% of the time.

$listingImage = [System.Windows.Forms.PictureBox]::new()
...
# Temp color code to help with visual
$listingImage.BackColor = ([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))

The rest of the code follows in the question but that is the heart of what I am trying to do. I have seen it working several times but repeated executions show what seem like, empty, blank or otherwise invisible picture boxes. If I hard code a colour like $listingImage.BackColor = [System.Drawing.Color]::DarkCyan I can see that working fine every time. I want different colours though so I can see how to boxes are lining up with each other.

The code you can use for testing is as follows:

Add-Type -AssemblyName System.Windows.Forms

$imageContainerSize = [Drawing.Size]::new(100,100)  # Width, Height
$numberOfImages = [pscustomobject]@{
    Horizontal = 5
    Vertical = 4
}
$formOverallSize = [Drawing.Size]::new(
    $imageContainerSize.Width * $numberOfImages.Horizontal,
    $imageContainerSize.Height * $numberOfImages.Vertical
)

$listingImageForm = New-Object System.Windows.Forms.Form
$listingImageForm.Text            = $listing.URL
$listingImageForm.Size            = $formOverallSize
$listingImageForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$listingImageForm.StartPosition   = [System.Windows.Forms.FormStartPosition]::CenterScreen


$imageMatrixXOffset = 0
$imageMatrixYOffset = 0

# Load the image place holder image.
$placeholderImagePath = "m:\scripts\test.png"
# $placeholderImage = [system.drawing.image]::FromStream([IO.MemoryStream]::new([System.IO.File]::ReadAllBytes($placeholderImagePath)))


# Create an image matrix from the images provided in a listing group
for ($verticalImageIndex = 0; $verticalImageIndex -lt $numberOfImages.Vertical; $verticalImageIndex++){ 
    for ($horizonalImageIndex = 0; $horizonalImageIndex -lt $numberOfImages.Horizontal; $horizonalImageIndex++){ 

        $listingImage = [System.Windows.Forms.PictureBox]::new()
        $listingImage.Size = $imageContainerSize
        $listingImage.BorderStyle = [System.Windows.Forms.BorderStyle]::None
        $listingImage.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::CenterImage
        $listingImage.Location = [System.Drawing.Point]::new($horizonalImageIndex * $listingImage.Size.Width  + $imageMatrixXOffset, 
                                                             $verticalImageIndex  * $listingImage.Size.Height + $imageMatrixYOffset )
        # Temp color code to help with visual
        $listingImage.BackColor =  ([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))

        # Place the image based 
        # $listingImage.Image = $placeholderImage
        $listingImage.Tag = "h:$horizonalImageIndex v:$verticalImageIndex"

        # Download the image as a memory stream to bypass saving the file
        $listingImageForm.Controls.Add($listingImage)
    }
}

# Adjust the size of the form to account for the title bar and the width of the form. 
$formBorderWidth = ($listingImageForm.Width - $listingImageForm.ClientSize.Width) / 2
$formTitleBarHeight = $listingImageForm.Height – $listingImageForm.ClientSize.Height – 2 * $formBorderWidth

# Adjust for based on previosly calculated values
$listingImageForm.Size.Height = $listingImageForm.Size.Height + $formTitleBarHeight + ($formBorderWidth * 2)
$listingImageForm.Size.Width  = $listingImageForm.Size.Width + ($formBorderWidth * 2)


$listingImageForm.Add_Shown({$listingImageForm.Activate()})
[void]$listingImageForm.ShowDialog()
"Form Height : $($listingImageForm.Size.Height)"
"Form Width  : $($listingImageForm.Size.Width)"
"Image Height: $($imageContainerSize.Height)"
"Image Width : $($imageContainerSize.Width)"

$listingImageForm.Dispose()

Why is my randomization not working correctly? I really don't think it is the randomization itself since I can run




Aucun commentaire:

Enregistrer un commentaire