Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Images: EnsureMaximumSize

2009-06-27

I have added a new function for resizing with maintained proportions to my image class WsImage.Image. The name is EnsureMaximumSize, and it takes one integer (the maximum width or height) and returns a boolean. The return value is True if the image size changed, otherwise the return value is False.

Imagine that you load an image that is 600 pixels high, and 400 pixels wide. If you call the EnsureMaximumSize function and pass 500 to the parameter, the image will be resized so the new height is 500, and the width is changed so that the width/height ratio is maintained. The return value will be True.

If you would have passed the value 1000 in this situation, the image would not be resized, and the return value would be False.

This code will produce a 800×600 pixel image, and draw both the 800×600 version and the 640×480 version of it. (I wrote this code in the Click event of a Form.)

'In the Click event of a form, create an image.
Dim X As New WsImage.Image(800, 600)

'Create a Graphics object to enable drawing on the image.
Using G As Graphics = X.CreateGraphics()

	'Draw a white background and a black ellipse.
	G.FillRectangle(Brushes.White, 0, 0, 800, 600)
	G.FillEllipse(Brushes.Black, 0, 0, 800, 600)

End Using

'Create a Graphics object to be able to draw on the form.
Using G As Graphics = Me.CreateGraphics()

	'Draw the original bitmap to the form.
	G.DrawImage(X.GetBitmap(), 0, 0)

	'Tint the background.
	Using Tint As New SolidBrush(Color.FromArgb(127, 0, 0, 0))
		G.FillRectangle(Tint, 0, 0, 800, 600)
	End Using

	'Do a 640 x 480 version, and draw it.
	X.EnsureMaximumSize(640)
	G.DrawImage(X.GetBitmap(), 40, 40)
End Using

Check the WsImage tag for more information on this class. You can download it here (if you use it, give me credit).

Categories: Visual Basic 9

Tags: WsImage

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips