简体   繁体   English

Windows forms 绘图图像质量

[英]Windows forms drawing image quality

I want take image from screen, magnify it, and draw it to the form.我想从屏幕上获取图像,放大它,然后将其绘制到表格中。

[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)

Code above works, but image is blurry.上面的代码有效,但图像模糊。 Another code, that do the same (via winapi), magnifies the image in good quality.另一个代码,做同样的事情(通过 winapi),以良好的质量放大图像。

形象好。像素是明确的正方形 生成我的代码的图像。模糊

Left - good image.左 - 良好的形象。 pixels are explicit squares.像素是明确的正方形。 Right - image that generate my code.右 - 生成我的代码的图像。 Blurred.:(模糊。:(

The way of resizing i took here.我在这里采取的调整大小的方式。

How properly resize image to get normal quality without blurring.如何正确调整图像大小以获得正常质量而不模糊。

If someone, who knows answer, can write it only on c#, its ok, i think i can translate it to powershell.如果知道答案的人只能在 c# 上写,没关系,我想我可以将它翻译成 powershell。

Play with the InterpolationMode .InterpolationMode The current (HighQualityBicubic) is for shrinking images.当前 (HighQualityBicubic) 用于缩小图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM