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.
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.