Notifications
Clear all

[Closed] Dotnet Resize Icon

Hi

I have a trouble with resizing an icon … 24×24 to 240×240
Image is always shifted several pixels out of space, like in picture.

–copy an icon from any picture editing software and use paste button to get result.


  (
	--Create Interface
	form = dotNetObject "form"
	b1 = dotnetObject "button"
	b2 = dotnetObject "button"
	p1 = dotnetObject  "pictureBox"

	--Functions
	fn netDrRect pos size   = (dotnetObject "Drawing.Rectangle" pos.x pos.y size.x size.y)
	fn netDrColor clr       = ((dotNetClass "Drawing.Color").fromArgb clr.r clr.g clr.b)
	fn resizeImage img =
	(
		local new_img = (dotNetObject "Drawing.Bitmap" 240 240) --Create new bitmap object
		local g  = (dotNetClass "Drawing.Graphics").fromImage new_img --Create new Graphics object
		g.InterpolationMode = (dotnetClass "Drawing.Drawing2D.InterpolationMode").NearestNeighbor
		g.DrawImage img 0 0 240 240
		return new_img
	)
	fn getImageFromClipboard s =
	(
		--get image from clipboard
		local clipboardClass = dotNetClass "System.Windows.Forms.Clipboard" --create a Clipboard dotNetClass
		if not clipboardClass.ContainsImage() 
		then return undefined
		else s.image =  resizeImage (clipboardClass.getImage())
	)
	fn copyImageToClipboard img =
	(
		local clipboardClass = dotNetClass "System.Windows.Forms.Clipboard" --create a Clipboard dotNetClass
		clipboardClass.setImage img --copy the image to clipboard
	)
	fn onOpenForm = (p1.image = dotNetObject "Drawing.Bitmap" 480 480)
	fn onCloseForm = ()
	fn onBtn1MouseUp a s = (copyImageToClipboard p1.image)
	fn onBtn2MouseUp a s = (getImageFromClipboard p1)


	--Inicialize Interface
	form.Name = "Form1"
	form.Text = "Form1"
	form.backcolor = netDrColor yellow
	
	b1.text = "Copy"
	b2.text = "Paste 24x24"
	p1.text = "Image"
	
	form.bounds = netDrRect [200, 200] [400,400]
	b1.bounds = netDrRect [0, 0] [100, 24]
	b2.bounds = netDrRect [0, 24] [100, 24]

	p1.bounds = netDrRect [100, 50] [240, 240]
	p1.backcolor = netDrColor gray

	
	--handlers
	dotNet.addEventHandler form "Shown" onOpenForm
	dotNet.addEventHandler form "Closed" onCloseForm
	
	dotNet.addEventHandler b1 "MouseUp" onBtn1MouseUp
	dotNet.addEventHandler b2 "MouseUp" onBtn2MouseUp
	form.controls.addRange #(b1,  b2,  p1)
	
	form.show()
)

–add missing fn netDrRect and fn netDrColor

5 Replies
 lo1
g.pixelOffsetMode = g.pixelOffsetMode.highQuality
2 Replies
(@merlinel)
Joined: 11 months ago

Posts: 0

This is it! Im searching this all day :hmm: Thank you Lo

(@denist)
Joined: 11 months ago

Posts: 0

cool… i’ve made a fix in one of my tools. thanks!

 lo1

That’s funny, I also once spent a day searching for it

 lo1

Note that when drawing images without resizing them, you will want to set this parameter to pixelOffsetMode.None for higher precision.