Updating a value on another Class - c#

I have this code that draws a rectangle using the ShapeClass.cs. But I want to update the Rectangle parameters using a button event in the mainform. How do I do that? The PaintRectangle class only gets the initial value on the textbox from the main form. ......................................................................................................................................................................
MAin Class
#region //Passing of values
public string ShapeWidth() { return textBox_Shape_Width.Text;}
public string ShapeHeight() { return textBox_Shape_Height.Text; }
#endregion
private void btn_Draw_Click(object sender, EventArgs e)
{
ShapeClass s = new ShapeClass();
var value = s.diffVar[0];
MessageBox.Show(value.ToString());
pictureBox_Canvas.Paint += paintRect;
pictureBox_Canvas.Refresh();
}
#region //Methods for Hide and Show
public void HideButtons()
{
btn_Rectangle.Visible = false;
btn_Square.Visible = false;
btn_Ellipse.Visible = false;
btn_Circle.Visible = false;
btn_Triangle.Visible = false;
}
public void ShowButtons()
{
btn_Rectangle.Visible = true;
btn_Square.Visible = true;
btn_Ellipse.Visible = true;
btn_Circle.Visible = true;
btn_Triangle.Visible = true;
}
public void HideSettings()
{
btn_Draw.Visible = false;
btn_Accept.Visible = false;
btn_Cancel.Visible = false;
btn_Reset.Visible = false;
btn_Accept.Visible = false;
trackBar_Size.Visible = false;
trackBar_Stroke.Visible = false;
trackBar_Corner.Visible = false;
label_Corner.Visible = false;
label_Height.Visible = false;
label_Size.Visible = false;
label_Stroke.Visible = false;
rb_Both.Visible = false;
rb_Height.Visible = false;
rb_Width.Visible = false;
textBox_Shape_Height.Visible =false;
textBox_Shape_Width.Visible = false;
lbl_Width.Visible = false;
}
public void ShowSettings()
{
btn_Draw.Visible = true;
btn_Accept.Visible = true;
btn_Cancel.Visible = true;
btn_Reset.Visible = true;
btn_Accept.Visible = true;
trackBar_Size.Visible = true;
trackBar_Stroke.Visible = true;
trackBar_Corner.Visible = true;
label_Corner.Visible = true;
label_Height.Visible = true;
label_Size.Visible = true;
label_Stroke.Visible = true;
rb_Both.Visible = true;
rb_Height.Visible = true;
rb_Width.Visible = true;
textBox_Shape_Height.Visible = true;
textBox_Shape_Width.Visible = true;
lbl_Width.Visible = true;
}
#endregion
#region //Rectangle Creation and Scaling
private void btn_Rectangle_Click(object sender, EventArgs e)
{
HideButtons();
ShowSettings();
}
public void paintRect(object sender, PaintEventArgs e)
{
ShapeClass s = new ShapeClass();
s.paintRectangle(e);
}
#endregion
#region //Show and Hide Grid (Checkbox Event)
//Drawing the Grid
private void checkBox_Grid_CheckedChanged(object sender, EventArgs e)
{
if (checkBox_Grid.Checked == true)
{
DrawGrid();
}
else if (!checkBox_Grid.Checked == true)
{
pictureBox_Canvas.Refresh();
HideGrid();
}
else
return;
}
private void DrawGrid()
{
Graphics grid = Graphics.FromImage(bm);
int numOfCells = 301;
int cellSize = 5;
Pen p = new Pen(Color.LightSlateGray);
p.Width = 0.5F;
for (int y = 0; y < numOfCells; ++y)
{
grid.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
}
for (int x = 0; x < numOfCells; ++x)
{
grid.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
}
pictureBox_Canvas.Image = bm;
}
//Hides the Grid
private void HideGrid()
{
Graphics grid = Graphics.FromImage(bm);
Pen p = new Pen(Color.White);
p.Width = 0.5F;
int numOfCells = 301;
int cellSize = 5;
for (int y = 0; y < numOfCells; ++y)
{
grid.DrawLine(p, 0, y * cellSize, numOfCells * cellSize, y * cellSize);
}
for (int x = 0; x < numOfCells; ++x)
{
grid.DrawLine(p, x * cellSize, 0, x * cellSize, numOfCells * cellSize);
}
pictureBox_Canvas.Image = bm;
}
#endregion
#region //Square Creation and Scaling
private void btn_Square_Click(object sender, EventArgs e)
{
HideButtons();
ShowSettings();
}
#endregion
#region //Circle Creation and Scaling
private void btn_Circle_Click(object sender, EventArgs e)
{
HideButtons();
ShowSettings();
}
#endregion
#region //Ellipse Creation and Scaling
private void btn_Ellipse_Click(object sender, EventArgs e)
{
HideButtons();
ShowSettings();
}
#endregion
#region //Triangle Creation and Scaling
private void btn_Triangle_Click(object sender, EventArgs e)
{
HideButtons();
ShowSettings();
}
#endregion
#region //Accept Button
private void btn_Accept_Click(object sender, EventArgs e)
{
HideSettings();
ShowButtons();
}
#endregion
#region //Reset Button
private void btn_Reset_Click(object sender, EventArgs e)
{
HideSettings();
ShowButtons();
}
#endregion
#region //Cancel Button
private void btn_Cancel_Click(object sender, EventArgs e)
{
HideSettings();
ShowButtons();
}
#endregion
#region //VALIDATIONS
private void textBox_Shape_Width_TextChanged(object sender, EventArgs e)
{
if(Convert.ToInt32(textBox_Shape_Width.Text)>128)
{
MessageBox.Show("Width cannot exceed 128.");
textBox_Shape_Width.Text = 128.ToString();
}
}
private void textBox_Shape_Height_TextChanged(object sender, EventArgs e)
{
if (Convert.ToInt32(textBox_Shape_Height.Text) > 128)
{
MessageBox.Show("Height cannot exceed 128");
textBox_Shape_Height.Text = 128.ToString();
}
}
#endregion
private void trackBar_Size_ValueChanged(object sender, EventArgs e)
{
}
}
}
Shape Class
namespace SealCreator2._0
{
public class ShapeClass :Form1
{
Form1 f1 = new Form1();
public double w1,h1, Rect_Width;
public List<object> diffVar = new List<object>();
public void paintRectangle(PaintEventArgs e)
{
int x, y;
w1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeWidth()) * 96) / 25.4);
h1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeHeight()) * 96) / 25.4);
x = ((484 / 2) - (Convert.ToInt32(w1) / 2)); // Positions the Shape in the center of the PictureBox
y = ((484 / 2) - (Convert.ToInt32(h1) / 2));// Positions the Shape in the center of the PictureBox
//Draws a Rectangle
Pen red = new Pen(Color.Red, 3);
Rectangle rect = new Rectangle(x, y, Convert.ToInt32(w1), Convert.ToInt32(h1));
e.Graphics.DrawRectangle(red, rect);
diffVar.Add(w1);
diffVar.Add(h1);
}
}
}

You can create other class which contain both PaintEventArgs and your params. Ex:
public class DrawArgs
{
// parameter which you can modify base on your logic
public int width;
public int height;
public int x;
public int y;
//your paint event
public PaintEventArgs e;
}
And modify your ShapeClass.paintRectangle() method:
public class ShapeClass
{
public void paintRectangle(DrawArgs drawArgs)
{
//w1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeWidth()) * 96) / 25.4);
//h1 = Convert.ToDouble((Convert.ToInt32(f1.ShapeHeight()) * 96) / 25.4);
//x = ((484 / 2) - (Convert.ToInt32(w1) / 2));
//y = ((484 / 2) - (Convert.ToInt32(h1) / 2));
// get your params
var x = drawArgs.x;
var y = drawArgs.y;
var w1 = drawArgs.width;
var h1 = drawArgs.height;
Pen red = new Pen(Color.Red, 3);
Rectangle rect = new Rectangle(x, y, Convert.ToInt32(w1), Convert.ToInt32(h1));
drawArgs.e.Graphics.DrawRectangle(red, rect);
}
}
And you can call it from paintRect method():
public void paintRect(object sender, PaintEventArgs e)
{
ShapeClass s = new ShapeClass();
var drawArg = new DrawArgs
{
e = e,
// and add your params here
x = something,
y = something,
///.....
};
s.paintRectangle(drawArg);
}
P/S: it is just an example. You can modify the DrawArgs class base on your logic

Add a anothe function to change the parameters.
For example:
void UpdateParameters()
{
ChangeParameters();
}
Give you another suggestion: You can edit this question to add Codes.

Related

How to Zoom In and Out out of the bitmap

I am a novice in programming and i need Your advice, please.
I was tasked with writing a programm that will be counting number of items needed according to length of a track. This track we need to draw by ourselves, but I don't want to make this application full screen, so I need a Zooming function and for a life of me I can't get how should i do it.
Please help!
And here's the code.
namespace RFIDGovno
{
public partial class Form1 : Form
{
List<Line> lines = new List<Line>();
List<Dot> dots = new List<Dot>();
bool isVert;
Point sPlace;
Point ePlace;
bool downed=false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Point temp = e.Location;
if (downed)
{
if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
{
temp.X = sPlace.X;
}
else
{
temp.Y = sPlace.Y;
}
downed = false;
ePlace = temp;
dots.Add(new Dot(ePlace));
lines.Add(new Line(sPlace,ePlace,isVert, isVert ? Math.Abs(sPlace.Y-ePlace.Y) : Math.Abs(sPlace.X - ePlace.X)));
}else
{
if (!dots.Any<Dot>())
{
downed = true;
sPlace = e.Location;
dots.Add(new Dot(sPlace));
}
else if(dots.Where(t => t.fuse).Any<Dot>())
{
downed = true;
sPlace = dots.Where(t => t.fuse).First().place;
}
}
drawing(null);
}
private void Zooming()
{
}
private void drawing(Point? mouse)
{
Graphics g = pictureBox1.CreateGraphics();
Bitmap im = new Bitmap(pictureBox1.Width-1, pictureBox1.Height-1);
Graphics q = Graphics.FromImage(im);
q.Clear(Color.White);
lines.ForEach(line =>
{
q.DrawLine(new Pen(Brushes.Black, 2), line.pStart, line.pEnd);
q.DrawString(line.length.ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, line.isVert ? line.pStart.X : line.pStart.X + ((line.pEnd.X - line.pStart.X) / 2) - 5, line.isVert ? line.pStart.Y + ((line.pEnd.Y - line.pStart.Y) / 2) - 5 : line.pStart.Y);
});
if (downed)
{
q.DrawLine(new Pen(Brushes.Black, 2), sPlace, mouse ?? sPlace);
if ((isVert)&&(mouse.HasValue))
{
q.DrawString(Math.Abs(sPlace.Y-mouse.Value.Y).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
}
else if ((isVert==false) && (mouse.HasValue))
{
q.DrawString(Math.Abs(sPlace.X - mouse.Value.X).ToString(), new Font("Times New Roman", 12, FontStyle.Bold), Brushes.Black, sPlace);
}
}
dots.ForEach(dot =>
{
q.DrawEllipse(new Pen(Brushes.Black, 2),dot.place.X-1, dot.place.Y - 1, 2, 2);
if (dot.fuse)
{
q.DrawEllipse(new Pen(Brushes.Red, 2), dot.place.X - 2, dot.place.Y - 2, 4, 4);
}
});
g.DrawImage(im, 1, 1);
q.Dispose();
im.Dispose();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
Point temp = e.Location;
if (downed)
{
if (!(Math.Abs(e.X - sPlace.X) > Math.Abs(e.Y - sPlace.Y)))
{
temp.X = sPlace.X;
isVert = true;
}
else
{
temp.Y = sPlace.Y;
isVert = false;
}
}
else
{
dots.ForEach(t => t.fuse = false);
List<Dot> tDots = dots.Where(dt => Math.Abs(e.Location.X - dt.place.X) < 20 && Math.Abs(e.Location.Y - dt.place.Y) < 20).ToList();
if (tDots.Any<Dot>())
{
double min = 0;
min = tDots.Min(t => distance(t.place, e.Location));
tDots.Where(t => distance(t.place, e.Location) == min).First().fuse = true;
}
}
drawing(temp);
}
private double distance(Point p1, Point p2)
{
double dist=0;
dist = (Math.Sqrt(Math.Abs(p1.X-p2.X)+Math.Abs(p1.Y-p2.Y)));
return dist;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
int between = 0;
string curr = comboBox1.SelectedItem.ToString();
if (curr == "Short")
{
between = 1250;
}
else if (curr == "Medium")
{
between = 1875;
}
else
{
between = 3750;
}
double wut = (double.Parse(textBox1.Text) / between * 300 + double.Parse(textBox3.Text) + double.Parse(textBox2.Text) * 2);
string answer = Convert.ToString(Math.Ceiling(wut));
label4.Text = answer;
}
}
public class Line
{
public Point pEnd;
public Point pStart;
public bool isVert;
public int length;
public Line(Point pStart, Point pEnd, bool isVert,int length)
{
this.pStart = pStart;
this.pEnd = pEnd;
this.isVert = isVert;
this.length = length;
}
}
public class Dot
{
public bool fuse;
public Point place;
public Dot(Point place)
{
this.place = place;
}
}
}

How would I change my program so that the shape is a triangle?

Below is my code, I have already assigned the second and third lines of my triangle a value but am struggling to integrate them with them within the rest of the code. Could I have some advice on where I would implement the other lines.
public partial class Form1 : Form
{
Graphics graphics;
int ax = 150;
int ay = 100;
int bx = 25;
int by = 50;
int cx = 10;
int cy = 90;
int dx = 3;
int dy = 2;
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(triangle);
this.DoubleBuffered = true;
}
private void triangle(object sender, PaintEventArgs e)
{
graphics = e.Graphics;
SolidBrush brush = new SolidBrush(Color.Black);
graphics.FillEllipse(brush, ax, ay, 10, 10);
}
private void movetriangle()
{
int newposition_x = ax + dx;
int newposition_y = ay + dy;
if (newposition_x < -5 || newposition_x > this.ClientSize.Width) dx = -dx;
if (newposition_y < 0 || newposition_y > this.ClientSize.Height) dy = -dy;
ax += dx;
ay += dy;
Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick_1(object sender, EventArgs e)
{
movetriangle();
}
}

How to limit dragging an image inside a Panel?

Good evening. I'm currently working on an interactive map editor. I'm trying to keep the map image from being dragged out of view by using this code
if (currentPosition.X > 0)
currentPosition.X = 0;
else if (currentPosition.X < -mapSize.Width + Size.Width )
currentPosition.X = -mapSize.Width + Size.Width ;
if (currentPosition.Y > 0)
currentPosition.Y = 0;
else if (currentPosition.Y < -mapSize.Height + Size.Height)
currentPosition.Y = -mapSize.Height + Size.Height;
However I can't drag it to the limits Size-mapSize.
The currentPosition corresponds to the upper left location of the image. Here's the full code of the class
namespace GameMaps
{
public class MapPanel : Panel
{
private Bitmap shownMap = null;
private Point mapLocation;
private Size mapSize;
private bool dragging;
private Point currentPosition;
//private Point newPosition;
private Point initialPosition;
private Point initialMousePosition;
private Form1 parentForm;
public MapPanel(Form1 parentForm) : base()
{
this.DoubleBuffered = true;
this.parentForm = parentForm;
}
public MapPanel(Panel targetPanel) : this(new Form1())
{
}
public void ShowMap(Map map)
{
if (map == null) return;
shownMap = map.MapTexture;
mapLocation = new Point(0, 0);
mapSize = shownMap.Size;
currentPosition = new Point();
initialPosition = new Point();
initialMousePosition = new Point(); //mapLocation;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Pixel;
e.Graphics.PageScale = 1.0F;
// base.OnPaint(e);
if (shownMap == null)
{
base.OnPaint(e);
return;
}
e.Graphics.DrawImage(shownMap, currentPosition.X, currentPosition.Y);
}
protected override void OnMouseDown(MouseEventArgs e)
{
// base.OnMouseDown(e);
if (shownMap == null)
{
base.OnMouseDown(e);
return;
}
if (dragging)
{
dragging = false;
return;
}
else dragging = true;
initialPosition.X = currentPosition.X;
initialPosition.Y = currentPosition.Y;
initialMousePosition = e.Location;
}
protected override void OnMouseMove(MouseEventArgs e)
{
/*parentForm.label7.Text = currentPosition.X.ToString();
parentForm.label8.Text = currentPosition.Y.ToString();
parentForm.label9.Text = mapSize.Width.ToString();
parentForm.label10.Text = mapSize.Height.ToString();
parentForm.label11.Text = Size.Width.ToString();
parentForm.label12.Text = Size.Width.ToString();*/
//base.OnMouseMove(e);
if (!dragging) return;
currentPosition.X = e.X - initialMousePosition.X + initialPosition.X;
currentPosition.Y = e.Y - initialMousePosition.Y + initialPosition.Y;
if (currentPosition.X > 0)
currentPosition.X = 0;
else if (currentPosition.X < -mapSize.Width + Size.Width )
currentPosition.X = -mapSize.Width + Size.Width ;
if (currentPosition.Y > 0)
currentPosition.Y = 0;
else if (currentPosition.Y < -mapSize.Height + Size.Height)
currentPosition.Y = -mapSize.Height + Size.Height;
//if (currentPosition.X + mapSize.Width < this.Size.Width)
// currentPosition.X = this.Size.Width - mapSize.Width;
Invalidate();
// (e.X - initialPosition.X + xinit, e.Y - init_loc.Y + yinit);
}
protected override void OnMouseLeave(EventArgs e)
{
// base.OnMouseLeave(e);
dragging = false;
// F*! if I hold the mouse down this does not work
}
protected override void OnMouseUp(MouseEventArgs e)
{
//base.OnMouseUp(e);
dragging = false;
}
}
}
Well, it seems there was no bad logic except for the fact that I was using the Panel's Size instead of its DisplayRectangle (Size.Width == DisplayRectangle.Width-2). The whole problem was that the images I was using had been exported from Photoshop and had a resolution of 72dpi instead of 96dpi: Same Size, but on using DrawImage they were scaled up. Here's my solution. There are still this to correct but this is the solution regarding my original question. Also some variable names were changed.
namespace GameMaps
{
public class MapPanel : Panel
{
private Map sourceMap;
private Bitmap mapTexture;
private Size mapSize;
private Point mapPosition;
private float dpiX, dpiY;
private Point initialPosition;
private Point initialMousePosition;
private bool dragging;
/* Constructors */
public MapPanel() : base()
{
this.DoubleBuffered = true;
this.sourceMap = null;
this.dpiX = 96.0F; this.dpiY = 96.0F; // For now, this Application won't be DPI aware
}
public MapPanel(Panel targetPanel) : this()
{
// TO DO
}
/* Show a Map */
public void ShowMap(Map map)
{
if (map == null)
return;
sourceMap = map;
mapTexture = new Bitmap(map.MapTexture);
mapTexture.SetResolution(dpiX, dpiY);
mapSize = mapTexture.Size; // ?
mapPosition = new Point(0, 0);
}
protected override void OnPaint(PaintEventArgs e)
{
// base.OnPaint(e);
e.Graphics.PageUnit = GraphicsUnit.Pixel; // ?
e.Graphics.PageScale = 1.0F; // ?
if (sourceMap == null)
{
base.OnPaint(e);
return;
}
e.Graphics.DrawImage(mapTexture, mapPosition.X, mapPosition.Y);
}
protected override void OnMouseDown(MouseEventArgs e)
{
// base.OnMouseDown(e);
if (sourceMap == null)
{
base.OnMouseDown(e);
return;
}
if (dragging)
{
dragging = false;
return;
}
else dragging = true;
initialPosition.X = mapPosition.X;
initialPosition.Y = mapPosition.Y;
initialMousePosition.X = e.Location.X;
initialMousePosition.Y = e.Location.Y;
}
protected override void OnMouseMove(MouseEventArgs e)
{
//base.OnMouseMove(e);
if (!dragging) return;
mapPosition.X = e.X - initialMousePosition.X + initialPosition.X;
mapPosition.Y = e.Y - initialMousePosition.Y + initialPosition.Y;
if (mapPosition.X > 0)
mapPosition.X = 0;
else if (mapPosition.X < DisplayRectangle.Width - mapSize.Width)
mapPosition.X = DisplayRectangle.Width - mapSize.Width;
if (mapPosition.Y > 0)
mapPosition.Y = 0;
else if (mapPosition.Y < DisplayRectangle.Height - mapSize.Height)
mapPosition.Y = DisplayRectangle.Height - mapSize.Height;
Invalidate(); // Force Repaint
}
protected override void OnMouseLeave(EventArgs e)
{
// base.OnMouseLeave(e);
dragging = false;
// TO DO: Correct this
}
protected override void OnMouseUp(MouseEventArgs e)
{
//base.OnMouseUp(e);
dragging = false;
}
}
}

How to add multiple canvases to a picturebox?

Let me explain my problem:
I have a picturebox and this should be used to draw several elements of a listbox.
I gave the picturebox a canvas. by left and rightclick u can put up a rectangle which should preview the element u want to draw on the canvas of the picturebox.
Now this is my problem:
this rectangle should just be a preview and should be erased every time i click on a another position of the picturebox and put up a new rectangle. it should just be drawn on the canvas when i click on the inputbutton.
So how can i preview the chosen elements in the rectangle made by left and rightclick?
I hope my question is clealy enough : /
This is what i did so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Interaktive_systeme_sta
{
public partial class Bildeditor : Form
{
void drawRect()
{
pbxBild.Refresh();
using (Graphics g = this.pbxBild.CreateGraphics())
{
Pen blackPen = new Pen(Color.Black, 1);
blackPen.DashPattern = new float[] {2,2};
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawRectangle(blackPen, X, Y, B, H);
g.Dispose();
}
}
public Bildeditor()
{
InitializeComponent();
this.MinimumSize = new Size(630,420);
this.MaximumSize = new Size(630, 420);
int canvasWidth = pbxBild.Width;
int canvasHeight = pbxBild.Height;
nupBreite.Maximum = canvasWidth;
nupHoehe.Maximum = canvasHeight;
nupBreite.Minimum = 0;
nupHoehe.Minimum = 0;
nupBreite.Value = canvasWidth;
nupHoehe.Value = canvasHeight;
nupX.Maximum = canvasWidth;
nupY.Maximum = canvasHeight;
nupX.Minimum = 0;
nupY.Minimum = 0;
nupX.Value = 0;
nupY.Value = 0;
drawRect();
}
private void btnZuruecksetzten_Click(object sender, EventArgs e)
{
int canvasWidth = pbxBild.Width;
int canvasHeight = pbxBild.Height;
nupX.Value = 0;
nupY.Value = 0;
nupBreite.Value = canvasWidth-1;
nupHoehe.Value = canvasHeight-1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void pbxBild_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
switch (e.Button)
{
case MouseButtons.Left:
if (e.X < 0)
{
nupX.Value = 0;
}
else
{
nupX.Value = e.X;
}
if (e.Y < 0)
{
nupY.Value = 0;
}
else
{
nupY.Value = e.Y;
}
break;
case MouseButtons.Right:
if (e.X - nupX.Value < 1)
{
nupBreite.Value = 1;
}
else
{
nupBreite.Value = e.X - nupX.Value;
}
if (e.Y - nupY.Value < 1)
{
nupHoehe.Value = 1;
}
else
{
nupHoehe.Value = e.Y - nupY.Value;
}
break;
}
drawRect();
}
private void nupY_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void nupBreite_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void nupHoehe_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void btnEinfuegen_Click(object sender, EventArgs e)
{
if (lbBildelemente.SelectedIndex == 0)
{
zeichneBild();
}
else if (lbBildelemente.SelectedIndex == 1)
{
zeichneLine();
}
else if (lbBildelemente.SelectedIndex == 2)
{
zeichneRect();
}
else if (lbBildelemente.SelectedIndex == 3)
{
zeichneText();
}
else if (lbBildelemente.SelectedIndex == 4)
{
zeichneKreis();
}
}
private void lbBildelemente_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbBildelemente.SelectedIndex == 0)
{
tbFarbe.Enabled = false;
btnFarbe.Enabled = false;
nupPen.Enabled = false;
tbBild.Enabled = true;
btnBild.Enabled = true;
tbText.Enabled = false;
tbSchrift.Enabled = false;
btnSchrift.Enabled = false;
}
else if (lbBildelemente.SelectedIndex == 1 || lbBildelemente.SelectedIndex == 2 || lbBildelemente.SelectedIndex == 4)
{
tbFarbe.Enabled = true;
btnFarbe.Enabled = true;
nupPen.Enabled = true;
tbBild.Enabled = false;
btnBild.Enabled = false;
tbText.Enabled = false;
tbSchrift.Enabled = false;
btnSchrift.Enabled = false;
}
else if (lbBildelemente.SelectedIndex == 3)
{
tbFarbe.Enabled = true;
btnFarbe.Enabled = true;
nupPen.Enabled = true;
tbBild.Enabled = false;
btnBild.Enabled = false;
tbText.Enabled = true;
tbSchrift.Enabled = true;
btnSchrift.Enabled = true;
}
}
void zeichneBild()
{
Graphics g = pbxBild.CreateGraphics();
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
Bitmap bitmap = new Bitmap(tbBild.Text);
g.DrawImage(bitmap, X, Y, B, H);
}
// Als zusatzfunktion kann man die Pinseldicke auswählen
void zeichneRect()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawRectangle(Pen, X, Y, B, H);
}
//zusatzfunktion
void zeichneKreis()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawEllipse(Pen, X, Y, B, H);
}
void zeichneLine()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawLine(Pen, X, Y, X+B, Y+H);
}
void zeichneText()
{
Graphics g = pbxBild.CreateGraphics();
Brush brush = new SolidBrush(tbFarbe.BackColor);
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
g.DrawString(tbText.Text, fontDialog1.Font, brush, X, Y);
}
private void btnFarbe_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
tbFarbe.BackColor = colorDialog1.Color;
}
}
private void btnSchrift_Click(object sender, EventArgs e)
{
// Show the dialog.
DialogResult result = fontDialog1.ShowDialog();
// See if OK was pressed.
if (result == DialogResult.OK)
{
// Get Font.
Font font = fontDialog1.Font;
// Set TextBox properties.
this.tbSchrift.Text = string.Format(font.Name +"; "+ font.Size +"pt");
}
}
private void btnBild_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
+ "EMF Enhanced Metafile Format (*.emf)|*.emf|"
+ "Graphics Interchange Format (*.gif)|*.gif|"
+ "Bitmap (*.bmp)|*.bmp|"
+ "W3C Portable Network Graphics (*.png)|*.png";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
tbBild.Text = openFileDialog.FileName;
}
}
private void bildbereichLöschenToolStripMenuItem_Click(object sender, EventArgs e)
{
pbxBild.Refresh();
}
private void bildLadenToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
+ "EMF Enhanced Metafile Format (*.emf)|*.emf|"
+ "Graphics Interchange Format (*.gif)|*.gif|"
+ "Bitmap (*.bmp)|*.bmp|"
+ "W3C Portable Network Graphics (*.png)|*.png";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
Graphics g = pbxBild.CreateGraphics();
int X = 0;
int Y = 0;
int B = Convert.ToInt32(pbxBild.Width);
int H = Convert.ToInt32(pbxBild.Height);
Bitmap bitmap = new Bitmap(openFileDialog.FileName);
g.DrawImage(bitmap, X, Y, B, H);
}
}
}
}
I think this is what you need
private void Form1_Load(object sender, EventArgs e)
{
listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == "Circle")
{
pictureBox1.Image = circle;
}
}

I have two pictureBoxes in other sizes how can i make that when i click on one pictureBox it will draw a point on the smaller pictureBox?

This is my code but its not working not drawing points at all on pictureBox2.
public partial class Form1 : Form
{
private int counter;
private int pb1mouse_x;
private int pb1mouse_y;
private int pbsize_x;
private int pbsize_y;
public Form1()
{
InitializeComponent();
pbsize_x = pictureBox2.Width / pictureBox1.Width;
pbsize_y = pictureBox2.Height / pictureBox1.Height;
label4.Visible = false;
label5.Visible = false;
label6.Visible = false;
counter = 0;
pictureBox1.Load(#"d:\radar000075.png");
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label4.Visible = true;
label4.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
label5.Visible = true;
label5.Text = String.Format("X: {0}; Y: {1}", e.X, e.Y);
counter += 1;
label6.Visible = true;
label6.Text = counter.ToString();
pb1mouse_x = e.X;
pb1mouse_y = e.Y;
pb1mouse_x = pb1mouse_x * pbsize_x;
pb1mouse_y = pb1mouse_y * pbsize_y;
pictureBox2.Invalidate();
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Red);
var g = e.Graphics;
g.Clear(pictureBox1.BackColor);
g.DrawEllipse(p, new Rectangle(pb1mouse_x, pb1mouse_y, 10, 10));
}
}
You should be able to simply multiply by factors, i.e. smallBox.Width/largeBox.Width and smallBox.Height/largeBox.Height. Multiply the coordinates for the larger box by those factors and it will give you coordinates for the smaller box.
Edit:
This is what my code looks like:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace PBoxes
{
public partial class Form1 : Form
{
private float xFactor, yFactor;
List<PointF> points = new List<PointF>();
public Form1()
{
InitializeComponent();
xFactor = (float)pictureBox2.Width / pictureBox1.Width;
yFactor = (float)pictureBox2.Height / pictureBox1.Height;
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
points.Add(new PointF(e.X * xFactor, e.Y * yFactor));
pictureBox2.Invalidate();
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
foreach (PointF pt in points)
{
e.Graphics.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
}
}
private void pictureBox_SizeChanged(object sender, EventArgs e)
{
xFactor = (float)pictureBox2.Width / pictureBox1.Width;
yFactor = (float)pictureBox2.Height / pictureBox1.Height;
}
}
}

Categories