Autor Beitrag
Chiyoko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: Mi 23.06.10 10:47 
Huhu,

Ich wuerde gerne ohne die vorgefertigten Klassen ein Bild mit Aspect ratio auf die Form verkleinern.(Zoom)
ich habe zwar viel im Net gefunden aber daraus werd ich nicht ganz schlau.

Ein Bild an die Form per Strech anpassen ist kein Problem.

In meinem (3D) Spiel habe ich es immer so gemacht, um z.B. das Menue an die Bildschirmaufloesung anzupassen.

ausblenden C#-Quelltext
1:
2:
Bild.pos_x = (screen_size.x - bild_width ) / 2;
Bild.pos_y = (screen_size.y - bild_height ) / 2;


Das kann ich natuerlich schlecht anwenden^^

Hat vielleicht jemand einen Rat fuer mich?
Danke.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mi 23.06.10 11:55 
Du mußt dir das Minimum der beiden Faktoren heraussuchen und dann je Dimension zentrieren:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
double fx = (double)x / X;
double fy = (double)y / y;
double f = Math.Min(fx, fy);

pos_x = (X - x) * f;
pos_y = (Y - y) * f;

d.h. analog zu deiner bisherigen Zentrier-Formel (nur daß statt /2 der Faktor hier dann 0.5 wäre).
Chiyoko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: Mi 23.06.10 12:47 
Danke, das hilft mir denk ich ganz gut weiter.
Ich versuch mich mal dran:)
Chiyoko Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 298
Erhaltene Danke: 8

Win 98, Win Xp, Win 10
C# / C (VS 2019)
BeitragVerfasst: Di 29.06.10 12:30 
Der Code abschnitt ist zwar nicht von mir, tut aber genau das, was ich brauche.
Vielleicht bekomm ich es ja noch besser hin.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
using System.Drawing;

namespace Uhr_app
{
    public partial class Form1
    {
        // http://www.mycsharp.de/wbb2/thread.php?threadid=79676
        private Point Center( Size size )
        {
            int x = this.ClientRectangle.Width / 2 - size.Width / 2;
            int y = this.ClientRectangle.Height / 2 - size.Height / 2;
            return new Point ( x , y );
        }
        private Size AspectRatio()
        {
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            int nSourceWidth = this.BackgroundImage.Width;
            int nSourceHeight = this.BackgroundImage.Height;

            nPercentW = ( ( float ) Width / ( float ) nSourceWidth );
            nPercentH = ( ( float ) Height / ( float ) nSourceHeight );
            int nDestinationX = 0;
            int nDestinationY = 0;
            if ( nPercentH < nPercentW )
            {
                nPercent = nPercentH;
                nDestinationX = System.Convert.ToInt16 ( ( Width -
                              ( nSourceWidth * nPercent ) ) / 2 );
            }
            else
            {
                nPercent = nPercentW;
                nDestinationY = System.Convert.ToInt16 ( ( Height -
                              ( nSourceHeight * nPercent ) ) / 2 );
            }

            int nDestinationWidth = ( int ) ( nSourceWidth * nPercent );
            int nDestinationHeight = ( int ) ( nSourceHeight * nPercent );
            return new Size ( nDestinationWidth , nDestinationHeight );
        }
    }
}


Sobald dann das Bild gewaehlt wurde, setzt man die Werte und zeichnet das Bild neu.

ausblenden C#-Quelltext
1:
2:
3:
Size sizeAspectRatio = AspectRatio ();
Point ptCenterZoom = Center ( sizeAspectRatio );
g.DrawImage ( this.BackgroundImage , ptCenterZoom.X , ptCenterZoom.Y , sizeAspectRatio.Width , sizeAspectRatio.Height );