GDI+: Size and position of a scaled image?

O

Olaf Rabbachin

Hi folks,

using Transform (see i.e. www.bobpowell.net/bestfit.htm) I'm drawing a
scaled version of an image onto a panel (code s.b.).

Any pointers as to how to get the size and position after the transform?

Using something like this (with the code below) ...
rectScaled = New Rectangle( _
CType(sngPosX, Integer), _
CType(sngPosY, Integer), _
ctlContainer.Width - CType(sngPosX * 2, Integer), _
ctlContainer.Height - CType(sngPosY * 2, Integer))
.... will work for images being wider than high but not vice versa.

I just don't get it. :-(

TIA,
Olaf

--

Code:
--- 8< ---
Dim dblLargestRatio As Double = _
Math.Max( _
CType(img.Width, Double) / ctlContainer.Width, _
CType(img.Height, Double) / ctlContainer.Height)

Dim sngPosX As Single = _
CType((ctlContainer.Width * dblLargestRatio / 2) - _
(img.Width / 2), Single)

Dim sngPosY As Single = _
CType((ctlContainer.Height * dblLargestRatio / 2) - _
(img.Height / 2), Single)

dim m as New Matrix(1 / CType(dblLargestRatio, Single), 0, 0, _
1 / CType(dblLargestRatio, Single), 0, 0)

m.Translate(sngPosX, sngPosY)

e.Graphics.Transform = m
e.Graphics.DrawImageUnscaled(img, 0, 0)
 
B

Bob Powell [MVP]

You can use the tranform to modify a pair of points that correspond to the
top-left and bottom-right of the image..

dim pa() as new Point(){new Point(0,0), new Point(img.Width,img.Height)}
mx.TransformPoints(pa)
dim rect as Rectangle=new
Rectangle(pa(0).X,pa(0).Y,pa(1).X-pa(0).X,pa(1).Y-pa(0).Y)

The rectangle should now be that same as that of the displayed image.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
O

Olaf Rabbachin

Hi,
You can use the tranform to modify a pair of points that correspond to the
top-left and bottom-right of the image..

dim pa() as new Point(){new Point(0,0), new Point(img.Width,img.Height)}

typo - that should read
dim pa as new Point(){new Point(0,0), new Point(img.Width,img.Height)}
mx.TransformPoints(pa)
dim rect as Rectangle=new
Rectangle(pa(0).X,pa(0).Y,pa(1).X-pa(0).X,pa(1).Y-pa(0).Y)

The rectangle should now be that same as that of the displayed image.

works like a charm - thanks bunches!

Do you have any suggestion as to articles on the web regarding
transformation? The OH isn't a big help there and I didn't yet find
anything on the web. A good book about GDI+ would also be something I'd
like investing into.

Cheers,
Olaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top