Winforms 使用powershell在windows.forms中的图像上绘制多个圆

Winforms 使用powershell在windows.forms中的图像上绘制多个圆,winforms,powershell,Winforms,Powershell,我大部分时间都在创建一个GUI,它将在picturebox中弹出一个图像,并允许用户通过单击在其上绘制多个红色圆圈(然后将其保存到传递的文件路径/名称) 用户通过单击图像上希望左上角起点所在的位置并拖动以调整其大小(上一个圆消失并显示正确大小的圆)绘制圆,直到松开鼠标 第一个圆显示正确,但对于所有后续圆,它不会正确刷新,因此,拖动鼠标时,不会调整一个圆的大小,而是会显示一组不同大小的圆,对应于所有过去拖动的鼠标位置 我一整天都在这上面敲我的头,我错过了什么可以让后续圆圈正确显示 $file =

我大部分时间都在创建一个GUI,它将在picturebox中弹出一个图像,并允许用户通过单击在其上绘制多个红色圆圈(然后将其保存到传递的文件路径/名称)

用户通过单击图像上希望左上角起点所在的位置并拖动以调整其大小(上一个圆消失并显示正确大小的圆)绘制圆,直到松开鼠标

第一个圆显示正确,但对于所有后续圆,它不会正确刷新,因此,拖动鼠标时,不会调整一个圆的大小,而是会显示一组不同大小的圆,对应于所有过去拖动的鼠标位置

我一整天都在这上面敲我的头,我错过了什么可以让后续圆圈正确显示

$file = "...TestImage.PNG"


$script:DrawingStatus = "In_Progress" #switch case for mouse drag



$panel1 = New-Object Windows.Forms.Panel
$Panel1.AutoScroll = $true;



$win = New-Object Windows.Forms.Form
$box = New-Object Windows.Forms.PictureBox
$box.Image = [System.Drawing.Image]::FromFile($file)
$box.AutoSize = $true




$src = $box.Image



$bmp=new-object System.Drawing.Bitmap $src.width, $src.height
$script:graphics=[System.Drawing.Graphics]::FromImage($bmp)
$units = [System.Drawing.GraphicsUnit]::Pixel



$destRect = new-object Drawing.Rectangle 0, 0, $src.width, $src.height
$srcRect = new-object Drawing.Rectangle 0, 0, $src.width, $src.height


$graphics.DrawImage($src, $destRect, $srcRect.X, $srcRect.Y, $srcRect.width, $srcRect.height, $units)

$image = $src 

$Clicky4 = { #Executed on Mouse Drag while mouse is down

param($point)



$graphics.DrawImage($image, $destRect, $srcRect.X, $srcRect.Y, $srcRect.width, $srcRect.height, $units)
$box.image = $bmp


$mouse = [System.Windows.Forms.Cursor]::Position; 
$point2 = $box.PointToClient($mouse); 
write-host $point; 
$mypen = new-object System.Drawing.Pen black
$mypen.color = "red"
$mypen.width = 3



$graphics.DrawEllipse($mypen, $point.X, $point.Y, ($point2.X-$point.X), ($point2.Y-$point.Y))




}

$clicky2 = { #executed on mouse down, calls clicky4

$script:DrawingStatus = "In_Progress"


write-host "Test 2 Passed!"; 
$mouse = [System.Windows.Forms.Cursor]::Position; 


$point = $box.PointToClient($mouse); 
write-host $point; 
$box.tag = $point;


$superclick = {

switch ($script:DrawingStatus){

"In_Progress" {


Invoke-Command $clicky4 -ArgumentList $box.tag

}

"Done"{ #does nothing, so dragging mouse with mouse up does not trigger circle resizing

}


}




}

$box.add_MouseMove($superclick)

}



$clicky3 = { #executed on mouse up
write-host "Test 3 Passed!";

$script:DrawingStatus = "Done"



$script:image = $script:BMP #this is my attempt to create a BMP with the drawn circle after resizing so that the box image can be refreshed to this when drawing the next circle

$script:graphics=[System.Drawing.Graphics]::FromImage($script:image)


$mouse = [System.Windows.Forms.Cursor]::Position; 
$point = $box.PointToClient($mouse); 
write-host $point; 
$array = @(); 
$array += $box.tag; 
$array += $point; 
$win.tag = $array



}





$Panel1.controls.add($box)

$win.width = 1500
$win.height = 800



$panel1.width = ($win.width - 15)

$panel1.height = ($win.height -50)




$box.add_MouseDown($clicky2)
$box.add_MouseUp($clicky3)



$win.Controls.Add($panel1)





$win.ShowDialog()

write-host "Coords: " $win.tag
$initial = $win.tag[0]
$Final = $win.tag[1]


write-host "Initial: " $initial
write-host "Final: " $Final


write-host "X1: " $initial.X
write-host "Y1: " $initial.Y

write-host "X2: " $final.X
write-host "Y2: " $final.Y

获得多个圆的最大原因是代码没有删除旧的圆。因为您希望他们能够在窗口中绘制多个圆,所以最好的方法是使用两个对象。一个是永久性圆圈,另一个是正在调整大小的临时圆圈。MouseUp会将当前临时椭圆添加到永久图形spath,而鼠标移动会清除并重新绘制临时图形spath,并调用Invalidate。在Add_Paint事件调用中,您可以将两个图形直接绘制到ImageBox,以便在基础图像上绘制。保存后,您可以在保存到文件之前将永久路径绘制到图像上以永久更改它们。

您在此处所做的任何操作都不是特定于PowerShell的。这是Winforms,如果您使用VB.net/C等等,您会遇到完全相同的问题。您只是使用PowerShell作为您的后台代码,但是,您的评论说了两件不同的事情。也就是说,你说你想改变一个现有的圆并创建一个新的圆,而你的代码中没有任何东西表明你正在选择一个现有的对象/圆进行操作。硅基生命形式(硬件/软件)仅符合要求。他们只能按命令去做。在执行过程中,您设置/评估了哪些断点?