T
Thomas Bauer
Hello,
I search a example like that.
(1,0,0,1,4,5 ) -- moving
(1,0,0,1,0,0 ) -- normal matrix
(-1,0,0,1,0,0 ) -- Mirror Y-Axis
(0,-1,0,1,0,0 ) -- Mirror X-Axis
I caluculate all in mm.
After then, I want to make a transformation.
private void MapMillimetersToPixels(Graphics gfx, Rectangle
rectPixels, Rectangle rectMm)
{
Matrix matrix = new Matrix();
ScaleX = (float)rectPixels.Width / Math.Abs(rectMm.Width);
ScaleY = (float)rectPixels.Height /
Math.Abs(rectMm.Height);
matrix.Scale(ScaleX, ScaleY);
matrix.Translate(rectPixels.Left - ScaleX * rectMm.Left,
rectPixels.Top - ScaleY * rectMm.Top);
gfx.Transform = matrix;
}
The zero point.
left
right
top or bottom
All corners are possible, so I need a Matrox multiplication, or?
I prefer a transformation, but I haven't here experience.
So my example is without transformation matrix.
Best regards and thanks for your help
Thomas
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Panel
{
public partial class Form1 : Form
{
public List<Codes> listCodes;
/// <summary>Die Farbe, mit der die Buchstaben
/// neben dem Kreuz gezeichnet werden</summary>
Brush brush;
int anzX = 4;
int anzY = 5;
int abstandX = 50;
int abstandY = 30;
/// <summary>Radius des Kreuzes</summary>
int kRad = 5;
/// <summary>X-Abstand der Buchstaben vom Punkt</summary>
int xPunktAbstand = 10;
/// <summary>Skalierung der gesamten Grafik. 1=nicht
skaliert.</summary>
float gesamtSkalierung = 1;
float fontGrössePunktBeschreibung = 6.0F;
Font punktBeschreibungsFont;
public class Codes
{
private double x;
private double y;
private int boardNumber;
public double X
{
get { return x; }
set { x = value; }
}
public double Y
{
get { return y; }
set { y = value; }
}
public PointF GetPointF()
{
return new PointF((float)x, (float)y);
}
public int BoardNumber
{
get { return boardNumber; }
set { boardNumber = value; }
}
public Codes(double x, double y, int boardNumber)
{
this.x = x;
this.y = y;
this.boardNumber = boardNumber;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ListCodesFüllen(ref listCodes, anzX, anzY, abstandX,
abstandY);
//brush = new SolidBrush(this.ForeColor);
brush = new SolidBrush(Color.Red);
punktBeschreibungsFont = new Font("Arial",
fontGrössePunktBeschreibung);
}
private void ListCodesFüllen(ref List<Codes> listCodes, int
anzX, int anzY, int abstandX, int abstandY)
{
listCodes = new List<Codes>();
Codes codes; int boardNumber = 0;
for (int x = 0; x < anzX; x++)
{
for (int y = 0; y < anzY; y++)
{
boardNumber++;
codes = new Codes(x * abstandX, y * abstandY,
boardNumber);
listCodes.Add(codes);
}
}
}
public void DrawPanel(List<Codes> listCodes, PaintEventArgs e)
{
Graphics g = e.Graphics;
PointF ptCur;
string strPoint;
// momentan keine Auswirkung
g.ScaleTransform(gesamtSkalierung, gesamtSkalierung);
ptCur = new PointF();
foreach (Codes code in listCodes)
{
ptCur = MillimeterToPixelPointF(g, code.GetPointF());
// Zeichnen der zwei Linien, die das Kreuz bilden
g.DrawLine(Pens.Black, ptCur.X - kRad, ptCur.Y,
ptCur.X + kRad, ptCur.Y);
g.DrawLine(Pens.Black, ptCur.X, ptCur.Y - kRad,
ptCur.X, ptCur.Y + kRad);
// Buchstaben und Bezeichnungen für den Punkt zeichnen
strPoint = "P" + code.BoardNumber + "(" + code.X + ","
+ code.Y + ")";
//g.DrawString(strPoint, Font, brush, ptCur.X +
xPunktAbstand, ptCur.Y);
g.DrawString(strPoint, punktBeschreibungsFont, brush,
ptCur.X + xPunktAbstand, ptCur.Y);
}
ZeichneAuswahlRechteck(g, 1, 1, 2, 2);
}
private void ZeichneAuswahlRechteck(Graphics g, int links, int
rechts, int breite, int höhe)
{
Point p1 = Point.Ceiling(MillimeterToPixelPointF(
g, new PointF(links * abstandX, rechts * abstandY)));
Point p2 = Point.Ceiling(MillimeterToPixelPointF(
g, new PointF(breite * abstandX, höhe * abstandY)));
Rectangle r = new Rectangle(p1.X, p1.Y, p2.X, p2.Y);
g.DrawRectangle(Pens.Red, r);
}
float zollAlsMillimeter = 25.4F;
PointF MillimeterToPixelPointF(Graphics g, PointF p)
{
p.X *= g.DpiX / zollAlsMillimeter;
p.Y *= g.DpiY / zollAlsMillimeter;
return p;
}
private void panel_draw_Paint(object sender, PaintEventArgs e)
{
DrawPanel(listCodes, e);
}
}
}
I search a example like that.
(1,0,0,1,4,5 ) -- moving
(1,0,0,1,0,0 ) -- normal matrix
(-1,0,0,1,0,0 ) -- Mirror Y-Axis
(0,-1,0,1,0,0 ) -- Mirror X-Axis
I caluculate all in mm.
After then, I want to make a transformation.
private void MapMillimetersToPixels(Graphics gfx, Rectangle
rectPixels, Rectangle rectMm)
{
Matrix matrix = new Matrix();
ScaleX = (float)rectPixels.Width / Math.Abs(rectMm.Width);
ScaleY = (float)rectPixels.Height /
Math.Abs(rectMm.Height);
matrix.Scale(ScaleX, ScaleY);
matrix.Translate(rectPixels.Left - ScaleX * rectMm.Left,
rectPixels.Top - ScaleY * rectMm.Top);
gfx.Transform = matrix;
}
The zero point.
left
right
top or bottom
All corners are possible, so I need a Matrox multiplication, or?
I prefer a transformation, but I haven't here experience.
So my example is without transformation matrix.
Best regards and thanks for your help
Thomas
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Panel
{
public partial class Form1 : Form
{
public List<Codes> listCodes;
/// <summary>Die Farbe, mit der die Buchstaben
/// neben dem Kreuz gezeichnet werden</summary>
Brush brush;
int anzX = 4;
int anzY = 5;
int abstandX = 50;
int abstandY = 30;
/// <summary>Radius des Kreuzes</summary>
int kRad = 5;
/// <summary>X-Abstand der Buchstaben vom Punkt</summary>
int xPunktAbstand = 10;
/// <summary>Skalierung der gesamten Grafik. 1=nicht
skaliert.</summary>
float gesamtSkalierung = 1;
float fontGrössePunktBeschreibung = 6.0F;
Font punktBeschreibungsFont;
public class Codes
{
private double x;
private double y;
private int boardNumber;
public double X
{
get { return x; }
set { x = value; }
}
public double Y
{
get { return y; }
set { y = value; }
}
public PointF GetPointF()
{
return new PointF((float)x, (float)y);
}
public int BoardNumber
{
get { return boardNumber; }
set { boardNumber = value; }
}
public Codes(double x, double y, int boardNumber)
{
this.x = x;
this.y = y;
this.boardNumber = boardNumber;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ListCodesFüllen(ref listCodes, anzX, anzY, abstandX,
abstandY);
//brush = new SolidBrush(this.ForeColor);
brush = new SolidBrush(Color.Red);
punktBeschreibungsFont = new Font("Arial",
fontGrössePunktBeschreibung);
}
private void ListCodesFüllen(ref List<Codes> listCodes, int
anzX, int anzY, int abstandX, int abstandY)
{
listCodes = new List<Codes>();
Codes codes; int boardNumber = 0;
for (int x = 0; x < anzX; x++)
{
for (int y = 0; y < anzY; y++)
{
boardNumber++;
codes = new Codes(x * abstandX, y * abstandY,
boardNumber);
listCodes.Add(codes);
}
}
}
public void DrawPanel(List<Codes> listCodes, PaintEventArgs e)
{
Graphics g = e.Graphics;
PointF ptCur;
string strPoint;
// momentan keine Auswirkung
g.ScaleTransform(gesamtSkalierung, gesamtSkalierung);
ptCur = new PointF();
foreach (Codes code in listCodes)
{
ptCur = MillimeterToPixelPointF(g, code.GetPointF());
// Zeichnen der zwei Linien, die das Kreuz bilden
g.DrawLine(Pens.Black, ptCur.X - kRad, ptCur.Y,
ptCur.X + kRad, ptCur.Y);
g.DrawLine(Pens.Black, ptCur.X, ptCur.Y - kRad,
ptCur.X, ptCur.Y + kRad);
// Buchstaben und Bezeichnungen für den Punkt zeichnen
strPoint = "P" + code.BoardNumber + "(" + code.X + ","
+ code.Y + ")";
//g.DrawString(strPoint, Font, brush, ptCur.X +
xPunktAbstand, ptCur.Y);
g.DrawString(strPoint, punktBeschreibungsFont, brush,
ptCur.X + xPunktAbstand, ptCur.Y);
}
ZeichneAuswahlRechteck(g, 1, 1, 2, 2);
}
private void ZeichneAuswahlRechteck(Graphics g, int links, int
rechts, int breite, int höhe)
{
Point p1 = Point.Ceiling(MillimeterToPixelPointF(
g, new PointF(links * abstandX, rechts * abstandY)));
Point p2 = Point.Ceiling(MillimeterToPixelPointF(
g, new PointF(breite * abstandX, höhe * abstandY)));
Rectangle r = new Rectangle(p1.X, p1.Y, p2.X, p2.Y);
g.DrawRectangle(Pens.Red, r);
}
float zollAlsMillimeter = 25.4F;
PointF MillimeterToPixelPointF(Graphics g, PointF p)
{
p.X *= g.DpiX / zollAlsMillimeter;
p.Y *= g.DpiY / zollAlsMillimeter;
return p;
}
private void panel_draw_Paint(object sender, PaintEventArgs e)
{
DrawPanel(listCodes, e);
}
}
}