//缩放图像 private void PicBox_MouseWheel(object sender, MouseEventArgs e) { if (m_nScale <= 0.1 && e.Delta <= 0) { return; //缩小下线 } if (m_nScale >= 10 && e.Delta >= 0) { return; //放大上线 } //获取 当前点到画布坐标原点的距离 SizeF szSub = (Size)m_ptCanvas - (Size)e.Location; //当前的距离差除以缩放比还原到未缩放长度 float tempX = szSub.Width / m_nScale; //这里 float tempY = szSub.Height / m_nScale; //将画布比例 //还原上一次的偏移 //按照当前缩放比还原到 m_ptCanvas.X -= (int)(szSub.Width - tempX); //没有缩放 m_ptCanvas.Y -= (int)(szSub.Height - tempY); //的状态 //重置距离差为 未缩放状态 szSub.Width = tempX; szSub.Height = tempY; m_nScale += e.Delta > 0 ? 0.2F : -0.2F; //重新计算 缩放并 重置画布原点坐标 m_ptCanvas.X += (int)(szSub.Width * m_nScale - szSub.Width); m_ptCanvas.Y += (int)(szSub.Height * m_nScale - szSub.Height); PicBox.Invalidate(); }
/// <summary> /// Make sure that the PicBox have the focus, otherwise it doesn´t receive /// mousewheel events !. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PicBox_MouseEnter(object sender, EventArgs e) { if (PicBox.Focused == false) { PicBox.Focus(); } }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (_isMouseDown) { _size = new Size(Math.Abs(_point.X - e.Location.X), Math.Abs(_point.Y - e.Location.Y)); PicBox.Invalidate(); } }
protected override void OnActivated(EventArgs e)//this function is triggered when the user selects the program window { base.OnActivated(e); if (Started) //if the program already started, don't start again { return; //this gets out of the function } else//if the program hasn't started, do stuff { Started = true; //program needs to remember that we started it } Canvas = PicBox.CreateGraphics(); //these variables help with displaying stuff to the screen Scratch = new Bitmap(PicBox.Width, PicBox.Height); ScratchCanvas = Graphics.FromImage(Scratch); Font fSmall = new Font("Consolas", 10, GraphicsUnit.Point);//fSmall, fMedium, and fBig are 10, 16, and 30 point fonts. Font fMedium = new Font("Consolas", 16, GraphicsUnit.Point); Font fBig = new Font("Consolas", 30, GraphicsUnit.Point); float Theta = 1f;//some numbers int Theta2 = -100; float Radius = 1f; ScratchCanvas.FillRectangle(Brushes.Black, 0, 0, PicBox.Width, PicBox.Height);//make the background black while (!userQuits) { Random Rand = new Random((int)Theta * 1); //creates a random seed byte R = (byte)((Rand.Next(0, 9) * 32) - 1); //Rand.Next makes it so the random will only give a number from 0-8 (9 is length) byte G = (byte)((Rand.Next(0, 9) * 32) - 1); byte B = (byte)((Rand.Next(0, 9) * 32) - 1); ScratchCanvas.FillRectangle(Brushes.Black, 0, 0, PicBox.Width, PicBox.Height); ScratchCanvas.DrawString("Hello World!", fSmall, new SolidBrush(Color.FromArgb(255, R, G, B)), (int)(Radius * Math.Sin(Theta)) + PicBox.Width / 2, (int)(Radius * Math.Cos(Theta)) + PicBox.Height / 2);//This function draws text to a specific location ScratchCanvas.DrawString("Hello World!", fMedium, new SolidBrush(Color.FromArgb(255, B, R, G)), 2 * Theta2, (int)(10 * Math.Cos(Theta2)) + PicBox.Height / 2); ScratchCanvas.DrawString("Hello World!", fBig, new SolidBrush(Color.FromArgb(255, B, G, R)), 800 - 15 * Theta2, 5 * Math.Abs((int)(3 * Math.Cos(Theta2))) + PicBox.Height / 4); /* * The best strategy for displaying stuff to the screen is to figure out everything you want to draw to the screen and put it into * a variable; in this case, ScratchCanvas or Scratch (kinda tricky like that). * Once everything is in Scratch, draw Scratch to the screen. */ Canvas.DrawImage(Scratch, 0, 0); //Draw Scratch Theta2++; //Add stuff to numbers Theta *= 1.01f; Radius *= 1.01f; System.Threading.Thread.Sleep((int)(1000 / 30)); //30 frames per second is a standard for display. Since the sleep function wants //an integer, and 1000/30 is not, we cast to an int Application.DoEvents(); //This checks for things like when we close the program } }
/// <summary> /// 鼠标中键按下 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PicBox_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) { //如果中键点下 初始化计算要用的临时数据 m_ptMouseDown = e.Location; m_ptCanvasBuf = m_ptCanvas; } PicBox.Focus(); }
private void ListPath_SelectedIndexChanged(object sender, EventArgs e) { if (ListPath.SelectedIndex < 0) { return; } string path = ListPath.SelectedItem as string; PicBox.Load(path); }
private void PicBox_MouseMove(object sender, MouseEventArgs e) { if (moveLocation != Point.Empty) { PicBox.BringToFront(); Point p = PicBox.Location; p.X += e.X - moveLocation.X; p.Y += e.Y - moveLocation.Y; PicBox.Location = p; } }
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { _isMouseDown = false; _size = new Size(Math.Abs(_point.X - e.Location.X), Math.Abs(_point.Y - e.Location.Y)); PicBox.Invalidate(); var tmpPoint = _point; var tmpSize = _size; Crop_Release(); }
private void PicBox_MouseUp(object sender, MouseEventArgs e) { var finish = new Point(e.X, e.Y); //для рисования var g = Graphics.FromImage(bm); var pen = new Pen(Color.Black, 5f); g.DrawLine(pen, start, finish); g.Save(); drawing = false; g.Dispose(); PicBox.Invalidate(); }
public Form1() { InitializeComponent(); // initilize JLusb jlusb = new JLusb(); // initilize JLvideo jlvideo = new JLvideo(); // initilize WindowsMediaPlayer //mWinMediaPlayer = new WindowsMediaPlayer(); //mWinMediaPlayer.openPlayer("E:\\迅雷下载\\J点_bd.mp4"); //PicBox.Load("http://images.china.cn/attachement/jpg/site1000/20150803/c03fd556687217294d854a.jpg"); PicBox.Load("o.jpg"); }
/// <summary> /// 重置图片 /// </summary> private void ResetPicture() { if (PicBox.Image != null) { PicBox.Image.Dispose(); //释放原图像 } m_ptCanvas = new Point(0, 0); //画布原点在设备上的坐标 m_ptCanvasBuf = new Point(0, 0); //重置画布坐标计算时用的临时变量 m_ptBmp = new Point(0, 0); //图像位于画布坐标系中的坐标 m_ptMouseDown = new Point(0, 0); //鼠标点下是在设备坐标上的坐标 RefreshFormSize(); PicBox.Invalidate(); //资源回收 GC.Collect(); }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (!drawing) { return; //тут 3 процедуры на рисование - движение мыши, нажатая кнопка и отжатая } var finish = new Point(e.X, e.Y); bm2 = new Bitmap(bm); PicBox.Image = bm2; var pen = new Pen(Color.Black, 5f); var g = Graphics.FromImage(bm2); g.DrawLine(pen, start, finish); g.Dispose(); PicBox.Invalidate(); }
private void Form1_Load(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.CheckFileExists = true; ofd.Filter = "(*.jpg)|*.jpg|(*.png)|*.png"; ofd.Multiselect = true; //ofd.InitialDirectory= if (ofd.ShowDialog() == DialogResult.OK) { FileNames = ofd.FileNames; } if (FileNames != null && FileNames.Length >= 1) { PicBox.Load(FileNames[0]); ListPath.Items.AddRange(FileNames); } }
/// <summary> /// 依据控件图像大小,计算缩放比例,使图像铺满绘图框 /// </summary> public void RefreshFormSize() { if (m_bmp != null) { if ((Width < m_bmp.Width) || (Height < m_bmp.Height)) { m_nScale = ((float)Width / m_bmp.Width) > ((float)Height / m_bmp.Height) ? ((float)Height / m_bmp.Height) : ((float)Width / m_bmp.Width); //this.Width = (int)(m_bmp.Width * m_nScale); //this.Height = (int)(m_bmp.Height * m_nScale); } else { m_nScale = ((float)m_bmp.Width / Width) > ((float)m_bmp.Height / Height) ? ((float)m_bmp.Height / Height) : ((float)m_bmp.Width / Width); //this.Width = (int)(Width * m_nScale); //this.Height = (int)(Height * m_nScale); } } PicBox.Invalidate(); }
/// <summary> /// 平移图像 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PicBox_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) { //移动过程中 中键点下 重置画布坐标系 //我总感觉这样写不妥 但却是方便计算 如果多次这样搞的话 还是重载操作符吧 m_ptCanvas = (Point)((Size)m_ptCanvasBuf + ((Size)e.Location - (Size)m_ptMouseDown)); PicBox.Invalidate(); } //计算 右上角显示的坐标信息 SizeF szSub = (Size)e.Location - (Size)m_ptCanvas; //计算鼠标当前点对应画布中的坐标 szSub.Width /= m_nScale; szSub.Height /= m_nScale; Size sz = TextRenderer.MeasureText(m_strMousePt, this.Font); //获取上一次的区域并重绘 PicBox.Invalidate(new Rectangle(PicBox.Width - sz.Width, 0, sz.Width, sz.Height)); m_strMousePt = e.Location.ToString() + "\n" + ((Point)(szSub.ToSize())).ToString(); sz = TextRenderer.MeasureText(m_strMousePt, this.Font); //绘制新的区域 PicBox.Invalidate(new Rectangle(PicBox.Width - sz.Width, 0, sz.Width, sz.Height)); }
/// <summary> /// 指定位置缩放,查看图片细节 /// </summary> /// <param name="originPos"></param> /// <param name="ratio"></param> public void FocusPic(Point originPos, float ratio) { m_ptCanvas = originPos; //聚焦坐标 m_nScale = ratio; //缩放系数 PicBox.Invalidate(); //重绘 }