我使用了一个相当简单的颜色随机化片段,因此我可以准确地看到我在屏幕上制作的图像矩阵的边界。问题是我不能100%的时间让它工作。
$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()
为什么我的随机化无法正常工作?我真的不认为这是随机化本身,因为我可以运行
答案 0 :(得分:1)
问题在于随机化上限。
[System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999)
所以这只会产生最大值999999.这个数字永远不会高到足以改变随机'的 alpha通道/不透明度值。颜色。如果您在命令行中继续尝试该代码,您将看到A值始终为零。
([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))
R : 10
G : 177
B : 51
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : ab133
一个更大的测试集来证明这一点。
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但是对于应该正常工作的简单测试。
或者,正如Ansgar Wiechers在评论中提到的那样,这可能是解决问题的更友好的方法
[Drawing.Color]::FromArgb((Random 256),(Random 256),(Random 256),(Random 256))