473,473 Members | 1,824 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GDI+: Size and position of a scaled image?

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)
Nov 21 '05 #1
2 2086
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.

"Olaf Rabbachin" <Ol*********@IntuiDev.com> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...
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)

Nov 21 '05 #2
Hi,

Bob Powell [MVP] wrote:
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
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul E Collins | last post by:
I'm writing a fairly simple two-player puzzle game. I have a background image in a PNG file (44 KB, 160 x 320 pixels) which I load into an Image object ... imgCachedBackground =...
5
by: anonymous | last post by:
I'm writing a program that deals extensively with the printer. For the most part my application runs fine, but occasionally I run into some Exceptions. The most common exceptions I run into are...
3
by: Just Me | last post by:
Using a Richtextbox I use EM_SETZOOM to set a zoom factor. If I set it to 2 the image on the screen increase but not exactly by 2. I've coded a ruler above the Richtextbox which does scale by...
1
by: Prasad More | last post by:
Hello, I am trying to write a text on Multi-page TIFF image using C# and .NET GDI+. I have written following code to do this. When I execute this code I get "Invalid Parameter User. at...
2
by: Alphonse Giambrone | last post by:
I am currently reading 'Programming The Web with Visual Basic .NET' and have so far found it to be excellent. Downloaded all the code from Apress and working in chapter 4, I get the error shown...
0
by: Supra | last post by:
in ludo game i got 4 images on board. if i clicked first image then i wanted first images be delete or invisible or whatever and place another location and so on. how do i delete first...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: jkristia | last post by:
I'm completely lost right now, and can't figure out how to solve this problem. I want my app and dialogs to show up in the same position and same size as when they were closed. For that I do...
5
by: Roderik | last post by:
Hi, My logo images are positioned some more to the right in IE6 than in FF1.5. I like them to be as shown in FF1.5. Does anyone have a clue, why they are positioned differently in IE? Problem...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.