I'm making a tool that automates cropping and positioning w/out resizing an image into an other image. I found this on microsoft docs for .NET, but I cannot understand how to implement in my code. So far I can download images from the Mojang API, like:
I was wondering if I could crop a rectangle of 8x8 pixel at coordinates of 8,0 and paste it on top of Steve.png at coordinates 8,8 so at the end the output will look like this:
How should I use .NET function .DrawImage
to achieve the crop?
EDIT
Thanks to the link provided by @Caramiriel I can finally crop an area of the image with this script:
Add-Type -AssemblyName System.Drawing
$Username = "Steve"
$destRect = new-object Drawing.Rectangle 8, 0, 8, 8
$srcRect = new-object Drawing.Rectangle 0, 8, 8, 8
$src=[System.Drawing.Image]::FromFile("$pwd\$Username.png")
$bmp=new-object System.Drawing.Bitmap 64,64
$graphics=[System.Drawing.Graphics]::FromImage($bmp)
$units = [System.Drawing.GraphicsUnit]::Pixel
$graphics.DrawImage($src, $destRect, $srcRect, $units)
$graphics.Dispose()
$bmp.Save("$pwd\output.png")
If there's a more compact/elegant way to do it I would really like to know it!
EDIT 2
I posted an answer with a generic function to do the job.