【发布时间】:2021-01-23 11:21:44
【问题描述】:
我想从屏幕上获取图像,将其放大,然后将其绘制到表单中。
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Form creating
$Form = [Windows.Forms.Form]::new()
$Form.ControlBox = $false
$form.Width = 240
$form.Height= 240
$Form.Topmost = $True
$form.UseWaitCursor= $false
$Form.StartPosition = "CenterScreen"
[void] $Form.Show()
# gfx to form handle
$formGfx = [Drawing.Graphics]::FromHwnd($form.Handle)
# image from screen
$scrPic = [Drawing.Bitmap]::new(24, 24)
$scrGfx = [Drawing.Graphics]::FromImage($scrPic)
$mPos = [Windows.Forms.Cursor]::Position
# taking image from screen
$scrGfx.CopyFromScreen(
[Drawing.Point]::new($mPos.x-12, $mPos.y-12),
[Drawing.Point]::new(0, 0),
[Drawing.Size]::new(24, 24)
)
# new big image
$newPic = [Drawing.Bitmap]::new(240, 240)
$newgfx = [Drawing.Graphics]::FromImage($newPic)
$newRect = [Drawing.Rectangle]::new(0, 0, 240, 240)
$newPic.SetResolution($scrPic.HorizontalResolution, $scrPic.VerticalResolution)
# scaling settings (as i understand)
$newgfx.CompositingMode = [Drawing.Drawing2D.CompositingMode]::SourceCopy
$newgfx.CompositingQuality = [Drawing.Drawing2D.Compositingquality]::HighQuality
$newgfx.InterpolationMode = [Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$newgfx.SmoothingMode = [Drawing.Drawing2D.SmoothingMode]::HighQuality
$newgfx.PixelOffsetMode = [Drawing.Drawing2D.PixelOffsetMode]::HighQuality
$wrapMode = [Drawing.Imaging.ImageAttributes]::new()
$wrapMode.SetWrapMode([Drawing.Drawing2D.WrapMode]::TileFlipX)
# new image creating
$newgfx.DrawImage($scrPic, $newRect, 0, 0, $pic.Width, $pic.Height, [Drawing.GraphicsUnit]::Pixel, $wrapMode)
# drawing image to form
$formGfx.DrawImage($newPic, 0, 0)
上面的代码有效,但图像模糊。另一个执行相同操作的代码(通过 winapi)以良好的质量放大图像。
左 - 良好的形象。像素是明确的正方形。对 - 生成我的代码的图像。模糊。:(
我采取的调整大小的方式here.
如何正确调整图像大小以获得正常质量而不会出现模糊。
如果有人知道答案,只能在c#上写,没关系,我想我可以把它翻译成powershell。
【问题讨论】:
标签: c# .net winforms powershell