How to Zoom In and Out out of the bitmap - c#

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;
}
}
}

Related

Printing out Panel in C#

Again, I am asking a question about a USB Printer. This time however, the issue is more on the programming end of the spectrum.
I have a windows forms application in which I generate a receipt, which is then supposed to be sent to the printer. The Following things work: Generating the receipt, Sending the data to the printer.
The data however is only an empty strip of paper. I have the feeling that the Software is ignoring the Panel contents and just sends the empty panel to the printer.
The Sourcecode is as follows:
namespace MakeyMakey
{
public partial class Form1 : Form
{
public void Gridsettings()
{
GVReceipt.ScrollBars = ScrollBars.None;
GVReceipt.RowHeadersVisible = false;
GVReceipt.ColumnCount = 3;
GVReceipt.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
GVReceipt.CellBorderStyle = DataGridViewCellBorderStyle.None;
}
public Form1()
{
InitializeComponent();
pCompanyImage.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap Logo = new Bitmap(Bitmap.FromFile(#"C:/Users/manue/source/repos/MakeyMakey/MakeyMakey/logo.png"));
pCompanyImage.Image = MakeBW(Logo);
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("Hier kann so gut wie alles stehen", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
pQRCode.Image = qrCodeImage;
panel1.Height = GVProduct.Height + 220;
GVReceipt.Columns.Add("Name", "Name");
GVReceipt.Columns.Add("QTY", "QTY");
GVReceipt.Columns.Add("Price", "Price");
GVReceipt.Columns[0].Width = 50;
GVReceipt.Columns[1].Width = 15;
GVReceipt.Columns[2].Width = 50;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string[] row = new string[] {txtName.Text, txtQty.Text, txtPrice.Text };
GVReceipt.Rows.Add(row);
GVProduct.Rows.Add(row);
Gridsettings();
Calc();
txtName.Clear();
txtQty.Clear();
txtPrice.Clear();
txtName.Focus();
}
private void GVResize()
{
//MessageBox.Show(GVReceipt.Height.ToString());
GVReceipt.Height = GVReceipt.ColumnHeadersHeight + GVReceipt.RowCount * GVReceipt.Rows[0].Height;
//MessageBox.Show(GVReceipt.Height.ToString());
panel1.Height += GVReceipt.Rows[0].Height;
//MessageBox.Show(panel1.Height.ToString());
GVReceipt.ClearSelection();
Refresh();
}
private void GVReceipt_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
}
private void GVReceipt_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
panel1.Height = 389;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
public Bitmap MakeBW(Bitmap Map)
{
Bitmap newbmp = new Bitmap(Map.Width, Map.Height); // New image
for (int row = 0; row < Map.Width; row++) // Indicates row number
{
for (int column = 0; column < Map.Height; column++) // Indicate column number
{
var colorValue = Map.GetPixel(row, column); // Get the color pixel
var averageValue = ((int)colorValue.R + (int)colorValue.B + (int)colorValue.G) / 3; // get the average for black and white
newbmp.SetPixel(row, column, Color.FromArgb(averageValue, averageValue, averageValue)); // Set the value to new pixel
}
}
return newbmp;
}
public void Calc()
{
decimal total = 0;
for(int i = 0; i < GVReceipt.RowCount-1; i++)
{
total += Convert.ToDecimal(GVReceipt.Rows[i].Cells[2].Value) * Convert.ToDecimal(GVReceipt.Rows[i].Cells[1].Value);
}
//Console.WriteLine("HELLO");
//MessageBox.Show(total.ToString());
lbltotal.Text = total.ToString() + "€";
Refresh();
GVResize();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Print(panel1);
}
Bitmap MemoryImage;
PrintDocument printdoc1 = new PrintDocument();
PrintPreviewDialog previewdlg = new PrintPreviewDialog();
public void GetPrintArea(Panel pnl)
{
MemoryImage = new Bitmap(pnl.Width, pnl.Height);
Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}
public void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(MemoryImage, 0, 0);
}
public void Print(Panel pnl)
{
GetPrintArea(pnl);
printdoc1.PrinterSettings.PrinterName = "Generico";
printdoc1.DefaultPageSettings.PaperSize = new PaperSize("Custom", panel1.Width, panel1.Height);
previewdlg.Document = printdoc1;
previewdlg.ShowDialog();
printdoc1.Print();
}
private void btnPrinter_Click(object sender, EventArgs e)
{
printDocument1.Print();
GVProduct.Rows.Clear();
GVReceipt.Rows.Clear();
Calc();
}
private void GVReceipt_RowsAdded_1(object sender, DataGridViewRowsAddedEventArgs e)
{
//GVResize();
}
}
I have also included two images, one shows what the receipt looks like, the other what the print preview generated.
I'd highy appreciate any help on that topic.

move the label from left to right in table layout panel c#

I have this code and when I run it, it has moved but has two label display on screen, one is no move, one has move
public partial class Form1: Form
{
int x = 25, y = 1;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.SetBounds(x, y, 255, 255);
x++;
if (x >= 800)
{
x = 1;
}
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "hii";
label1.Font = new Font(" ", 22, FontStyle.Bold);
timer1.Interval = 10;
timer1.Start();
}
}
You have to add one more label from toolbox to form. In timer set new label's bounds also like this:
int x = 25, y = 1;
public Form1()
{
InitializeComponent();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
label1.SetBounds(x, y, 255, 255);
label2.SetBounds(x, 100, 255, 255);
x++;
if (x >= 800)
{
x = 1;
}
}
private void Form1_Load_1(object sender, EventArgs e)
{
label1.Text = "hii";
label1.Font = new Font(" ", 22, FontStyle.Bold);
label2.Text = "hii2";
label2.Font = new Font(" ", 22, FontStyle.Bold);
timer1.Interval = 10;
timer1.Start();
}

Updating a value on another Class

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.

Visual studio c# platformer,background issue

I am currently working on a platformer in C#,VS 2015
The problem i have encountered is:when i press the left/right arrow,the background moves behind the player in the opposite direction,and this happens using a timer(which cannot be set to a value lower than 1 milisecond).
Because the timer is limited to 1 milisecond,the background moves laggy,and i would like to solve this :D
Here is the code
public Form1()
{
InitializeComponent();
}
Bitmap back;
Bitmap bobright;
Bitmap bobleft;
Bitmap bobimage;
Bitmap exit;
Point boblocation = Point.Empty;
Point exitlocation = Point.Empty;
float offSet = 0, vit;
int acc_secunde, acc_secunde_max = 30;
bool left = false, right = false;
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
public void Game()
{
int imageNumber = panel1.Width / back.Width + 3;
using (Bitmap frame = new Bitmap(panel1.Width, panel1.Height))
{
using (Graphics graph = Graphics.FromImage(frame))
{
for (int i = 0; i < imageNumber; i++)
{
graph.DrawImage(back, offSet/2 % back.Width + i * back.Width - back.Width, 0);
}
graph.DrawImage(bobimage, boblocation);
graph.DrawImage(exit,exitlocation);
}
using (Graphics graph = panel1.CreateGraphics())
{
graph.DrawImage(frame, Point.Empty);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
back = Properties.Resources.back;
bobleft = Properties.Resources.bobleft;
bobright = Properties.Resources.bobright;
bobimage = bobright;
exit = Properties.Resources.Exit;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
boblocation.X = panel1.Width / 2 - bobimage.Width / 2;
boblocation.Y = 210;
exitlocation.X = panel1.Width - exit.Width - 10;
exitlocation.Y = 5;
if (left)
{
offSet = offSet + acc_secunde;
bobimage = bobleft;
}
if(right)
{
bobimage = bobright;
offSet = offSet - acc_secunde;
}
Game();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
acceleratie.Start();
left = true;
}
if(e.KeyCode == Keys.Right)
{
acceleratie.Start();
right = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
acceleratie.Stop();
deacceleratie.Start();
}
if(e.KeyCode == Keys.Right)
{
acceleratie.Stop();
deacceleratie.Start();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
private void deacceleratie_Tick(object sender, EventArgs e)
{
if(acc_secunde > 0)
acc_secunde = acc_secunde - acc_secunde_max / 3;
if (acc_secunde <= 0)
{
deacceleratie.Stop();
if (right == true)
right = false;
if (left == true)
left = false;
acc_secunde = 0;
}
}
private void acceleratie_Tick(object sender, EventArgs e)
{
if(acc_secunde< acc_secunde_max)
acc_secunde+=2;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
Point mousePt = new Point(e.X, e.Y);
if (mousePt.X > panel1.Width - exit.Width - 10 && mousePt.X < panel1.Width - 10 && mousePt.Y > panel1.Top + 10 && mousePt.Y < panel1.Top + 10 + exit.Height)
Application.Exit();
}
}
}

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