ITPub博客

首页 > > > pictureBox如何改变控件位置

pictureBox如何改变控件位置

作者:choubou 时间:2019-08-28 10:53:24 0 删除 编辑
界面:button,timer,picturebox

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace PictureSport;

{

public partial class Test_Picture_Yuan : Form
{
public Test_Picture_Yuan()
{
InitializeComponent();
}

private Graphics g;
private Bitmap bmp;
private Pen pen;
private Point start;
private Point stop;

Bitmap image1;
public Point P_lo;
private void Test_Picture_Yuan_Load(object sender, EventArgs e)
{

timer1.Enabled = false;
bmp = new Bitmap(this.Width, this.Height);
g = Graphics.FromImage(bmp);
//g = this.CreateGraphics();
pen = new Pen(Color.Red);

}
int i = 1;
private void Test_Picture_Yuan_MouseClick(object sender, MouseEventArgs e)
{
if (i == 1)
{
start = e.Location;
i++;
}
else
{
if (i == 2)
{
stop = e.Location;
g.DrawLine(pen, start, stop);
i++;
this.BackgroundImage = bmp;

}
}
}

private void button1_Click(object sender, EventArgs e)
{

// g.DrawLine(pen, start, stop);
//直线的坐标 横轴坐标不变, 纵轴坐标+ - 50
//(pen,start,stop) (pen,new Point (start.X,start.Y-50),new Point (stop.X,stop.Y-50)) (pen, new Point(start.X, start.Y +50), new Point(stop.X, stop.Y +50))
//圆的坐标 横轴坐标不变, 纵轴坐标+ - 50
//(pen,start.X-15,start.Y-15,30,30); (pen, start.X - 15, start.Y - 65, 30, 30); (pen, start.X - 15, start.Y + 35, 30, 30);


g.DrawLine(pen,new Point (start.X,start.Y-50),new Point (stop.X,stop.Y-50));
g.DrawLine(pen, new Point(start.X, start.Y +50), new Point(stop.X, stop.Y +50));

timer1.Enabled = true;


try
{
// Retrieve the image.

image1 = new Bitmap(Application.StartupPath + @"/images/black.png", true);

int x, y;

// Loop through the images pixels to reset color.
for (x = 0; x < image1.Width; x++)
{
for (y = 0; y < image1.Height; y++)
{
Color pixelColor = image1.GetPixel(x, y);
Color newColor = Color.FromArgb(pixelColor.B, 0, 0);
image1.SetPixel(x, y, newColor);
}
}

// Set the PictureBox to display the image.
pictureBox1.Image = image1;


}
catch (ArgumentException)
{
MessageBox.Show("There was an error." +
"Check the path to the image file.");
}


this.BackgroundImage = null;
this.BackgroundImage = bmp;

}

private void timer1_Tick(object sender, EventArgs e)
{
P_lo = pictureBox1.Location;
P_lo = new Point(start.X, start.Y);
P_lo.X ++;
P_lo.Y ++;
pictureBox1.Location = P_lo;
}
}
}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4548/viewspace-3124487/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-08

  • 博文量
    175
  • 访问量
    10707