Reputation: 13723
Basically what I am trying to do is make my drawing job easier.
Back in the days of VB6 there was something called Scalewidth and Scaleheight and I could set them to custom values. Ex. 100.
Then when I needed to draw a point at the center of the available space i would just draw it at 50,50.
Is there any way in .Net that I can get similar functionality?
So that no matter what the size of drawing canvas I get, I can draw on it using absolute co-ordinates.
Upvotes: 2
Views: 3954
Reputation: 4703
First, why don't you use Graphics.ScaleTransform instead of handling all the scaling yourself? Something like:
e.Graphics.ScaleTransform(
100.0 / this.ClientSize.Width,
100.0 / this.ClientSize.Height );
Your code will end up much clearer, and I'd bet a beer this would be a little faster.
Second, if you stick with your cnvX/rcnvX functions, make sure to use this.ClientSize.Width (and the same for height) instead of "this.Width".
Upvotes: 1
Reputation: 13723
Schnaader had the right idea... Ultimately I implemented four functions to do this. The functions are below
private float cnvX(double x)
{
return (float)((Width / 100.00) * x);
}
private float rcnvX(double x)
{
return (float)(x / Width) * 100;
}
private float rcnvY(double y)
{
return (float)((y / Height) * 100);
}
private float cnvY(double y)
{
return (float)((Height / 100.00) * y);
}
Upvotes: 0
Reputation: 49719
I don't know if there's a way to achieve this in .NET, but you could easily implement this yourself:
// Unscaled coordinates = x, y; canvas size = w, h;
// Scaled coordinates = sx, sy; Scalewidth, Scaleheight = sw, sh;
x = (sx / sw) * w;
y = (sy / sh) * h;
// Or the other way round
sx = (x / w) * sw;
sy = (y / h) * sh;
Upvotes: 1