Powershell 如何可靠地将picturebox的backroundimage设置为随机颜色?
我使用了一个非常简单的颜色随机化片段,这样我就可以准确地看到我在屏幕上制作的图像矩阵的边界。问题是,我不能让它100%的时间工作Powershell 如何可靠地将picturebox的backroundimage设置为随机颜色?,powershell,random,colors,powershell-5.0,Powershell,Random,Colors,Powershell 5.0,我使用了一个非常简单的颜色随机化片段,这样我就可以准确地看到我在屏幕上制作的图像矩阵的边界。问题是,我不能让它100%的时间工作 $listingImage = [System.Windows.Forms.PictureBox]::new() ... # Temp color code to help with visual $listingImage.BackColor = ([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 99999
$listingImage = [System.Windows.Forms.PictureBox]::new()
...
# Temp color code to help with visual
$listingImage.BackColor = ([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))
问题中的其余代码如下,但这是我试图做的核心。我已经看到它工作了好几次,但重复执行显示了看起来像是空的、空白的或其他看不见的图片框。如果我硬编码一种颜色,比如$listingImage.BackColor=[System.Drawing.Color]::DarkCyan
我每次都能看到它工作得很好。不过我想要不同的颜色,这样我可以看到盒子是如何排列的
可用于测试的代码如下所示:
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()
为什么我的随机化工作不正常?我真的不认为这是随机化本身,因为我可以运行问题在于随机化上限
[System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999)
因此,这将只生成最大99999的值。该数字永远不会高到足以更改“随机”颜色的alpha通道/不透明度值。如果您一直在命令行中尝试该代码,您将看到A值始终为零
一个更大的测试集来证明这一点
1..1000 | ForEach-Object {
([system.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))} |
Group-Object -Property A -NoElement
Count Name
----- ----
1000 0
这就是为什么它看起来不起作用,因为不透明度始终为零。我怀疑它是周期性工作的,因为您可能在测试之间更改了随机代码,而没有将更改关联起来
RGBA值可以表示为int32整数值。将你的随机化设置为将其用作上限将证明更有效
[System.Drawing.Color](Get-Random ([int32]::MaxValue))
是的,您可以再次将所有0 alpha随机化,但对于简单的测试,应该可以正常工作
或者,正如评论中提到的那样,这可能是一个更友好的解决方案
[Drawing.Color]::FromArgb((Random 256),(Random 256),(Random 256),(Random 256))
使用4个介于0和255之间的随机输入值不是更好吗?是的,有很多其他方法可以做到这一点。我只是想弄明白为什么我的坏了。如果我试图更多地调整随机性,我会考虑的。我遇到过这种情况,但我不准备放弃我正在做的事情,我想弄清楚为什么它不起作用。“我是在写问题的时候想出来的,只是不想浪费我写问题的时间。@AnsgarWiechers我点头补充了你的答案。”
[Drawing.Color]::FromArgb((Random 256),(Random 256),(Random 256),(Random 256))